我正在使用 bootstrap 2。我有下拉菜单,其中包含填充下拉菜单的各种元素。我将溢出设置为隐藏,以便保留边界半径。当位置设置为绝对时,伪前/后箭头消失。当位置为静态时它们仍然可见,但我必须使用绝对位置。
HTML:
<div class="dropdown">
<ul class="dropdown-menu">
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
</div>
CSS:
.dropdown {
position: relative;
}
.dropdown-menu {
position: static;
display: block;
overflow: hidden;
float: left;
min-width: 0;
padding: 5px 0;
margin: 0;
list-style: none;
background-color: #fff;
border: 1px solid rgba(0, 0, 0, 0.1);
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
border-radius: 6px;
-webkit-box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
-moz-box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
-webkit-background-clip: padding-box;
-moz-background-clip: padding-box;
background-clip: padding-box;
}
li {
padding: 4px 10px;
}
.dropdown-menu:before {
content: '';
display: inline-block;
border-left: 7px solid transparent;
border-right: 7px solid transparent;
border-bottom: 7px solid #ccc;
border-bottom-color: rgba(0, 0, 0, 0.2);
position: absolute;
top: -7px;
left: 16px;
}
.dropdown-menu:after {
content: '';
display: inline-block;
border-left: 6px solid transparent;
border-right: 6px solid transparent;
border-bottom: 6px solid #ffffff;
position: absolute;
top: -5px;
left: 17px;
}
这是一个 fiddle :http://jsfiddle.net/rdellara/4epZV/
改变位置:静态;在 .dropdown 中绝对查看示例。
最佳答案
这是因为你在.dropdown-menu
上设置了overflow:hidden
,因此,如果你绝对将伪元素放在元素之外,它们将不会出现。
jsFiddle example - overflow:hidden
已删除 - 现在可以使用了。
至于为什么overflow:hidden
在position为static
时不起作用,这本身就是另一个问题,除了我找不到任何相关规范:
CSS Positioned Layout Module Level 3 - 6.7. static
The box is a normal box, laid out according to the normal flow. The ‘top’, ‘right’, ‘bottom’, and ‘left’ properties do not apply.
我猜是因为元素没有定位,所以不会溢出。
关于css - 伪类后/前显示位置静态但不是绝对位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20013705/