【发布时间】:2014-02-23 19:24:36
【问题描述】:
我正在尝试使用纯 CSS 和 HTML 创建多级弹出菜单。我已经尝试过this answer 和this one,但是它们都从菜单顶部的第三级开始,而我希望第三级(及更高级别)从与父菜单项相同的高度开始.
我尝试了绝对定位并使用 top 属性将其向下推,但它不再是动态的,并且需要在菜单更改时进行更改,这不是我想要的。
如果可能的话,我希望避免浮动整个菜单,因为这会破坏标题中的其他内容并有可能破坏网站布局。
如果所有文本都比父级短,我还希望第一个子菜单(下拉菜单)的宽度至少与父级相同。
我不需要任何 IE Hacks,因为该网站仅适用于最新的 Chrome 和 Firefox 版本。代码应该是有效的 HTML5/CSS3。
HTML:
<header id="header-box">
<div id="header">
<nav class="primary">
<ul>
<li class="current"><a href="#">Home</a></li>
<li class="link"><a href="#">Some Menu 2</a>
<ul>
<li class="link"><a href="#">SubMenu 1 - 1</a></li>
<li class="Link"><a href="#">SubMenu 1 - 2</a>
<ul>
<li class="link"><a href="#">SubMenu 2 - 1</a></li>
<li class="link"><a href="#">SubMenu 2 - 2</a>
<ul>
<li class="link"><a href="#">SubMenu 3 - 1</a></li>
</ul>
</li>
<li class="link"><a href="#">SubMenu 2 - 3</a></li>
</ul>
</li>
<li class="link"><a href="#">SubMenu 1 - 3</a></li>
</ul>
</li>
<li class="link"><a href="#">Long Menu 3</a>
<ul>
<li class="link"><a href="#">Short 1</a></li>
<li class="link"><a href="#">Short 2</a></li>
</ul>
</li>
<li class="link"><a href="#">Links</a></li>
</ul>
</nav>
</div>
</header>
CSS:
nav {
font-size: 0; /* Remove annoying whitespace between Nav Elements */
}
nav a {
font-size: 1rem; /* Restore Font Size */
padding: 0.5rem;
display: block;
white-space: nowrap;
}
nav ul {
list-style: none; /* Remove Bullets from Lists */
padding: 0; /* left align the Nav */
}
nav li {
display: inline-block;
background-color: #AB2524;
}
nav li:hover {
background-color: #801B1B;
}
nav li.current, nav li.section {
background-color: #D3302E;
}
/* SubMenu Definitions */
nav li ul { /* Hide by default */
display: none;
}
nav li:hover>ul { /* Show Submenu when cursor is on parent */
display: block;
position: absolute; /* Make the menu flow out of the box and overflow the content. */
}
nav li:hover>ul>li { /* Dropdown */
display: block;
}
/* Third Level and below (4th etc.) */
nav li:hover>ul>li:hover>ul { /* Show third level */
display: inline-block;
left: 100%;
/* TODO: Make these submenues appear on the same height (from top of page) as their parent menu item rega*/
}
这可以在没有纯 CSS/HTML 的情况下完成吗?
提前致谢
【问题讨论】:
标签: css drop-down-menu