Replacing existing CSS with styles from the CSS library (Bootstrap)

One major challenge I face when using CSS libraries is their tendency to overwrite my custom styles. For instance, I have a list within a Bootstrap expand/collapse menu like this:

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

To set my own font color, I apply the following CSS:

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

However, upon inspecting in Firefox's Inspector, I notice that Bootstrap is overriding the color I've chosen.

It's worth noting that my custom CSS comes after loading the Bootstrap files in the HTML document head section.

Is there a way to prevent this without having to individually add style="color: #004687" to each element?

UPDATE: Despite the suggestions provided so far, none have yielded any success. To offer more context, I'm sharing the complete original code below:

<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>

The inclusion of my CSS looks 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>

UPDATE 3: After some experimentation, I discovered a workaround by utilizing IDs instead of classes, as ID selectors take precedence over class selectors due to CSS specificity. While effective, this approach isn't ideal, and I still seek insights into why my attempts to override Bootstrap classes with my own have been unsuccessful.

Answer №1

Add a new class to the existing 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>

Include this in your custom CSS:

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

Answer №2

Ensure that your custom CSS file is positioned below the bootstrap.css file for proper styling.

Alternatively, you can use the following CSS code:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Choose an image and save the selection information for the following page (Tarot card)

I'm in the process of creating a website that showcases multiple tarot cards. The goal is for users to select the cards they're interested in and have their chosen card displayed on the next page. I've implemented some code for selecting the ...

Create an animated hamburger menu using Tailwind CSS and Angular framework

Within my Angular project, I have successfully integrated Tailwind UI to handle the opening and closing of the hamburger menu by utilizing the code below: <header> <div class="-mr-2 -my-2 md:hidden"> <button type="button ...

"Utilize jQuery to create a new row when an image button is clicked

I am looking for a way to duplicate the existing <tr> every time I click on the + button, represented by <i class="fa fa-plus" aria-hidden="true"></i> Here is the structure of my <tr>. How can I achieve this using jQuery or JavaScr ...

There are no elements displayed beneath the div

If you're short on time, let me explain my point with an image: Here is the HTML code I am working with: <div align="center"> <div class="img-container"> <div class="myconatiner"> <h2>Headline< ...

In order to ensure proper coverage for phones, tablets, and standard screens across various widths, it is essential to implement the

As I delve into my project focused on responsive design and media queries, I have been studying resources like Aaron Gustavson's book, Adaptive Web Design, and Ben Frain's Responsive Web Design with HTML5 and CSS3, along with various online sourc ...

Is there a way to eliminate the white border on a pie chart in Chart.js V2.6.0?

https://i.sstatic.net/YIFQx.png Currently, I am working with a pie chart using Chart.js (version 2.6.0), and I am looking to remove the white lines that appear between the slices. Can anyone provide me with a solution for achieving this? I have already a ...

Looping through jQuery click() function

I used a loop to generate div elements and I am trying to modify the background-color when the div is clicked. var c = $("#container")[0]; for(var i=0; i<6; i++){ var x = document.createElement("div"); x.className = "sqare"; x.click(changecolor(t ...

Endless rotation with stunning magnification feature

My goal is to create a responsive carousel with auto-play infinite loop functionality, where the center item always occupies 70% of the viewport width. Currently, I have achieved a similar result using the Slick library: https://codepen.io/anon/pen/pBvQB ...

unable to reset contact form functionality

I need to implement a Contact Us form on my website. The form should clear the text fields after submission and display a Thank You message. Below is the HTML code: <div class="form"> <div id="sendmessage">Your message has been sent. Thank yo ...

Ways to output HTML from a function

I'm struggling to get my HTML function to work properly, even though it seems like a simple task. For some reason, the content isn't displaying when I return it, but things work fine when using console.log. Could someone please assist me with th ...

Is there a way to dismiss a modal window from the parent?

How can I close a modal window from its parent? This task seems quite challenging to accomplish. Essentially, I am opening a non-modal window. In some cases, the user may open a modal window from this non-modal window. If I close the non-modal window, I al ...

Transforming the input button into images

I'm new to JavaScript and I'm looking to change the show button and hide button to images instead. The show button image should be different from the hide button image. Can anyone guide me on how to achieve this? Click here for reference $( ...

Bootstrap navbar does not appear on smaller devices due to a display issue

This particular navigation bar, featuring a logo image in the center, functions well on larger screens. However, when viewed on smaller screens, everything disappears, including the logo image, and the hamburger menu icon fails to display. Is there a solut ...

What can be done to avoid a form being reset when it returns no results?

I've recently created a mysql and php search page, which is working well. However, I noticed that when the form is submitted and there are no results returned, the <option> tags get reset. Is there a way to prevent this from happening? META: I ...

Using HTML and CSS to create a line break between div elements

Currently, I am working on a page that allows users to post comments. However, I have encountered an issue with the div element that contains the posted message. When the message is too long and lacks line breaks, a horizontal scrollbar appears. I would li ...

Encountering obstacles as a novice trying to master CSS styling techniques

I'm having trouble aligning buttons at the center top of the screen. Despite my efforts, I can't seem to get them perfectly aligned https://i.stack.imgur.com/j7Bea.png button { width: 13%; height: 50px; display: inline-block; fo ...

Ways to execute several actions within an HTML form

If I wanted to implement a double action in an HTML form, how would I go about it? The first action should direct to examle1.php And the second action should go to example2.php Currently, my code looks like this: <form name="input" action="" method= ...

"Enhancing user experience with jQuery hover effects on table

This is an example of the HTML structure: <tr class="row-1 row-first"> <td class="col-1 col-first"> <div class="views-field views-field-field-logo-image"></div> <div class="views-field views-field-field-logo-title"> ...

The element is not occupying the full width within the div container

I'm working with a div that contains some elements. My goal is to have these elements span the entire width of the container div. .container { height: 100px; width: 100px; display: flex; flex-direction: column; overflow: scroll; borde ...

Display the collapsible panel and modify the class of the other panel

I am trying to implement a collapse panel with a button and adjust the class of another panel so that it fits within the width of the page. Here is the code snippet I have been working on: button // first attempt $('#signup').on('click&a ...