如何使用 CSS 创建可悬停的下拉菜单?
cssweb developmentfront end technology更新于 2025/9/27 0:44:17
以下是使用 CSS 创建可悬停下拉菜单的代码 −
示例
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
.menu-btn {
background-color: #7e32d4;
color: white;
padding: 16px;
font-size: 20px;
font-weight: bolder;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
border: none;
}
.dropdown-menu {
position: relative;
display: inline-block;
}
.menu-content {
display: none;
position: absolute;
background-color: #017575;
min-width: 160px;
z-index: 1;
}
.links {
color: rgb(255, 255, 255);
padding: 12px 16px;
text-decoration: none;
display: block;
font-size: 18px;
font-weight: bold;
border-bottom: 1px solid black;
}
.links:hover {
background-color: rgb(8, 107, 46);
}
.dropdown-menu:hover .menu-content {
display: block;
}
.dropdown-menu:hover .menu-btn {
background-color: #3e8e41;
}
</style>
</head>
<body>
<h2>Hover over the below dropdown button to open dropdown menu</h2>
<div class="dropdown-menu">
<button class="menu-btn">Open </button>
<div class="menu-content">
<a class="links" href="#">Contact Us</a>
<a class="links" href="#">Visit Us</a>
<a class="links" href="#">About Us</a>
</div>
</div>
</body>
</html>
输出
上述代码将产生以下输出 −

将鼠标悬停在打开按钮上时,下拉菜单将打开,如下所示 −

相关文章
有用资源
css 参考教程 - 该教程包含有关 css 的更多信息:https://www.cainiaomax.com/css/

