gpt4 book ai didi

html - 导航子菜单出现在悬停父级之前

转载 作者:行者123 更新时间:2023-11-28 03:46:04 24 4
gpt4 key购买 nike

我有一个水平导航栏,我将子菜单设置为仅当您将鼠标悬停在父级上时才显示,但当我将鼠标移动到父级下方几英寸时,子菜单就会显示。我不确定如何解决这个问题。

HTML:

<body>

<header>
<div class="nav">
<ul class="mainmenu">
<li><a href="home.html">Home</a></li>
<li><a href="photography.html">Photography</a>
<ul class="submenu">
<li><a href="#belize">Belize</a></li>
<li><a href="#fernie">Fernie BC</a></li>
<li><a href="#montana">Montana</a></li>
<li><a href="#philippines">Philippines</a></li>
<li><a href="#tahoe">Lake Tahoe</a></li>
<li><a href="#kids">The Kids</a></li>
</ul>
</li>
<li><a href="woodworking.html">Woodworking</a>
<ul class="submenu">
<li><a href="woodworking.html#furniture">Furniture</a></li>
<li><a href="woodworking.html#cutting">Cutting Boards</a></li>
<li><a href="woodworking.html#bandsaw">Bandsaw Boxes</a></li>
<li><a href="woodworking.html#keepsake">Keepsake Boxes</a></li>
<li><a href="woodworking.html#odds">Odds &amp Ends</a></li>
</ul>
<li><a href="about.html">About</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</div>
</header>

</body>

</html>

CSS:

body {
background-color: rgb(51,51,51);
color: white;
}

.nav > ul {
list-style: none;
background-color: rgba(0, 0, 0, 0.6);
text-align: center;
padding: 10px;
width: 100%;
}

.nav > li {
width: 230px;
border-bottom: none;
height: 50px;
line-height: 40px;
display: inline-block;
margin-right: -4px;
}

.nav > ul > li {
list-style-type: none;
display:inline-block;
padding: 5px 35px 5px 35px;
position: relative;
text-align: left;
line-height: 40px;
font-size: 25px;
}

.nav > ul > li:hover {
background-color: #009933;
}

ul.submenu {
position: absolute;
background-color: #141414;
list-style-type: none;
width: 190px;
padding-left: 0px;
padding-top: 5px;
padding-right: 60px;
margin-top: 15px;
margin-left: -35px;
opacity: 0;
}

ul.submenu li {
padding-left: 25px;
padding-top: 5px;
padding-bottom: 5px;
}

.nav li:hover .submenu {
opacity: 1;
}

ul.submenu li:hover {
color: white;
}

.nav a {
text-decoration: none;
color: white;
display: block;
padding-left: 15px;
padding-right: 15px;
transition: .3s background-color;
}

.nav a:hover {
background-color: #009933;
color: black;
}

最佳答案

将建议添加要隐藏在“li”元素上的可见性属性。不透明度 0 仍然使“li”出现并且“li”元素随子菜单扩展。请参阅下面的代码,希望对您有所帮助。

body {
background-color: rgb(51,51,51);
color: white;
}

.nav > ul {
list-style: none;
background-color: rgba(0, 0, 0, 0.6);
text-align: center;
padding: 10px;
width: 100%;
}

.nav > li {
width: 230px;
border-bottom: none;
height: 50px;
line-height: 40px;
display: inline-block;
margin-right: -4px;
}

.nav > ul > li {
list-style-type: none;
display:inline-block;
padding: 5px 35px 5px 35px;
position: relative;
text-align: left;
line-height: 40px;
font-size: 25px;
}

.nav > ul > li:hover {
background-color: #009933;
}

ul.submenu {
position: absolute;
background-color: #141414;
list-style-type: none;
width: 190px;
padding-left: 0px;
padding-top: 5px;
padding-right: 60px;
margin-top: 15px;
margin-left: -35px;
opacity: 0;
visibility: hidden;
}

ul.submenu li {
padding-left: 25px;
padding-top: 5px;
padding-bottom: 5px;
}

.nav li:hover .submenu {
opacity: 1;
visibility: visible;
}

ul.submenu li:hover {
color: white;
}

.nav a {
text-decoration: none;
color: white;
display: block;
padding-left: 15px;
padding-right: 15px;
transition: .3s background-color;
}

.nav a:hover {
background-color: #009933;
color: black;
}
<body>

<header>
<div class="nav">
<ul class="mainmenu">
<li><a href="home.html">Home</a></li>
<li><a href="photography.html">Photography</a>
<ul class="submenu">
<li><a href="#belize">Belize</a></li>
<li><a href="#fernie">Fernie BC</a></li>
<li><a href="#montana">Montana</a></li>
<li><a href="#philippines">Philippines</a></li>
<li><a href="#tahoe">Lake Tahoe</a></li>
<li><a href="#kids">The Kids</a></li>
</ul>
</li>
<li><a href="woodworking.html">Woodworking</a>
<ul class="submenu">
<li><a href="woodworking.html#furniture">Furniture</a></li>
<li><a href="woodworking.html#cutting">Cutting Boards</a></li>
<li><a href="woodworking.html#bandsaw">Bandsaw Boxes</a></li>
<li><a href="woodworking.html#keepsake">Keepsake Boxes</a></li>
<li><a href="woodworking.html#odds">Odds &amp Ends</a></li>
</ul>
<li><a href="about.html">About</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</div>
</header>

</body>

关于html - 导航子菜单出现在悬停父级之前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43970794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com