每当我将鼠标悬停在菜单上时,我希望它显示一个下拉菜单,但我得到这种奇怪的故障效果,我的代码有问题吗?
例如,如果我将鼠标悬停在菜单上,则会出现下拉菜单,但主菜单也会向下移动,直到我停止在主菜单上停留。
body {
text-align: center;
margin: 0;
background-color: white;
}
nav {
position: fixed;
width: 100%;
height: 100px;
background-color: rgba(0, 0, 0, 0.2);
z-index: 99;
color: white;
}
nav li {
display: inline-block;
list-style: none;
margin-left: 40px;
font-size: 25px;
line-height: 80px;
}
nav a {
color: white;
font-family: arial;
text-decoration: none;
}
nav a:hover {
border-bottom-color: #29b26b;
border-bottom-style: solid;
border-bottom-width: 100%;
}
nav ul li ul li {
display: none;
background-color: black;
}
nav ul li:hover ul li {
display: block;
}

<nav>
<ul>
<li><a href="#">Home</a>
<ul>
<li><a href="#">Button 1</a></li>
<li><a href="#">Button 2</a></li>
</ul>
</li>
<li><a href="#">About Us</a>
<ul>
<li><a href="#">Button 1</a></li>
<li><a href="#">Button 2</a></li>
</ul>
</li>
<li><a href="#">The Team</a>
<ul>
<li><a href="#">Button 1</a></li>
<li><a href="#">Button 2</a></li>
</ul>
</li>
</ul>
</nav>
&#13;
答案 0 :(得分:0)
/* Add these two classses */
nav ul {
position:relative;
}
nav ul li ul{
position:absolute;
}
/* Add these two classses ------------ End */
body {
text-align: center;
margin: 0;
background-color: white;
}
nav {
position: fixed;
width: 100%;
height: 100px;
background-color: rgba(0, 0, 0, 0.2);
z-index: 99;
color: white;
}
nav li {
display: inline-block;
list-style: none;
margin-left: 40px;
font-size: 25px;
line-height: 80px;
}
nav a {
color: white;
font-family: arial;
text-decoration: none;
}
nav a:hover {
border-bottom-color: #29b26b;
border-bottom-style: solid;
border-bottom-width: 100%;
}
nav ul li ul li {
display: none;
background-color: black;
}
nav ul li:hover ul li {
display: block;
}
&#13;
<nav>
<ul>
<li><a href="#">Home</a>
<ul>
<li><a href="#">Button 1</a></li>
<li><a href="#">Button 2</a></li>
</ul>
</li>
<li><a href="#">About Us</a>
<ul>
<li><a href="#">Button 1</a></li>
<li><a href="#">Button 2</a></li>
</ul>
</li>
<li><a href="#">The Team</a>
<ul>
<li><a href="#">Button 1</a></li>
<li><a href="#">Button 2</a></li>
</ul>
</li>
</ul>
</nav>
&#13;
问题这是当您在悬停时将内部下拉列表ul设为display:block
时。它正在推动剩余的li
项目。
您应将下拉列表ul
position
设置为absolute
,并将父ul
position
设为relative
答案 1 :(得分:0)
也许是这样的? 我在代码中添加了注释。
/* CSS */
body
{
text-align: center;
margin: 0;
background-color: grey;
}
nav
{
position: fixed;
width: 100%;
height: 100px;
background-color: rgba(0, 0, 0, 0.2);
z-index: 99;
color: white;
}
nav li
{
display: inline-block;
list-style: none;
margin-left: 40px;
font-size: 25px;
line-height: 80px;
}
nav a
{
color: white;
font-family: arial;
text-decoration: none;
}
nav a:hover
{
border-bottom-color: #29b26b;
border-bottom-style: solid;
border-bottom-width: 100%;
}
nav ul li {
/* Important to make sub elements be positioned as absolute*/
position: relative;
}
nav ul li ul
{
position: absolute;
display: none;
background-color: black;
/* Width is derived from width of parent nav ul, You need to set it here*/
width: 200px;
}
/* Show the whole ul part and not individual li elements*/
nav ul li:hover ul
{
display: block;
}
&#13;
<nav>
<ul>
<li><a href="#">Home</a>
<ul>
<li><a href="#">Button 1</a></li>
<li><a href="#">Button 2</a></li>
</ul>
</li>
<li><a href="#">About Us</a>
<ul>
<li><a href="#">Button 1</a></li>
<li><a href="#">Button 2</a></li>
</ul>
</li>
<li><a href="#">The Team</a>
<ul>
<li><a href="#">Button 1</a></li>
<li><a href="#">Button 2</a></li>
</ul>
</li>
</ul>
</nav>
&#13;