htmlcss

What is the order of precedence for CSS?


I'm trying to figure out why one of my CSS classes seems to override the other (and not the other way around)?

Here I have two CSS classes:

.smallbox { 
    background-color: white;
    height: 75px;
    width: 150px;
    font-size:20px;
    box-shadow: 0 0 10px #ccc;
    font-family: inherit;
}

.smallbox-paysummary {
    @extend .smallbox; 
    font-size:10px;
}

and in my view I call:

<pre class = "span12 pre-scrollable smallbox-paysummary smallbox "> 

The font (the overlapping element) shows up as 10px instead of 20px - could someone explain why this is the case?


Solution

  • There are several rules ( applied in this order ) :

    1. inline css ( html style attribute ) overrides css rules in style tag and css file
    2. a more specific selector takes precedence over a less specific one
    3. rules that appear later in the code override earlier rules if both have the same specificity.
    4. A css rule with !important always takes precedence.

    In your case its rule 3 that applies.

    Specificity for single selectors from highest to lowest:

    To compare the specificity of two combined selectors, compare the number of occurences of single selectors of each of the specificity groups above.

    Example: compare #nav ul li a:hover to #nav ul li.active a::after

    A good article about css selector specificity.