这是JSfiddle http://jsfiddle.net/sgmxh1hg/
这是HTML:
<header>
<nav>
<ul id="menu">
<li><a href="#">Home</a>
</li>
<li><a href="#">News</a>
</li>
<li id="streams">
<a href="#">Streams</a>
<ul id="submenu">
<li><a href="#">First link</a>
</li>
<li><a href="#">Second link</a>
</li>
<li><a href="#">Third link</a>
</li>
</ul>
</li>
<li><a href="#">Tournaments</a>
</li>
</ul>
</nav>
</header>
这是CSS:
header {
position: relative;
display: block;
width: 100%;
height: 30px;
background: -webkit-linear-gradient(#eeb418, #e59313);
background: -o-linear-gradient(#eeb418, #e59313);
background: -moz-linear-gradient(#eeb418, #e59313);
background: linear-gradient(#eeb418, #e59313);
margin: 0px;
padding: 0px;
font-family:"Verdana", "Arial", sans-serif;
font-size: 1.4em;
}
ul {
list-style-type: none;
}
nav ul {
padding: 0px;
margin: 0px;
}
nav li {
float: left;
line-height: 30px;
vertical-align: middle;
}
#menu a {
display: block;
padding: 0px 10px;
color: #f4f4f4;
text-decoration: none;
}
#menu li:hover {
color: white;
background: #eeb418;
}
#menu li:hover a {
color: white;
}
#submenu li {
float: none;
}
#submenu li {
float: none;
display: none;
}
#streams:hover ul li {
display: block;
}
如果你将鼠标悬停在&#34; Streams&#34;在菜单中,当子菜单出现时,网站的布局中断。为什么会发生这种情况,我该如何解决?
答案 0 :(得分:2)
实现这一目标的最常见方法是绝对定位元素。在这种情况下,您将绝对定位直接ul
元素。在这样做时,元件基本上从正常流动中移除。因此,当将鼠标悬停在菜单上时,其他元素将不受影响,并且不会再破坏您的布局。
#menu > li > ul {
position: absolute;
background: #eeb418;
}