gpt4 book ai didi

html - 下拉菜单水平显示子菜单而不是垂直显示

转载 作者:太空宇宙 更新时间:2023-11-04 09:40:59 25 4
gpt4 key购买 nike

我需要以下方面的帮助,任何帮助都会很棒。我敢肯定这很简单,可能是看了太久了

下拉菜单水平显示子菜单而不是垂直显示,而且子菜单背景保留在那里。

代码:

body {
font-family: arial, sans-serif;
}
* {
padding: 0px;
margin: 0px;
}
#Header {
width: 100%;
height: 100vh;
background: url(images/glasgow.jpg);
background-size: cover;
}
/*Top Logo*/

#Logo {
width: 115px;
height: auto;
float: left;
margin: 15px;
cursor: pointer;
}
/*Navigation ul*/

#NavBar ul {
width: 100%;
height: 100px;
background: #6563af;
line-height: 100px;
}
/*Navigation li*/

#NavBar ul li {
list-style-type: none;
display: inline;
float: right;
}
/*Navigation a*/

#NavBar ul li a {
color: white;
text-decoration: none;
padding: 20px;
}
/*Navigation li HOVER*/

#NavBar ul li:hover {
background: #8269e0;
transition: all 0.40s;
}
#NavBar ul li ul li {
display: none;
}
#NavBar ul li:hover ul li {
display: inline;
}
<body>
<div id="MainContainer">
<!--Start of MainContainer-->

<div id="Header">
<!--Start of Header-->

<div>
<img id="Logo" src="images/logo.png">
</div>

<div id="NavBar">
<ul>
<li><a href="#">Item 5</a>
</li>
<li><a href="#">Item 4</a>
<ul>
<li><a href="#">Sub 1</a>
</li>
<li><a href="#">Sub 2</a>
</li>
<li><a href="#">Sub 3</a>
</li>
</ul>
</li>
<li><a href="#">Item 3</a>
</li>
<li><a href="#">Item 2</a>
</li>
<li><a href="#">Item 1</a>
</li>
</ul>
</div>

</div>
<!--End of Header-->




</div>
<!--End of MainContainer-->
</body>

最佳答案

你需要显示/隐藏 <UL> 而不是 <li> 子菜单元素并且不需要提及 parent 的高度 <UL>

检查工作演示

body {
font-family: arial, sans-serif;
}
* {
padding: 0px;
margin: 0px;
}
#Header {
width: 100%;
height: 100vh;
background: url(images/glasgow.jpg);
background-size: cover;
}
/*Top Logo*/

#Logo {
width: 115px;
height: auto;
float: right;
margin: 15px;
cursor: pointer;
}
/*Navigation ul*/

#NavBar ul {
width: 100%;
background: #6563af;
line-height: 100px;
}
/*Navigation li*/

#NavBar ul li {
list-style-type: none;
float: left;
position:relative;
}

#NavBar ul:after{content:"";display:block;clear:both}

/*Navigation a*/

#NavBar ul li a {
color: white;
text-decoration: none;
padding: 20px;
}
/*Navigation li HOVER*/

#NavBar ul li:hover {
background: #8269e0;
transition: all 0.40s;
}

/*hide submenu*/
#NavBar ul li ul {
display: none;
}

/*Show submenu at hover*/
#NavBar ul li:hover ul {
display:block;
position:absolute;
top:100px

}

/*Make submenu vertical */
#NavBar ul li ul li{
display: block;
float:none;
line-height:50px
}
<div id="MainContainer"><!--Start of MainContainer-->

<div id="Header"><!--Start of Header-->

<div><img id="Logo"src="images/logo.png"></div>

<div id="NavBar">
<ul>
<li><a href="#">Item 5</a></li>
<li><a href="#">Item 4</a>
<ul class="sub-ul">
<li><a href="#">Sub 1</a></li>
<li><a href="#">Sub 2</a></li>
<li><a href="#">Sub 3</a></li>
</ul>
</li>
<li><a href="#">Item 3</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 1</a></li>
</ul>
</div>

</div><!--End of Header-->




</div><!--End of MainContainer-->

希望对你有帮助

关于html - 下拉菜单水平显示子菜单而不是垂直显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39929201/

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