2

This great answer mentioned how giving an ID is a good way to override existing class elements when you still want some styling from said class. For example, below I have the following:

<ul class="nav navbar-nav navbar-right" id="ft-nav-text">
     <li><a href="#">Home</a></li>
     <li><a href="#">About</a></li>
</ul>

I want to change some of the properties of the text, like color.

#ft-nav-text {
    font-size: 18px;
    font-family: "Myriad Pro";
    color: blue;
}

Yet the text will not change to blue unless I delete the navbar-nav class, which I still want some styling from. I don't want to hack away at the main Bootstrap file.

Community
  • 1
  • 1
8protons
  • 3,591
  • 5
  • 32
  • 67
  • Try use !important. If you want to override this class set this property to .nav.navbar-nav.navbar-right – Matteo1010 Jul 04 '16 at 05:20
  • Only the color will not change. The font-size changes, the font-family changes, but color will not change – 8protons Jul 04 '16 at 05:22

2 Answers2

5

You aren't targeting the elements you want to change, use a more specific selector.

Working Example:

#ft-nav-text > li > a {
  font-size: 18px;
  font-family: "Myriad Pro";
  color: blue;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-default">
  <div class="container-fluid">

    <div class="navbar-header">
      <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#">Brand</a>
    </div>

    <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
      <ul class="nav navbar-nav navbar-right" id="ft-nav-text">
        <li><a href="#">Home</a>
        </li>
        <li><a href="#">About</a>
        </li>
      </ul>
    </div>

  </div>
</nav>
vanburen
  • 21,502
  • 7
  • 28
  • 41
  • as a note, my code did work when I took out navbar-nav; it'd change the text to blue. The font-size and font-family would work regardless. Any idea why? – 8protons Jul 04 '16 at 05:23
  • 3
    @8protons: CSS has the concept of *specificity* of rules. If an element's style is governed by multiple rules, the most *specific* rule wins. (Rules of equal specificity are applied according to which is later in the CSS.) So for example, `a { }` is quite general; `.navbar-nav a { }` is more specific. So if they both specify a color, the latter will win for `a` elements within `.navbar-nav` elements. Another example: `.navbar-nav.nav a { }` is more specific than `.navbar-nav a { }`. – T.J. Crowder Jul 04 '16 at 05:27
  • 1
    If you look at the [bootstrap.css](https://github.com/twbs/bootstrap/blob/master/dist/css/bootstrap.css) file around line #4513 you can check out the rules that are being applied. – vanburen Jul 04 '16 at 05:27
1

Bootstrap define some styles on .navbar-nav > li > a in bootstrap i.e it is not inheriting color styles from parent. So defining id on any of its parent won't work.

You need to use #fg-nav-text a to override styles.

a {
  color: black;
}

#ft-nav-text {
  font-size: 18px;
  font-family: "Myriad Pro";
  color: blue;
}
<ul class="nav navbar-nav navbar-right" id="ft-nav-text">
     <li><a href="#">Home</a></li>
     <li><a href="#">About</a></li>
</ul>
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95