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 out</li>
<li>my profile</li>
<li>new 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;
}
source to share
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;
}
source to share