Make sure the dropdown always appears on top of its parent, position: relative
P粉738346380
P粉738346380 2024-04-01 18:43:16
0
1
394

I'm trying to achieve something like below.

  • The container contains a set of dropdown lists that activate when clicked and are located on top of each parent.

the problem we are facing:

The problem I'm having right now is that the dropdown menu is hidden inside the container instead of appearing on top of the parent level.

What I tried:

I tried position:absolute and it works great for putting the list on top of each parent.

On the other hand, an overflowed dropdown will retain its position at its set location rather than following its dropdown label.

Therefore, position:relative is required.


Can someone provide some clarification on this?

Thank you for your help.

http://jsfiddle.net/6r94bpof/

    html,body{
    margin: 0;
    padding: 0;
    overflow: hidden;
    }
    
    .land{
        display: grid;
        grid-template-rows: 1fr 8fr;
        height: 100vh;
    
    }
    
    .mainmenu{
        border: solid 2px grey;
        width: 100%;
        height: 80px;
        display: flex;
        overflow: hidden;
        position: relative;
    }
    
    .left{
        width: 50%;
        max-width: 50%;
    }
    
    .left-selection{
        display: flex;
        overflow-y: hidden;
        overflow-x: scroll;
    }
    
    .right{
        width: 50%;
        max-width: 50%;
    }
    
    .right-selection{
        display: flex;
        overflow-y: hidden;
        overflow-x: scroll;
    }
    
    .ddgroup{
        display: flex;
    }
    
    .ddlist{
        position: relative;
        display: none;
        list-style-type: none;
    }

    input[type=checkbox]:checked ~ .ddlist{
        display: block;
        position: relative;
    }
    
 <html>
    <body>
    <div class="land">
      <div class="mainmenu">
          <div class="left">
              <div class="left-selection">
                  <div class="ddgroup">
                      <ul class="ddmenu">
                          <input type="checkbox" id="ctrl"/>
                          <label class="btn" for="ctrl">Click me!</label> 
                          <ul class="ddlist">
                              <li><a href="#">Sub-1</a></li>
                              <li><a href="#">Sub-2</a></li>
                              <li><a href="#">Sub-3</a></li>
                          </ul> 
                      </ul>
                      <ul class="ddmenu">
                          <input type="checkbox" id="ctrl"/>
                          <label class="btn" for="ctrl">Click me!</label>          
                          <ul class="ddlist">
                              <li><a href="#">Sub-1</a></li>
                              <li><a href="#">Sub-2</a></li>
                              <li><a href="#">Sub-3</a></li>
                          </ul> 
                      </ul>
                      <ul class="ddmenu">
                          <input type="checkbox" id="ctrl"/>
                          <label class="btn" for="ctrl">Click me!</label>          
                          <ul class="ddlist">
                              <li><a href="#">Sub-1</a></li>
                              <li><a href="#">Sub-2</a></li>
                              <li><a href="#">Sub-3</a></li>
                          </ul> 
                      </ul>
                      <ul class="ddmenu">
                          <input type="checkbox" id="ctrl"/>
                          <label class="btn" for="ctrl">Click me!</label>          
                          <ul class="ddlist">
                              <li><a href="#">Sub-1</a></li>
                              <li><a href="#">Sub-2</a></li>
                              <li><a href="#">Sub-3</a></li>
                          </ul> 
                      </ul>
                      <ul class="ddmenu">
                          <input type="checkbox" id="ctrl"/>
                          <label class="btn" for="ctrl">Click me!</label>          
                          <ul class="ddlist">
                              <li><a href="#">Sub-1</a></li>
                              <li><a href="#">Sub-2</a></li>
                              <li><a href="#">Sub-3</a></li>
                          </ul> 
                      </ul>
                      <ul class="ddmenu">
                          <input type="checkbox" id="ctrl"/>
                          <label class="btn" for="ctrl">Click me!</label>          
                          <ul class="ddlist">
                              <li><a href="#">Sub-1</a></li>
                              <li><a href="#">Sub-2</a></li>
                              <li><a href="#">Sub-3</a></li>
                          </ul> 
                      </ul>
                      <ul class="ddmenu">
                          <input type="checkbox" id="ctrl"/>
                          <label class="btn" for="ctrl">Click me!</label>          
                          <ul class="ddlist">
                              <li><a href="#">Sub-1</a></li>
                              <li><a href="#">Sub-2</a></li>
                              <li><a href="#">Sub-3</a></li>
                          </ul> 
                      </ul>
                      <ul class="ddmenu">
                          <input type="checkbox" id="ctrl"/>
                          <label class="btn" for="ctrl">Click me!</label>          
                          <ul class="ddlist">
                              <li><a href="#">Sub-1</a></li>
                              <li><a href="#">Sub-2</a></li>
                              <li><a href="#">Sub-3</a></li>
                          </ul> 
                      </ul>
                      <ul class="ddmenu">
                          <input type="checkbox" id="ctrl"/>
                          <label class="btn" for="ctrl">Click me!</label>          
                          <ul class="ddlist">
                              <li><a href="#">Sub-1</a></li>
                              <li><a href="#">Sub-2</a></li>
                              <li><a href="#">Sub-3</a></li>
                          </ul> 
                      </ul>
                  </div>   
              </div>
          </div>
          <div class="right">
              <div class="right-selection">
                <h1>Test<h1/>
                <h1>Test<h1/>
              </div>
          </div>
      </div>
    </div>
    </body>
    </html>

P粉738346380
P粉738346380

reply all(1)
P粉896751037

I couldn't copy your html and css so I created my own, if it doesn't match your query please let me know and I will adjust

 *{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    
}


input{


display:none;
}
ul, li{
    list-style: none;
    
}
ul{
    display: flex;
    justify-content: space-around;
    gap:1rem;
    padding: 1rem ;
}
li{
    position: relative;
    
}
.dropdown-content {
    box-shadow: rgba(50, 50, 93, 0.25) 0px 13px 27px -5px, rgba(0, 0, 0, 0.3) 0px 8px 16px -8px;
    margin-top: 10px;
    display: none;
}
.dropdown-content > *{
display: block;
text-align: center;
text-decoration: none;
color: #38dce5;
margin-top: 0.3rem;
padding: 0.3rem;


}

input:checked ~ .dropdown-content {
    display: block;
}
 

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template