Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwriting CSS from CSS library (Bootstrap)

My main issue with using CSS libraries is that they often overwrite custom CSS styles that I specifically set. For example, here I have a list that exists within a Bootstrap expand/collapse menu:

<ul class="nav navbar-nav">
   <li>
      <a href="#" style=""><img src="images/arrow.gif" style="width: 20px;">Link A</a>
   </li>
</ul>

I want to set my own font color, so I use the following CSS:

nav li {
  font-size: 16px;
  color: #004687;
}

But when I view in Firefox's Inspector, I see the color I have chosen is being overridden by Bootstrap.

My custom CSS occurs after the Bootstrap files have been loaded in the of the HTML document.

How do I prevent this happening without setting style="color: #004687" to every element?

EDIT: Thanks for the suggestions thus far, but none have been successful. I'm pasting the full original code to give you greater detail:

<div class="container">
    <header class="navbar navbar-static-top bs-docs-nav">
      <div class="col-md-4 visible-xs" id="mobile-nav-outer">
        <div class="navbar-header">
            <button class="navbar-toggle" type="button" data-toggle="collapse" data-target=".bs-navbar-collapse">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
          <a href="../" class="navbar-brand">Menu</a>
        </div>
        <nav class="collapse navbar-collapse bs-navbar-collapse" id="mobile-nav-inner" role="navigation">
          <ul class="nav navbar-nav">
            <li>
              <a href="#" class="nav-mobile-link"><img src="images/arrow.gif" style="width: 20px;">Link A</a>
            </li>
          </ul>

        </nav>
      </div>
  </header>
</div>

My CSS is included like this:

  <head>
    <link href="bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="bootstrap/css/docs.min.css" rel="stylesheet">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="bootstrap/js/bootstrap.min.js"></script>

    <style type="text/css">
        #mobile-nav-outer {
          border: 1px solid #CCC;
          background-color: #FFF;
        }
        #mobile-nav-inner {
          border: 1px solid #CCC;
          background-color: #FFF;
          margin: 3px;
        }
        nav li {
            font-size: 16px;
            color: #004687;
        }
        .nav-mobile-link {
            font-size: 16px;
            color: #004687;        
        }
    </style>
  </head>

EDIT 3: I have found a cheap workaround by using ID's rather than classes which works due to CSS specificity (ID's override classes). This isn't a very clean solution and I'd still like to know why I cannot override the Bootstrap classes with my own.

like image 884
John 'Mark' Smith Avatar asked Nov 01 '22 22:11

John 'Mark' Smith


1 Answers

Overwrite it with additional class added to the list:

<ul class="nav navbar-nav custom-class">
    <li>
        <a href="#" style=""><img src="images/arrow.gif" style="width: 20px;">Link A</a>
    </li>
</ul>

and in your custom styles:

.nav.custom-class li {
    font-size: 16px;
    color: #004687;
}
like image 108
alla.lipina Avatar answered Nov 12 '22 18:11

alla.lipina