gpt4 book ai didi

html - 设置下拉菜单位置(CSS)

转载 作者:太空宇宙 更新时间:2023-11-04 10:29:51 24 4
gpt4 key购买 nike

我试图将下拉菜单放在黄色边框之后。使用当前代码,菜单覆盖产品按钮,所以我需要它显示在产品的红色处。如“点击我”按钮所示。

代码:

ul {
list-style-type: none;
overflow: hidden;
border: 2px solid red;
}
li {
float: left;
padding: 5px;
margin: 10px;
border: 2px solid green;
}
.button {
display: inline-block;
border: 1px dashed black;
}
.dropdown {
display: inline-block;
border: 1px solid yellow;
}
.menu {
padding: 15px;
display: none;
position: absolute;
border: 1px solid blue;
z-index: 1;
}
.menu a {
padding: 15px;
display: block;
width: 150px;
background-color: pink;
border: 2px solid black;
}
li:hover {
background-color: red;
}
.dropdown:hover {
background-color: red;
}
.dropdown:hover .menu {
display: block;
}
<!DOCTYPE html>
<html>
<head>
<title>Dropdown web</title>
<link rel="stylesheet" type="text/css" href="w3style.css">
</head>
<body>
<ul>
<li>Home</li>
<li>About</li>
<li>Contect Us</li>
<div class="dropdown">
<li>
<a class="button" href="#">Products</a>
</li>
<div class="menu">
<a href="#">01</a>
<a href="#">02</a>
<a href="#">03</a>
<a href="#">04</a>
<a href="#">05</a>
<a href="#">06</a>
</div>
</div>
</ul>
<div class="dropdown">
<button class="button">Click Me</button>
<div class="menu">
<a href="#"> Link 01 </a>
<a href="#"> Link 02 </a>
<a href="#"> Link 03 </a>
</div>
</div>
</body>
</html>

最佳答案

您需要更正 HTML(当前无效),因为只有 liul 的子元素。

然后是一个不同的清除方法,因此下拉列表可以出现在父 ul 之外。

Codepen Demo

.cf:after { /* clearfix */
content: "";
display: table;
clear: both;
}
ul {
list-style-type: none;
border: 2px solid red;
}
li {
float: left;
padding: 5px;
margin: 10px;
border: 2px solid green;
position: relative; /* positioning context */
}
.button {
display: inline-block;
border: 1px dashed black;
}
.menu {
padding: 15px;
display: none;
position: absolute;
top: 100%;
left: 0;
border: 1px solid blue;
z-index: 1;
}
.menu a {
padding: 15px;
display: block;
width: 150px;
background-color: pink;
border: 2px solid black;
}
li:hover {
background-color: red;
}
.dropdown:hover {
background-color: red;
}
.dropdown:hover .menu {
display: block;
}
<ul class="cf">
<li>Home</li>
<li>About</li>
<li>Contect Us</li>
<li class="dropdown">
<a class="button" href="#">Products</a>
<div class="menu">
<a href="#">01</a>
<a href="#">02</a>
<a href="#">03</a>
<a href="#">04</a>
<a href="#">05</a>
<a href="#">06</a>
</div>
</li>
</ul>

关于html - 设置下拉菜单位置(CSS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36576819/

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