8protons 8protons - 5 months ago 9
CSS Question

Why won't my ID override the Bootstrap class?

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.

Answer

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>

Comments