[英]CSS position absolute and width of parent container in percent
I'm trying to build a HTML/CSS dropdown menu which is flexible in width. 我正在尝试构建宽度灵活的HTML / CSS下拉菜单。 Due to the
position:absolute
for the second level of the navigation, I don't get the width of the first level. 由于导航的第二层的
position:absolute
的,我没有得到第一层的宽度。 Removing the position:absolute will move all following elements on hover... 移除position:absolute将在悬停时移动以下所有元素...
How can I solve this? 我该如何解决?
Here is the code: 这是代码:
ul { margin: 0; padding: 0; list-style: none; } .level_1 > li { float: left; width: 45%; background-color: #2FA4CF; margin-right: 6px; } .level_1 > li:hover ul { display: block; } .level_2 { display: none; position: absolute; width: 45%; } .level_2 li { background-color: #535B68; }
<ul class="level_1"> <li> <a href="#">Level one (1)</a> <ul class="level_2"> <li><a href="#">Level two (1)</a></li> </ul> </li> <li><a href="#">Level one (2)</a></li> </ul> <p>Paragraph</p>
See the result here: http://jsfiddle.net/5uf2Y/ Hover "Level one (1)" and you will see, that the second level is not the same size like the first level... 在此处查看结果: http : //jsfiddle.net/5uf2Y/将鼠标悬停在“第一级(1)”上,您将看到第二级的大小与第一级不同...
You have forget two elements for display 100%. 您忘记了两个要显示100%的元素。
1st elements forgets it's : Position relative on level_1 > li 第一个元素忘记了它:在level_1> li上的相对位置
.level_1 > li {
float: left;
width: 45%;
background-color: #2FA4CF;
margin-right: 6px;
**position:relative;**
}
2nd elements corrections it's : change size of 2nd li 第二元素更正是:更改第二锂的大小
.level_2 {
display: none;
position: absolute;
width: 100%;
}
With " width:100%
" on .level_2
it automatically turns with the width of its parent 在
.level_2
上使用“ width:100%
”时,它将自动以其父级的宽度旋转
Add position:relative
to level_1 > li
添加
position:relative
对于level_1 > li
.level_1 > li {
float: left;
width: 45%;
background-color: #2FA4CF;
margin-right: 6px;
position:relative;
}
Try to set the body { width:100%;}
property, it will fix this issue, like shown below (added to your original CSS): 尝试设置
body { width:100%;}
属性,它将解决此问题,如下所示(添加到原始CSS中):
body{ width:100%;}
ul {
margin: 0;
padding: 0;
list-style: none;
}
.level_1 > li {
float: left;
width: 45%;
background-color: #2FA4CF;
margin-right: 6px;
}
.level_1 > li:hover ul {
display: block;
}
.level_2 {
display: none;
position: absolute;
width: 45%;
}
.level_2 li {
background-color: #535B68;
}
Hey man you have a margin of 6px
on your first row li
thats why its a little bigger. 嗨,老兄,你有一个保证金
6px
上的第一行li
这就是为什么它有点大。 I would use a margin left rather than right. 我将使用左边距而不是右边距。 That should fix the spacing.
那应该固定间距。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.