如何使用 CSS 创建子导航菜单?

cssweb developmentfront end technology更新于 2025/9/27 2:20:17

以下是使用 CSS 创建底部带边框(下划线)导航链接的代码 −

示例

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
   font-family: Arial, Helvetica, sans-serif;
   margin: 0;
}
nav {
   overflow: hidden;
   background-color: rgb(0, 52, 73);
}
nav .links {
   float: left;
   font-size: 16px;
   color: white;
   text-align: center;
   padding: 14px 16px;
   text-decoration: none;
}
.subnav {
   float: left;
   overflow: hidden;
}
.subnav .sub-btn {
   font-size: 16px;
   border: none;
   outline: none;
   color: white;
   padding: 14px 16px;
   background-color: rgb(0, 109, 67);
   margin: 0;
}
nav .links:hover,
.subnav:hover .sub-btn {
   background-color: rgb(101, 219, 255);
   color: black;
   font-weight: bolder;
}
.sub-content {
   display: none;
   position: absolute;
   left: 0;
   background-color: rgb(0, 156, 83);
   width: 100%;
   z-index: 1;
}
.sub-content a {
   float: left;
   color: white;
   text-decoration: none;
}
.sub-content a:hover {
   background-color: #eee;
   color: black;
}
.subnav:hover .sub-content {
   display: block;
}
</style>
</head>
<body>
<nav>
<a class="links" href="#">Home</a>
<a class="links" href="#">Contact</a>
<a class="links" href="#">About Us</a>
<a class="links" href="#">More Info</a>
<div class="subnav">
<button class="sub-btn">Our Social Media></button>
<div class="sub-content">
<a class="links" href="#">Facebook</a>
<a class="links" href="#">Twitter</a>
<a class="links" href="#">LinkedIn</a>
<a class="links" href="#">Instagram</a>
</div>
</div>
</nav>
</body>
</html>

输出

上述代码将产生以下输出 −

当鼠标悬停在下拉按钮上时 −


相关文章


有用资源