简体   繁体   English

更改导航栏文本颜色

[英]Changing navbar text color

I'm working with Bootstrap navbar and I want to change colour of one of the links.我正在使用 Bootstrap 导航栏,我想更改其中一个链接的颜色。 I can easily change colour of all links with the code below, but i need to change just one.我可以使用下面的代码轻松更改所有链接的颜色,但我只需要更改一个。

.navbar-custom .navbar-nav .nav-link {
color: red;
}

and HTML和 HTML

<nav class="navbar navbar-expand-md navbar-custom fixed-top navbar-light bg-light">
    <div class="container-fluid">
        <ul class="navbar-nav mb-2 mb-lg-0">
          <li class="nav-item">
            <a class="nav-link active" aria-current="page" href="#">Home</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="/about">About</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="/login">Login</a>
          </li>
       </ul>
    </div>
  </nav>

Let's say I want to change colour of the Login link to red.假设我想将登录链接的颜色更改为红色。 How to do this without changing About link colour at the same time and what's the rules of changing style of one element instead of all in nested elements like lists in navbar?如何在不同时更改关于链接颜色的情况下执行此操作以及更改一个元素的样式而不是嵌套元素(如导航栏中的列表)中的所有样式的规则是什么?

One option would be to use an Attribute Selector:一种选择是使用属性选择器:

 a[href="/login"] { color: red; }
 <nav class="navbar navbar-expand-md navbar-custom fixed-top navbar-light bg-light"> <div class="container-fluid"> <ul class="navbar-nav mb-2 mb-lg-0"> <li class="nav-item"> <a class="nav-link active" aria-current="page" href="#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="/about">About</a> </li> <li class="nav-item"> <a class="nav-link" href="/login">Login</a> </li> </ul> </div> </nav>

Simply add text-danger class to your required tag to change color.只需将text-danger class 添加到您所需的标签即可更改颜色。

Bootstrap provides some different theme colors through classes. Bootstrap 通过类提供了一些不同的主题 colors。 Go through the document here Go 通过这里的文档

You required for login .您需要login Hence added text-danger to that respected <a href='/login'>Login</a> tag.因此,为受人尊敬的<a href='/login'>Login</a>标签添加了text-danger

<nav class="navbar navbar-expand-md navbar-custom fixed-top navbar-light bg-light">
    <div class="container-fluid">
        <ul class="navbar-nav mb-2 mb-lg-0">
          <li class="nav-item">
            <a class="nav-link active" aria-current="page" href="#">Home</a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="/about">About</a>
          </li>
          <li class="nav-item">
            <a class="nav-link text-danger" href="/login">Login</a>
          </li>
       </ul>
    </div>
  </nav>

Using :n-th-of-type()使用:n-th-of-type()

If you have similar tags as present here and want to change only selected ones then you can use this method.如果您有与此处类似的标签并且只想更改选定的标签,那么您可以使用此方法。 You can see n-th-of-type() , nth-child() , nth-last-of-type()你可以看到n-th-of-type() , nth-child() , nth-last-of-type()

li:nth-of-type(3) a{  // It will select third li a element
  color: red
}

li:nth-of-type(2n) a{  // It will select even li a element like 2nd, 4th, ..
  color: red
}

li:nth-of-type(2n+1) a{  // It will select odd li a element like 1st, 3rd, ..
  color: red
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM