我无法弄清楚这一点。我有一个水平导航,菜单按钮内的子链接没有显示在按钮下面。请帮忙。谢谢。
<!--Click dropdown menu-->
<nav class="container">
<button class="dropbtn" onclick="dropFile()">File</button>
<div id="myDropFile" class="dropdown-content">
<a href="" >link 1</a>
<a href="">link 2</a>
<a href="">link 3</a>
</div>
<button class="dropbtn" onclick="dropEdit()">Edit</button>
<div id="myDropEdit" class="dropdown-content">
<a href="" >link 4</a>
<a href="">link 5</a>
<a href="">link 6</a>
</div>
<button class="dropbtn" onclick="dropView()">View</button>
<div id="myDropView" class="dropdown-content">
<a href="" >link 7</a>
<a href="">link 8</a>
<a href="">link 9</a>
</div>
</nav>
然后我的css是:
.container {
overflow: hidden;
background-color: #333;
font-family: Arial;
}
.container a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropbtn {
cursor: pointer;
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
}
.dropbtn:hover {
background-color: red;
}
.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;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.show {
display: block;
}
菜单链接全部显示在第一个菜单按钮下,而不是显示在每个特定按钮下。我已经尝试了所有内容并阅读了之前的所有答案。
答案 0 :(得分:0)
您需要拥有一条规则,该规则会影响您悬停在div
上的同级button
。类似的东西:
.dropbtn:hover + .dropdown-content, .dropdown-content:hover {
display: inline-block;
position: absolute;
top: 53px;
right: inherit;
margin-left: -60px;
}
但是,需要修改下拉列表的位置。我会看看我是否可以把一个正在运行的例子放在一起
<强>已更新强>:
调整后的定位(上面的更新规则)。请参阅下面的运行示例
.container {
overflow: hidden;
background-color: #333;
font-family: Arial;
}
.container a {
float: left;
font-size: 16px;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
.dropdown {
float: left;
overflow: hidden;
}
.dropbtn {
cursor: pointer;
font-size: 16px;
border: none;
outline: none;
color: white;
padding: 14px 16px;
background-color: inherit;
}
.dropbtn:hover {
background-color: red;
}
.dropbtn:hover + .dropdown-content, .dropdown-content:hover {
display: inline-block;
position: absolute;
top: 53px;
right: inherit;
margin-left: -60px;
}
.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;
}
.dropdown-content a {
float: none;
color: black;
padding: 12px 16px;
text-decoration: none;
display: block;
text-align: left;
}
.dropdown-content a:hover {
background-color: #ddd;
}
.show {
display: block;
}
<!--Click dropdown menu-->
<nav class="container">
<button class="dropbtn" onclick="dropFile()">File</button>
<div id="myDropFile" class="dropdown-content">
<a href="">link 1</a>
<a href="">link 2</a>
<a href="">link 3</a>
</div>
<button class="dropbtn" onclick="dropEdit()">Edit</button>
<div id="myDropEdit" class="dropdown-content">
<a href="">link 4</a>
<a href="">link 5</a>
<a href="">link 6</a>
</div>
<button class="dropbtn" onclick="dropView()">View</button>
<div id="myDropView" class="dropdown-content">
<a href="">link 7</a>
<a href="">link 8</a>
<a href="">link 9</a>
</div>
</nav>