You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何移除导航栏Dropdown Menu的底部边框?

解决下拉菜单底部边框问题

问题描述

借助网友帮助制作出第一个下拉菜单,但下拉菜单中的选项仍显示底部边框。尝试在.navbar ul li a中添加border-bottom: 0px无效,相关代码如下:

原CSS代码

* {
  margin: 0;
  padding: 0;
  background-color: white;
}

body {
  font-family: Arial, Helvetica, sans-serif;
}

header {
  background-color: rgb(255, 255, 255);
  height: 600px;
}

.navbar {
  width: 85%;
  margin: auto;
  padding: 60px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  width: 160px;
  cursor: pointer;
  margin-right: auto;
}

.navbar .dropdown {
  display: none;
  box-shadow: 0px 0px 2px #000;
  border-radius: 5px;
  left: -30%;
}

.navbar ul li:hover .dropdown {
  display: block;
}

.navbar>ul>li {
  position: relative;
}

.navbar>ul>li>.dropdown {
  position: absolute;
  top: 1.4em;
}

.navbar .dropdown li {
  display: block;
  margin: 15px;
  text-align: center;
}

.navbar ul li {
  list-style: none;
  display: inline-block;
  margin: 0 20px;
  position: relative;
}

.navbar ul li a {
  text-decoration: none;
  color: black;
  border-bottom: 0px;
}

.navbar ul li a:hover {
  color: teal;
}

.navbar ul li::after {
  content: '';
  height: 3px;
  width: 0;
  background: teal;
  position: absolute;
  left: 0;
  bottom: 0px;
  transition: 0.5s;
}

.navbar ul li:hover::after {
  width: 100%;
}

button {
  list-style: none;
  border: none;
  background: teal;
  padding: 10px 20px;
  border-radius: 30px;
  color: white;
  font-size: 15px;
  transition: 0.4s;
}

原HTML代码

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>MediCare</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <header>
    <div class="banner">
      <div class="navbar">
        <img src="icon.png" class="logo">
        <ul>
          <li><a href="#">Home</a></li>
          <li><a href="#">Products</a>
            <ul class="dropdown">
              <li><a href="#">Healthcare</a></li>
              <li><a href="#">Cosmetic</a></li>
              <li><a href="#">Misc.</a></li>
            </ul>
          </li>
          <li><a href="#">About Us</a></li>
          <li><a href="#">Register</a></li>
        </ul>
        <button type="button"><a href="#"></a>Login</button>
      </div>
    </div>
  </header>
</body>

</html>

解决方案

你看到的下拉菜单里的底部边框,并不是<a>标签的边框,而是父元素<li>的::after伪元素。当前CSS中.navbar ul li::after会作用于所有层级的<li>,包括下拉菜单里的子项。

需要修改选择器,让这个伪元素只作用于顶级导航栏的<li>,替换以下两段CSS代码:

/* 替换前 */
.navbar ul li::after {
  content: '';
  height: 3px;
  width: 0;
  background: teal;
  position: absolute;
  left: 0;
  bottom: 0px;
  transition: 0.5s;
}

.navbar ul li:hover::after {
  width: 100%;
}
/* 替换后 */
.navbar > ul > li::after {
  content: '';
  height: 3px;
  width: 0;
  background: teal;
  position: absolute;
  left: 0;
  bottom: 0px;
  transition: 0.5s;
}

.navbar > ul > li:hover::after {
  width: 100%;
}

通过>子选择器限定作用范围,只会给顶级导航的<li>添加hover时的底部线条,下拉菜单里的子项不会再出现多余边框。

修改后的完整CSS代码

* {
  margin: 0;
  padding: 0;
  background-color: white;
}

body {
  font-family: Arial, Helvetica, sans-serif;
}

header {
  background-color: rgb(255, 255, 255);
  height: 600px;
}

.navbar {
  width: 85%;
  margin: auto;
  padding: 60px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  width: 160px;
  cursor: pointer;
  margin-right: auto;
}

.navbar .dropdown {
  display: none;
  box-shadow: 0px 0px 2px #000;
  border-radius: 5px;
  left: -30%;
}

.navbar ul li:hover .dropdown {
  display: block;
}

.navbar>ul>li {
  position: relative;
}

.navbar>ul>li>.dropdown {
  position: absolute;
  top: 1.4em;
}

.navbar .dropdown li {
  display: block;
  margin: 15px;
  text-align: center;
}

.navbar ul li {
  list-style: none;
  display: inline-block;
  margin: 0 20px;
  position: relative;
}

.navbar ul li a {
  text-decoration: none;
  color: black;
  border-bottom: 0px;
}

.navbar ul li a:hover {
  color: teal;
}

.navbar > ul > li::after {
  content: '';
  height: 3px;
  width: 0;
  background: teal;
  position: absolute;
  left: 0;
  bottom: 0px;
  transition: 0.5s;
}

.navbar > ul > li:hover::after {
  width: 100%;
}

button {
  list-style: none;
  border: none;
  background: teal;
  padding: 10px 20px;
  border-radius: 30px;
  color: white;
  font-size: 15px;
  transition: 0.4s;
}

内容的提问来源于stack exchange,提问作者Juju

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 13:10:31