I can't understand why my bootstrap custom CSS is being overridden

rustinpeace91

I've been learning CSS for a few months and I'm trying to build a practice website using bootstrap as well. I've selected a nav-bar and want to code page links that will change color based on which page is currently open. I'm doing this by making a class for each link called "activePage" and coding a CSS selector for that class in my custom styles.css

                <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav">
                    <li><a class="activePage" href="index.html">News</a></li>
                    <li><a href="tour-dates.html">Tour Dates</a></li>
                    <li><a href="about.html">About</a></li>
                    <li><a href="discography.html">Discography</a></li>
                    <li><a href="other-media.html">Other Media</a></li>
                </ul>
                <div align = "right" class="social">
                    <ul>
                        <li><a href="#"><i class="fa fa-lg fa-facebook"></i></a></li>
                        <li><a href="#"><i class="fa fa-lg fa-github"></i></a></li>
                        <li><a href="#"><i class="fa fa-lg fa-instagram"></i></a></li>
                        <li><a href="#"><i class="fa fa-lg fa-tumblr"></i></a></li>
                        <li><a href="#"><i class="fa fa-lg fa-youtube"></i></a></li>
                    </ul>


                </div>

            </div><!-- /.navbar-collapse -->

As you can see for HTML page I have a class on the nabber for the active page called "activePage"

For now i have

a.activePage {
color:blue;
}

as the CSS rule, but nothing shows up. Here's the odd thing When I hit inspect element to see what CSS rules are being applied to links I get this.

http://imgur.com/85REHsw

It says that a.activePage is the most specific selector, but for some reason the color of the text is being determined by .navbar-inverse .navbar-nav>li>a in bootstrap.css. Why is this happening. If my CSS knowledge is correct (and it may very well not be) those selectors should only effect things directly inside the div, things inside a custom class I created should override those selectors. Why is it not happening?

Btw I've tried moving the CSS selector all throughout the stylesheet, I've tried specifying li.activePage instead. I've tried putting it in bootstrap.css (it's not the stylesheet itself, other rules have been applied correctly) It seems that there is something I'm not understanding here.

Rion Williams

Considering Specificity

This is occurring due to CSS Specificify Rules, which govern how styles target certain elements and how decisions are made regarding conflicts should be resolved between them.

The Bootstrap style that is being applied to your element more specifically describes the element it is targeting as you can see by the multiple nesting :

.navbar-inverse .navbar-nav li a {
    color: #9d9d9d;
}

This is much more specific than the broad style that you are currently using :

a.activePage {
    color: blue;
}

Possible Approaches for Overriding

There are a few ways to ensure that your style properly overrides the existing Bootstrap one :

  • Ensure that your style is defined after your Bootstrap reference (this is very important if not using the !important option below)
  • Make your style more specific (e.g. .navbar-inverse .navbar-nav li a.activePage)
  • Consider using an !important modifier to your style to give your style precedence.

You can work around this quite easily to ensure that you want your style to be applied by using the !important modifier :

/* Example using a more specific style */
.navbar-inverse .navbar-nav li a.activePage {
   color: blue;
}

/* Example using the !important modifier */
a.activePage {
    color: blue!important;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Bootstrap CSS isn't being overridden by Custom css

From Dev

custom css being overridden by bootstrap css

From Dev

I can't edit my custom.css in a bootstrap project

From Dev

I can't understand why files are not being written to a mounted partition

From Dev

I can't understand why I'm not able to override a Bootstrap style with my own style sheet in this particular case

From Dev

I can't understand why my waveform is coming out this way

From Dev

I can't understand why CSS Transition is not working on keydown

From Dev

Why isn't my bootstrap Modal being destroyed on hide? How can I get Multiple remote Modals to work?

From Dev

Why isn't my bootstrap Modal being destroyed on hide? How can I get Multiple remote Modals to work?

From Dev

Why isn't my custom 404 page being redirected to in the case of 404 errors on a ColdFusion site on IIS 7.5? How can I fix it?

From Dev

Why my image/video metadata is being deleted? How can i save custom information in my files?

From Dev

Why my image/video metadata is being deleted? How can i save custom information in my files?

From Dev

Can't understand why my || is not working

From Dev

Can't understand why my || is not working

From Dev

I can't understand why my express route is encountering 'can't set headers after they are sent'

From Dev

Why can't I change the custom class of my UITableView?

From Dev

Why is my javascript function not being invoked for simple MVC tutorial? Why can't I debug either?

From Dev

Why is my javascript function not being invoked for simple MVC tutorial? Why can't I debug either?

From Dev

undefined index but i can't understand why

From Dev

Why can't I hide my 'ul' with CSS like this?

From Dev

Why can't I load js and css files on my pages?

From Dev

Why can't I load js and css files on my pages?

From Dev

My data is being written to Firebase database but i can't read it why?

From Dev

CSS rule not being overridden

From Dev

I can't understand why can't Haskell deduce this type

From Dev

Why can methods be overridden but variables can't?

From Dev

Why can't I add top margin to my <a> tag when I am using bootstrap?

From Dev

i dont understand why my "if" command don't work as it should

From Dev

How can I add my custom style for my bootstrap navbar?

Related Related

  1. 1

    Bootstrap CSS isn't being overridden by Custom css

  2. 2

    custom css being overridden by bootstrap css

  3. 3

    I can't edit my custom.css in a bootstrap project

  4. 4

    I can't understand why files are not being written to a mounted partition

  5. 5

    I can't understand why I'm not able to override a Bootstrap style with my own style sheet in this particular case

  6. 6

    I can't understand why my waveform is coming out this way

  7. 7

    I can't understand why CSS Transition is not working on keydown

  8. 8

    Why isn't my bootstrap Modal being destroyed on hide? How can I get Multiple remote Modals to work?

  9. 9

    Why isn't my bootstrap Modal being destroyed on hide? How can I get Multiple remote Modals to work?

  10. 10

    Why isn't my custom 404 page being redirected to in the case of 404 errors on a ColdFusion site on IIS 7.5? How can I fix it?

  11. 11

    Why my image/video metadata is being deleted? How can i save custom information in my files?

  12. 12

    Why my image/video metadata is being deleted? How can i save custom information in my files?

  13. 13

    Can't understand why my || is not working

  14. 14

    Can't understand why my || is not working

  15. 15

    I can't understand why my express route is encountering 'can't set headers after they are sent'

  16. 16

    Why can't I change the custom class of my UITableView?

  17. 17

    Why is my javascript function not being invoked for simple MVC tutorial? Why can't I debug either?

  18. 18

    Why is my javascript function not being invoked for simple MVC tutorial? Why can't I debug either?

  19. 19

    undefined index but i can't understand why

  20. 20

    Why can't I hide my 'ul' with CSS like this?

  21. 21

    Why can't I load js and css files on my pages?

  22. 22

    Why can't I load js and css files on my pages?

  23. 23

    My data is being written to Firebase database but i can't read it why?

  24. 24

    CSS rule not being overridden

  25. 25

    I can't understand why can't Haskell deduce this type

  26. 26

    Why can methods be overridden but variables can't?

  27. 27

    Why can't I add top margin to my <a> tag when I am using bootstrap?

  28. 28

    i dont understand why my "if" command don't work as it should

  29. 29

    How can I add my custom style for my bootstrap navbar?

HotTag

Archive