Bootstrap 4 Navbar Hover BG Color

时间:2018-03-18 22:38:27

标签: css twitter-bootstrap

我的导航栏出现问题。在非移动版本(或浏览器扩展时),我希望悬停在链接上的背景颜色填充导航栏的高度,而不是仅部分着色。

请告诉我如何在每个链接的悬停上获得完整的bg颜色。谢谢!



a,
a:visited,
a:hover,
a:active {
  color: inherit;
}


/* Navigation Bar */

.navbar {
  background-color: #303030;
}

.navbar-nav a {
  color: #ffffff;
  text-align: center;
}

.navbar .navbar-nav>a:hover,
.navbar .navbar-nav>a:focus {
  color: #ffffff;
  background-color: yellow;
}


/*Mobile menu icon color*/

.navbar .navbar-toggler {
  border-color: rgb(255, 255, 255);
}

.navbar .navbar-toggler-icon {
  background-image: url("data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='rgba(255,255,255, 0.7)' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 8h24M4 16h24M4 24h24'/%3E%3C/svg%3E");
}


/* Branding Dimension for Mobile */

.navbar-brand img {
  width: 125px;
  height: 29px;
}

@media (min-width: 576px) {
  .navbar-brand img {
    width: 195px;
    height: 45px;
  }
}

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

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <link rel="stylesheet" href="assets\css\style.css">

  <title>Street Scents</title>
</head>

<body>
  <nav class="navbar navbar-expand-lg">
    <a class="navbar-brand" href="#">
      <img src="assets/img/logo-text.png" alt="">
    </a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
      </button>
    <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
      <div class="navbar-nav ml-auto">
        <a class="nav-item nav-link active" href="#">Home <span class="sr-only">(current)</span></a>
        <a class="nav-item nav-link" href="#">About</a>
        <a class="nav-item nav-link" href="#">Videos</a>
        <a class="nav-item nav-link" href="#">Blog</a>
        <a class="nav-item nav-link" href="#">Merch</a>
      </div>
    </div>
  </nav>



  <!-- Optional JavaScript -->
  <!-- Scripts -->
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>

</html>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:0)

您需要从导航栏中删除填充,因此您可以尝试以下内容:

@media (min-width:992px) {
  .navbar {
    padding: 0!important;
  }
  .navbar-nav a {
    padding: 10px 0; /* add some padding to link to keep the same height*/
  }
}

以下是完整代码:

a,
a:visited,
a:hover,
a:active {
  color: inherit;
}


/* Navigation Bar */

.navbar {
  background-color: #303030;
}

.navbar-nav a {
  color: #ffffff;
  text-align: center;
}

.navbar .navbar-nav>a:hover,
.navbar .navbar-nav>a:focus {
  color: #ffffff;
  background-color: yellow;
}


/*Mobile menu icon color*/

.navbar .navbar-toggler {
  border-color: rgb(255, 255, 255);
}

.navbar .navbar-toggler-icon {
  background-image: url("data:image/svg+xml;charset=utf8,%3Csvg viewBox='0 0 32 32' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath stroke='rgba(255,255,255, 0.7)' stroke-width='2' stroke-linecap='round' stroke-miterlimit='10' d='M4 8h24M4 16h24M4 24h24'/%3E%3C/svg%3E");
}


/* Branding Dimension for Mobile */

.navbar-brand img {
  width: 125px;
  height: 29px;
}

@media (min-width: 576px) {
  .navbar-brand img {
    width: 195px;
    height: 45px;
  }
}

@media (min-width:992px) {
  .navbar {
    padding: 0!important;
  }
  .navbar-nav a {
    padding: 10px 0;
  }
}
<!doctype html>
<html lang="en">

<head>
  <!-- Required meta tags -->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <!-- CSS -->
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <link rel="stylesheet" href="assets\css\style.css">

  <title>Street Scents</title>
</head>

<body>
  <nav class="navbar navbar-expand-lg">
    <a class="navbar-brand" href="#">
      <img src="assets/img/logo-text.png" alt="">
    </a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
      </button>
    <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
      <div class="navbar-nav ml-auto">
        <a class="nav-item nav-link active" href="#">Home <span class="sr-only">(current)</span></a>
        <a class="nav-item nav-link" href="#">About</a>
        <a class="nav-item nav-link" href="#">Videos</a>
        <a class="nav-item nav-link" href="#">Blog</a>
        <a class="nav-item nav-link" href="#">Merch</a>
      </div>
    </div>
  </nav>



  <!-- Optional JavaScript -->
  <!-- Scripts -->
  <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>

</html>

答案 1 :(得分:0)

只需将py-0 Bootstrap类添加到Navbar即可删除顶部/底部填充,以便您的自定义悬停颜色填充高度。要保持原始高度,请在nav-link s。

中添加上/下填充

https://www.codeply.com/go/4ZBnt0gWFc

<nav class="navbar navbar-expand-lg py-0">
    <a class="navbar-brand" href="#">
      <img src="assets/img/logo-text.png" alt="">
    </a>
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
      </button>
    <div class="collapse navbar-collapse" id="navbarNavAltMarkup">
      <div class="navbar-nav ml-auto">
        <a class="nav-item nav-link active" href="#">Home <span class="sr-only">(current)</span></a>
        <a class="nav-item nav-link" href="#">About</a>
        <a class="nav-item nav-link" href="#">Videos</a>
        <a class="nav-item nav-link" href="#">Blog</a>
        <a class="nav-item nav-link" href="#">Merch</a>
      </div>
    </div>
  </nav>

用于更改链接或悬停颜色的CSS:

/* change the link color */
.navbar-nav .navbar-nav .nav-link {
    color: rgba(255,255,255,.5);
}

/* change the color of active or hovered links */
.navbar-nav .nav-item.active .nav-link,
.navbar-nav .nav-item:hover .nav-link {
    color: #ffffff;
}