将导航项的下边框与导航栏对齐

Align nav-item bottom border with navbar

提问人:Caldera500 提问时间:11/16/2023 更新时间:11/16/2023 访问量:22

问:

有没有办法将引导导航项的下边框与导航栏的下边框对齐?我想使用底部边框来显示选择了哪个元素。我能够使用导航栏的默认高度来做到这一点。一旦我增加到底部边框就不再对齐了。height80px

bottom border not aligned

HTML格式:

  <nav class="navbar navbar-expand-lg navbar-light bg-light">
    <a class="navbar-brand" href="#">Brand</a>
    <button
      class="navbar-toggler"
      type="button"
      data-toggle="collapse"
      data-target="#navbarNav"
      aria-controls="navbarNav"
      aria-expanded="false"
      aria-label="Toggle navigation"
    >
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
      <ul class="navbar-nav">
        <li class="nav-item">
          <a class="nav-link active" href="#"
            >Home<span class="sr-only">(current)</span></a
          >
          <div class="narrow-bar"></div>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Features</a>
          <div class="narrow-bar"></div>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Pricing</a>
          <div class="narrow-bar"></div>
        </li>
        <li class="nav-item">
          <a class="nav-link disabled" href="#">Disabled</a>
          <div class="narrow-bar"></div>
        </li>
      </ul>
    </div>
  </nav>

和 CSS:

     <style>
      .navbar {
        height: 80px;
        display: flex;
        align-items: center;
      }
      .navbar-nav {
        display: flex;
        justify-content: space-between;
        align-items: flex-end;
      }

      .navbar-nav .nav-link {
        border-bottom: 2px solid transparent;
        position: relative;
        text-align: center;
        width: 100%;
        height: 3rem;
      }

      .navbar-nav .nav-link.active::after {
        content: "";
        position: absolute;
        bottom: -2px;
        left: 0;
        right: 0;
        height: 2px;
        background-color: #007bff;
      }
    </style>

谢谢!!

HTML CSS twitter-bootstrap 引导-5

评论


答:

1赞 Nick Friesen 11/16/2023 #1

您可以将 .navbar-nav、.nav-link.active::after(蓝色条)的 css 从 -2px 调整为 -17px。(我还添加了,因此您可以在我制作的包含您的代码的代码片段上看到菜单打开)。bottomnavbar-expand-mdnavbar-expand-sm

.navbar {
  height: 80px;
  display: flex;
  align-items: center;
}

.navbar-nav {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
}

.navbar-nav .nav-link {
  border-bottom: 2px solid transparent;
  position: relative;
  text-align: center;
  width: 100%;
  height: 3rem;
}

.navbar-nav .nav-link.active::after {
  content: "";
  position: absolute;
  bottom: -17px; /*adjust this*/
  left: 0;
  right: 0;
  height: 2px;
  background-color: #007bff;
}
<head>
  <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
</head>

<body>
  <nav class="navbar navbar-expand-lg navbar-light bg-light navbar-expand-md navbar-expand-sm">
    <a class="navbar-brand" href="#">Brand</a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="collapse navbar-collapse" id="navbarNav">
      <ul class="navbar-nav">
        <li class="nav-item">
          <a class="nav-link active" href="#">Home<span class="sr-only">(current)</span></a
          >
          <div class="narrow-bar"></div>
        </li>
       <li class="nav-item">
          <a class="nav-link" href="#">Features</a>
          <div class="narrow-bar"></div>
        </li>
        <li class="nav-item">
          <a class="nav-link" href="#">Pricing</a>
          <div class="narrow-bar"></div>
        </li>
        <li class="nav-item">
          <a class="nav-link disabled" href="#">Disabled</a>
          <div class="narrow-bar"></div>
        </li>
      </ul>
    </div>
  </nav>
</body>

评论

1赞 Caldera500 11/16/2023
这很完美。非常感谢尼克!!
0赞 Nick Friesen 11/17/2023
很高兴我能帮上忙!