Make the dropdown placed inside the wrapper with no relative position

so when i hover over my username a dropdown appears. but this box is not always as wide as the username div which is variable width, so it looks weird. I can solve this by changing the position of the dropdown to relative

. But then when the window appears, the whole page moves down. Is there a way to keep the position as absolute

, but still contain it inside the shell? here's a jsFiddle for a demo. Thanks for reading.

<div id = "wrapper" style = "border:1px solid red;">
          <div id = "username">hover over me</div>
            <div id = "accBox">
              <ul>
                <li>sign&nbsp;out</li>
                <li>my&nbsp;profile</li>
                <li>new&nbsp;account</li>
              <ul>
            </div>
        </div>


   #username{
       color:grey;
       cursor:default;

    }

#accBox{
    border:1px solid grey;
    display:none;
    position:absolute;
    background-color:#fff;
}

#accBox ul{
        font-family:Arial, Helvetica, sans-serif;
        padding:0px;
        margin:0px;
}

#accBox li{
    list-style:none;
    margin: 0px;
    padding:6px;
    overflow:hidden;
    font-size:13px;
    cursor:pointer;
    text-align:left;
}

#accBox li:hover{
              background-color: #DAD6D6;
} 

      

+3


source to share


2 answers


First you need to set position: relative

in the parent div

so that it contains the dropdown absolute

:

<div style = "border:1px solid red;position: relative;">

      

Then you can set left: 0; right: 0

in the dropdown to make it width:



#accBox{
    border:1px solid grey;
    display:none;
    position:absolute;
    background-color:#fff;
    left: 0;
    right: 0;
}

      

FIDDLE

+1


source


See fiddle

Just add

left:8px;
right:8px;

      



This will make the unordered list items as wide as the #accBox div. A smaller value will increase its width, and a larger value will decrease its width.

to #accBox

+2


source







All Articles