我有一个CMS生成的主导航菜单,其中包含一个子菜单。两个菜单都是标准的ul标签,带有一个“navCMSListMenuUL”类,然后在我的CSS中相应地设置样式。问题是在a:hover上,容器会扩展以容纳新显示的子导航ul。
我希望sub-nav能够移出容器,并且流量高于容器下方的任何元素。我想在这个导航系统中清理其他一些东西,但这些是辅助导航流程的辅助工具。我在Codepen @ http://codepen.io/patrickTheWizard/pen/ZGWKYX创建了一个简化的测试用例。
<nav class="navigation">
<ul id="menuElem" class="navCMSListMenuUL">
<li class="navCMSListMenuLI">
<a class="navCMSListMenuLink" href="#">Home</a>
</li>
<li class="navCMSListMenuLI">
<a class="navCMSListMenuLink" href="#">About Us</a>
</li>
<li class="navCMSListMenuLI">
<a class="navCMSListMenuLink" href="#">Calendar</a>
</li>
<li class="navCMSListMenuHighlightedLI noseparator">
<a class="navCMSListMenuLinkHighlighted" href="#">Members</a>
<ul class="navCMSListMenuUL">
<li class="navCMSListMenuLI">
<a class="navCMSListMenuLink" href="#">Become a Member</a>
</li>
<li class="navCMSListMenuLI">
<a class="navCMSListMenuLink" href="#">Board Meetings</a>
</li>
<li class="navCMSListMenuLI">
<a class="navCMSListMenuLink" href="#">Committees</a>
</li>
<li class="navCMSListMenuLI">
<a class="navCMSListMenuLink" href="#">Current Members</a>
</li>
<li class="navCMSListMenuLI">
<a class="navCMSListMenuLink" href="#">Member Benefits</a>
</li>
<li class="navCMSListMenuLI noseparator">
<a class="navCMSListMenuLink" href="#">Statistics & Data</a>
</li>
</ul>
</li>
</ul>
/* #01-Header/Nav# */
nav.navigation {
clear: both;
/* background-color: #3c3c3c; */
overflow: visible;
/* padding: 15px; */
/* border-top: 6px solid #2a8cec; */
/* margin-bottom: 30px; */
}
ul#menuElem {
text-align: center;
background-color: #3c3c3c;
border-top: 6px solid #2a8cec;
margin-bottom: 30px;
}
ul#menuElem li {
display: inline-block;
margin: 16px 0 15px 0;
}
ul#menuElem a {
color: #fff;
text-decoration: none;
padding: 15px 35px;
font-weight: 600;
text-transform: uppercase;
border-right: 1px solid;
margin: 15px 0;
margin-left: -4px;
}
ul#menuElem a:hover {
background-color: #2a8cec;
}
ul#menuElem li ul {
display: none;
width: 100%;
float: left;
}
ul#menuElem li:hover ul {
display: block;
}
ul#menuElem ul li {
display: block;
}
答案 0 :(得分:0)
好的,你在做类似菜单的代码时,你将拥有相互覆盖的样式(多个嵌套的UL,LI和A)。
包含“&gt;”的好习惯样式表中的元素,它将查询的范围限制为直接子项。
我已经编辑了一些代码,主要是通过引入直接子规则(&gt;)。我还将子菜单的绝对位置设置为它的父级,这意味着你不会让子菜单推动你的主菜单变大。
代码:
/* #01-Header/Nav# */
nav.navigation {
clear: both;
/* background-color: #3c3c3c; */
overflow: visible;
/* padding: 15px; */
/* border-top: 6px solid #2a8cec; */
/* margin-bottom: 30px; */
}
ul#menuElem {
text-align: center;
background-color: #3c3c3c;
border-top: 6px solid #2a8cec;
margin-bottom: 30px;
}
ul#menuElem > li {
display: inline-block;
position: relative;
margin: 16px 0 15px 0;
}
ul#menuElem > li > a {
color: #fff;
text-decoration: none;
padding: 15px 35px;
font-weight: 600;
text-transform: uppercase;
border-right: 1px solid;
margin: 15px 0;
margin-left: -4px;
}
ul#menuElem > li > a:hover {
background-color: #2a8cec;
}
ul#menuElem > li > ul {
display: none;
width: 100%;
float: left;
}
ul#menuElem > li:hover ul {
display: block;
top: 30px;
right: 0px;
position: absolute;
background: #FFF;
padding: 0px;
margin: 0px;
}
ul#menuElem > li:hover > ul > li{
margin: 0px;
padding: 0px;
list-style: none;
text-align: left;
padding: 5px;
line-height: 30px;
border-bottom: solid 1px #dedede;
}
ul#menuElem > li:hover > ul > li > a{
text-decoration: none;
color: inherit;
}
ul#menuElem > li:hover > ul > li:hover {
background: #000;
color: #FFF;
}
ul#menuElem > ul > li {
display: block;
}