纯 CSS 导航遇到问题
在这里小提琴: http://jsfiddle.net/csaltyj/3A78u/
我想要子子导航菜单与其父级的顶部对齐(因此 top: 0),但由于某种原因它与父级的父级对齐。我不确定发生了什么事......有什么想法吗?
HTML:
<div id="nav">
<ul>
<li><a>Item One</a></li>
<li><a>Item Two</a>
<ul>
<li><a>Item two has babies</a></li>
<li><a>Baby #2</a>
<ul>
<li><a>Sub-babies</a></li>
<li><a>This is fun</a></li>
<li><a>Last Item</a></li>
</ul>
</li>
<li><a>SubSub</a>
<ul>
<li><a>Another Item</a></li>
<li><a>Another!</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
CSS:
#nav {
height: 40px;
}
#nav ul {
list-style: none;
font-family: Arial, sans-serif;
font-size: 0.8em;
}
/* When mousing over any LI, reveal its UL if any */
#nav > ul li:hover > ul {
display: block;
}
/* For all links */
#nav a {
text-decoration: none;
color: #000;
}
/* Main nav styling */
#nav > ul > li > a {
padding: 1em;
}
#nav a:hover {
color: red;
}
#nav > ul > li {
float: left;
border: 1px solid #999;
}
/* Subnav styling */
#nav > ul ul {
display: none;
position: absolute;
font-size: 1em;
background: #eee;
padding: 0.5em;
border: 1px solid #999;
}
/* Subsubnav styling */
#nav > ul ul ul {
left: 50px;
top: 0;
width: 150px;
position: absolute;
}
#content {
}
Fiddle here: http://jsfiddle.net/csaltyj/3A78u/
I want the sub-sub nav menu to align with the top of its parent (hence top: 0) but it aligns with the parent's parent for some reason. I'm not sure what's going on.. any ideas?
HTML:
<div id="nav">
<ul>
<li><a>Item One</a></li>
<li><a>Item Two</a>
<ul>
<li><a>Item two has babies</a></li>
<li><a>Baby #2</a>
<ul>
<li><a>Sub-babies</a></li>
<li><a>This is fun</a></li>
<li><a>Last Item</a></li>
</ul>
</li>
<li><a>SubSub</a>
<ul>
<li><a>Another Item</a></li>
<li><a>Another!</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
CSS:
#nav {
height: 40px;
}
#nav ul {
list-style: none;
font-family: Arial, sans-serif;
font-size: 0.8em;
}
/* When mousing over any LI, reveal its UL if any */
#nav > ul li:hover > ul {
display: block;
}
/* For all links */
#nav a {
text-decoration: none;
color: #000;
}
/* Main nav styling */
#nav > ul > li > a {
padding: 1em;
}
#nav a:hover {
color: red;
}
#nav > ul > li {
float: left;
border: 1px solid #999;
}
/* Subnav styling */
#nav > ul ul {
display: none;
position: absolute;
font-size: 1em;
background: #eee;
padding: 0.5em;
border: 1px solid #999;
}
/* Subsubnav styling */
#nav > ul ul ul {
left: 50px;
top: 0;
width: 150px;
position: absolute;
}
#content {
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将
position:relative
添加到“level 2”li
元素:这是一个婴儿全部排队的版本:http://jsfiddle.net/3A78u/2/
如果您想使用
>
,则会是#nav > ul>力> ul> li
.You need to add
position: relative
to the "level 2"li
elements:Here's a version where the babies are all lined up: http://jsfiddle.net/3A78u/2/
If you'd like to use
>
, it would be#nav > ul > li > ul > li
.“top”属性相对于第一个非静态定位的父级。在这种情况下,它是 UL 元素(绝对定位)。您需要添加“位置:相对;”到列表项以获取内部元素以相对于它对齐。
The 'top' property is relative to the first non-statically positioned parent. In this is case it's the UL element (which is absolutely positioned). You need to add 'position:relative;' to the list-items to get elements inside to align relatively to it.