悬停时更改引导导航栏链接的颜色

时间:2019-06-25 04:55:06

标签: html css bootstrap-4

我有一个Bootstrap导航栏,我想在悬停时更改导航栏中链接的颜色。我已经尝试过CSS,但无法正常工作。 我不想更改代码,但是我想在相同的标记类中添加效果。某些人在导航栏中使用列表,我不想这样做。

我尝试为每个标签中定义的类的悬停添加相同的CSS。

<!doctype html>
        <html>
        <head>
        <title>Contact Us</title>
            <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
            <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
            <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
            <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
            <link href="contact.css" type="text/css">
        </head>
        
        <body>
        <div class="container-fullwidth">
              <nav class="navbar navbar-expand-lg navbar-dark bg-success fixed-top">
                <h1 class="navbar-brand"><strong>Digitian Hub</strong></h1>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#NewMenu" aria-expanded="false" aria-controls="NewMenu" aria-label="toggleNavigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                    <div class="collapse navbar-collapse" id="NewMenu">
                        <div class="navbar-nav">
                            <a class="nav-item nav-link" href="Rishabh.html"><strong>Home</strong></a>
                            <a class="nav-item nav-link services-link" href="#"><strong>Services</strong></a>
                            <a class="nav-item active nav-link contact-link" href="Contact Us.html"><strong>Contact Us</strong></a>
                        </div>
                    </div>
                </nav>
            </div>
        </body>
        </html> 

当我将鼠标悬停在服务上时,我希望它具有绿色。

6 个答案:

答案 0 :(得分:0)

CSS 下添加,用于将悬停添加到导航中,并在 bootstrap CSS 后添加到标题中

.navbar-dark .navbar-nav .nav-link.services-link:hover {
  color: rgba(0,255,0,1);
}

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<link href="contact.css" type="text/css">
<style type="text/css">
  .navbar-dark .navbar-nav .nav-link.services-link:hover {
    color: rgba(0,255,0,1);
  }
</style>
        
        <div class="container-fullwidth">
          <nav class="navbar navbar-expand-lg navbar-dark bg-success fixed-top">
            <h1 class="navbar-brand"><strong>Digitian Hub</strong></h1>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#NewMenu" aria-expanded="false" aria-controls="NewMenu" aria-label="toggleNavigation">
                <span class="navbar-toggler-icon"></span>
            </button>
                <div class="collapse navbar-collapse" id="NewMenu">
                    <div class="navbar-nav">
                        <a class="nav-item nav-link" href="Rishabh.html"><strong>Home</strong></a>
                        <a class="nav-item nav-link services-link" href="#"><strong>Services</strong></a>
                        <a class="nav-item active nav-link contact-link" href="Contact Us.html"><strong>Contact Us</strong></a>
                    </div>
                </div>
            </nav>
        </div>

答案 1 :(得分:0)

使用hover进行服务链接会起作用

.services-link:hover{
color:#00ff00 !important;
}
<html>
    <head>
    <title>Contact Us</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
        <link href="contact.css" type="text/css">
    </head>

    <body>
    <div class="container-fullwidth">
          <nav class="navbar navbar-expand-lg navbar-dark bg-success fixed-top">
            <h1 class="navbar-brand"><strong>Digitian Hub</strong></h1>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#NewMenu" aria-expanded="false" aria-controls="NewMenu" aria-label="toggleNavigation">
                <span class="navbar-toggler-icon"></span>
            </button>
                <div class="collapse navbar-collapse" id="NewMenu">
                    <div class="navbar-nav">
                        <a class="nav-item nav-link" href="Rishabh.html"><strong>Home</strong></a>
                        <a class="nav-item nav-link services-link" href="#"><strong>Services</strong></a>
                        <a class="nav-item active nav-link contact-link" href="Contact Us.html"><strong>Contact Us</strong></a>
                    </div>
                </div>
            </nav>
        </div>
    </body>
    </html>

答案 2 :(得分:0)

如果要在服务中专门使用它,请在服务中添加一个ID <a class="nav-item nav-link services-link" id="services-link" href="#"><strong>Services</strong></a>

然后在您的css文件中添加此

#services-link:hover {
color: green;
}

答案 3 :(得分:0)

.services-link strong:hover {
  color: green;
}
<!doctype html>
<html>

<head>
  <title>Contact Us</title>
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
  <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
  <link href="contact.css" type="text/css">
</head>

<body>
  <div class="container-fullwidth">
    <nav class="navbar navbar-expand-lg navbar-dark bg-success fixed-top">
      <h1 class="navbar-brand"><strong>Digitian Hub</strong></h1>
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#NewMenu" aria-expanded="false" aria-controls="NewMenu" aria-label="toggleNavigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
      <div class="collapse navbar-collapse" id="NewMenu">
        <div class="navbar-nav">
          <a class="nav-item nav-link" href="Rishabh.html"><strong>Home</strong></a>
          <a class="nav-item nav-link services-link" href="#"><strong>Services</strong></a>
          <a class="nav-item active nav-link contact-link" href="Contact Us.html"><strong>Contact Us</strong></a>
        </div>
      </div>
    </nav>
  </div>
</body>

</html>

答案 4 :(得分:0)

  

这是您的解决方案

.navbar-nav .services-link:hover{
color: #7FFF00 !important; /* your color */
}
<html>
    <head>
    <title>Contact Us</title>
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
        <link href="contact.css" type="text/css">
    </head>

    <body>
    <div class="container-fullwidth">
          <nav class="navbar navbar-expand-lg navbar-dark bg-success fixed-top">
            <h1 class="navbar-brand"><strong>Digitian Hub</strong></h1>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#NewMenu" aria-expanded="false" aria-controls="NewMenu" aria-label="toggleNavigation">
                <span class="navbar-toggler-icon"></span>
            </button>
                <div class="collapse navbar-collapse" id="NewMenu">
                    <div class="navbar-nav">
                        <a class="nav-item nav-link" href="Rishabh.html"><strong>Home</strong></a>
                        <a class="nav-item nav-link services-link" href="#"><strong>Services</strong></a>
                        <a class="nav-item active nav-link contact-link" href="Contact Us.html"><strong>Contact Us</strong></a>
                    </div>
                </div>
            </nav>
        </div>
    </body>
    </html>

答案 5 :(得分:0)

<!doctype html>
<html>

<head>

    <title>Contact Us</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
        crossorigin="anonymous">
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
        crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
        crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
        crossorigin="anonymous"></script>
    <link href="contact.css" type="text/css">

    <style>
        .nav-item:hover {
            color: red !important;
            background: yellow;
        }
    </style>
</head>

<body>
    <div class="container-fullwidth">
        <nav class="navbar navbar-expand-lg navbar-dark bg-success fixed-top">
            <h1 class="navbar-brand">
                <strong>Digitian Hub</strong>
            </h1>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#NewMenu" aria-expanded="false" aria-controls="NewMenu"
                aria-label="toggleNavigation">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="NewMenu">
                <div class="navbar-nav">
                    <a class="nav-item nav-link" href="Rishabh.html">
                        <strong>Home</strong>
                    </a>
                    <a class="nav-item nav-link services-link" href="#">
                        <strong>Services</strong>
                    </a>
                    <a class="nav-item active nav-link contact-link" href="Contact Us.html">
                        <strong>Contact Us</strong>
                    </a>
                </div>
            </div>
        </nav>
    </div>
</body>

</html>