[英]One-level drop-down menu from ul list styled with css possible?
I have a list of items of which only the first is visible and on list hover shows all items with side effect of changing the position of surrounding content . 我有一个项目列表,其中只有第一个可见,并且悬停在列表上显示了所有具有更改周围内容位置副作用的项目。 How to evade this unwanted effect?
如何逃避这种不良影响?
Here is an example list: http://jsfiddle.net/dsbonev/z8Sjy/ 这是一个示例列表: http : //jsfiddle.net/dsbonev/z8Sjy/
All examples that I checked for styling menus have a two-level structure (parent -> children). 我检查过样式菜单的所有示例均具有两级结构(父级->子级)。 On parent hover children are shown.
在父悬停上显示了孩子。 But I don't have a parent to hover onto nor I want to promote one of the children as a parent by moving it out of the list and thus breaking the semantic of the markup.
但是我没有父母可以悬停,也不想通过将其移出列表并破坏标记的语义来提升其中一个孩子作为父母。
Figured it out! 弄清楚了! This is what I wanted:
这就是我想要的:
http://jsfiddle.net/z8Sjy/ http://jsfiddle.net/z8Sjy/
I accept comments with shortcomings or improvements of this method. 我接受这种方法的缺点或改进的评论。
HTML
的HTML
<div class="list-wrapper">
<ul class="items">
<li>stackoverflow</li>
<li>superuser</li>
<li>serverfault</li>
</ul>
</div>
CSS
的CSS
.list-wrapper, .items {
display: inline-block;
}
.list-wrapper {
position: relative;
background-color: blue;
height: 1em;
}
.items {
position: absolute;
background-color: red;
}
.items > li:not(:first-child) {
display: none;
}
.items:hover > li:not(:first-child) {
display: block;
}
Instead of using display: none
& display: block
use visibility: hidden
& visibility: visible
. 而不是使用
display: none
& display: block
使用visibility: hidden
和visibility: visible
。 That way they take up the space in the HTML document, but are not shown: 这样,它们将占用HTML文档中的空间,但未显示:
Working example: http://jsfiddle.net/z8Sjy/3/
工作示例: http : //jsfiddle.net/z8Sjy/3/
The following CSS would be more cross-browser compatable for showing / hiding "not first-child" elements as the selector :not
is actually CSS3. 以下CSS与跨浏览器的兼容性更好,因为选择器
:not
实际上是CSS3,用于显示/隐藏“非第一子”元素。
.items > li:first-child ~ li {
display: none;
}
.items:hover > li:first-child ~ li {
display: block;
}
You could position the list absolutely and then add padding to the paragraph to compensate. 您可以绝对定位列表,然后在段落中添加填充以进行补偿。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.