gpt4 book ai didi

html - 子菜单不会显示为 block

转载 作者:行者123 更新时间:2023-11-28 08:12:49 25 4
gpt4 key购买 nike

我正在尝试创建一个下拉菜单,但子菜单似乎继承了主菜单的特征。子菜单水平显示而不是向下显示,背景颜色也被继承。如果您能提供帮助,那就太好了。我对 CSS 还是很陌生,所以请原谅我的简单错误。

这是 fiddle :https://jsfiddle.net/9xt45qrt/

html

 <head>
<meta charset="utf-8">
<title>E&L </title>
<link rel="stylesheet" href="Envy.css">
</head>
<body>
<header id="M_head">
<nav id="M_nav">
<ul>
<div id="tit">E&L </div>
<li>M<span>e</span>n
<ul class="SSM">
<li>Sub</li>
<li>Sub</li>
<li>Sub</li>
</ul>
</li>
<li>Wo<span>me</span>n</li>
<li>K<span>id</span>z</li>
<li>Acce<span>ssor</span>ies</li>
</ul>
</nav>
</header>

<section id="M_section">

</section>
</body>

CSS:

*{
list-style-type:none;
}
span{
color:#33ff00;
}
#tit{
float:left;
position:relative;
left:-42px;
background:-webkit-linear-gradient(black,green,white,green,black);
font-size:3em;
font-style:italic;
font-weight:bold;
border-left:2px solid black;
}

#M_head ul {
text-align:left;
border:2px solid black;
width:90%;
margin:1em auto;
word-spacing:5em;
background:-webkit-linear-gradient(black,black,grey,black,black);
}

#M_head ul >li{
color:white;
display:inline-block;
margin:0em -1em;
font-size:2em;
padding: 10px 60px;
cursor:pointer;
-webkit-transform: all 0.2s;
-moz-transform: all 0.2s;
-o-transform:all 0.2s;
transform: all 0.2s;
}

#M_head ul >li:hover{
color:#33ff00;
background-color:black;
}
#M_head li:hover ul{
visibility:visible;
}
ul.SSM {
display:block !important;
position:absolute;
font-size:0.5em;
width:50%;
visibility:hidden;
background-color:#33ff00;
}
ul.SSM li{
display:none;
}

最佳答案

您的 CSS 编写顺序错误,CSS #M_head ul#M_head ul >li 将应用于子导航和主导航。我重新排序了 CSS 并为选择器添加了一些特殊性。而不是 #M_head ul 我添加了 #M_head > nav > ul 作为主导航和 #M_head > nav > ul > li:hover ul.SSM 用于悬停时的子导航。

已更新 jsFiddle link

这是代码。

* {
list-style-type: none;
}
span {
color: #33ff00;
}
#tit {
float: left;
position: relative;
left: -42px;
background: -webkit-linear-gradient(black, green, white, green, black);
font-size: 3em;
font-style: italic;
font-weight: bold;
border-left: 2px solid black;
}
#M_head ul {
text-align: left;
border: 2px solid black;
width: 100%;
margin: 1em auto;
word-spacing: 3em;
background: -webkit-linear-gradient(black, black, grey, black, black);
}
#M_head > nav > ul >li {
display: inline-block;
margin: 0em -1em;
}
#M_head ul >li {
color: white;
font-size: 2em;
padding: 10px 20px;
cursor: pointer;
-webkit-transform: all 0.2s;
-moz-transform: all 0.2s;
-o-transform: all 0.2s;
transform: all 0.2s;
}
#M_head ul >li:hover {
color: #33ff00;
background-color: black;
}
#M_head > nav > ul > li ul.SSM {
display: none;
position: absolute;
font-size: 0.5em;
width: 50%;
background-color: #33ff00;
margin: 0;
}
#M_head > nav > ul > li:hover ul.SSM {
display: block;
}
<body>
<header id="M_head">
<nav id="M_nav">
<ul>
<div id="tit">E&L </div>
<li>M<span>e</span>n
<ul class="SSM">
<li>Sub</li>
<li>Sub</li>
<li>Sub</li>
</ul>
</li>
<li>Wo<span>me</span>n</li>
<li>K<span>id</span>z</li>
<li>Acce<span>ssor</span>ies</li>
</ul>
</nav>
</header>
<section id="M_section"> </section>
</body>

希望这对您有所帮助。

关于html - 子菜单不会显示为 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29159031/

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