我正在为网站创建一个下拉菜单。我使第一段工作,但是当我要制作一个元素以在其右侧显示其他选项时,那只是不起作用。
这只是一些基本的HTML和CSS设置,我只是一个初学者。我已经尝试整理代码并以最简单的方式进行处理。
li a, dropbtn {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: #111;
}
li.dropdown {
display: inline-block;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.two {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
margin-top: -40px;
margin-left: 160px;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1;
}
<!-- About section -->
<li class="dropdown">
<a class="dropbtn">About</a>
<div class="dropdown-content">
<a href="mission.html">Mission</a>
<div class="two">
<a href="team.html">Team</a>
</div>
<a href="mission.html">Our Story</a>
<a href="mission.html">Partners</a>
<a href="team.html">Team</a>
</div>
</li>
我想了解自己在做错什么,以及如何进行这项工作。
答案 0 :(得分:0)
使用+
符号在CSS中选择子菜单并将其设置为display:block
。检查以下代码:
.dropdown-content a:hover + div {
display:block;
}
请参见下面的代码段
li a, dropbtn {
display: block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
background-color:gray;
}
li a:hover, .dropdown:hover .dropbtn {
background-color: #111;
}
li.dropdown {
display: inline-block;
}
.dropdown:hover .dropdown-content {
display: block;
}
.dropdown-content {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.two {
display: none;
position: absolute;
background-color: #f9f9f9;
min-width: 160px;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
margin-top: -40px;
margin-left: 160px;
}
.dropdown-content a {
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #f1f1f1;
}
.dropdown-content a:hover + div {
display:block;
}
<!-- About section -->
<li class="dropdown">
<a class="dropbtn">About</a>
<div class="dropdown-content">
<a href="mission.html">Mission</a>
<div class="two">
<a href="team.html">Team</a>
</div>
<a href="mission.html">Our Story</a>
<a href="mission.html">Partners</a>
<a href="team.html">Team</a>
<div class="two">
<a href="ceo.html">CEO</a>
</div>
</div>
</li>