203

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 20 - could someone explain why this is the case?

6
  • 3
    Are you using a CSS pre-processor? I see you have that @extend .smallbox;, which looks like non-standard cSS. Commented Aug 3, 2014 at 14:41
  • You really shouldn't combine @extend - if it does what I think it does - and using both selectors on the same element.
    – millimoose
    Commented Feb 13, 2016 at 19:18
  • SMACSS seems to recommend to only include the changed properties in a "subclass" (i.e. .smallbox-paysummary), and optionally make the CSS declaration more clearly specific (without relying on the order of appearance in the CSS file) by using .smallbox.smallbox-paysummary { font-size: 10px; }
    – millimoose
    Commented Feb 13, 2016 at 19:22
  • 1
    And if you do NOT want the .smallbox-paysummary to have a smaller font, why are you making it smaller in the first place?
    – millimoose
    Commented Feb 13, 2016 at 19:22
  • Now I'm wondering why if both rules have the same specificity the one declared later on the element's class attribute doesn't take precedence, regardless of the order of the CSS rules.
    – Andy
    Commented May 6, 2017 at 4:54

10 Answers 10

324

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:

  • ids (example: #main selects <div id="main">)
  • classes (ex.: .myclass), attribute selectors (ex.: [href=^https:]) and pseudo-classes (ex.: :hover)
  • elements (ex.: div) and pseudo-elements (ex.: ::before)

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

  • count the number of id selectors: there is one for each (#nav)
  • count the number of class selectors: there is one for each (:hover and .active)
  • count the number of element selectors: there are 3 (ul li a) for the first and 4 for the second (ul li a ::after), thus the second combined selector is more specific.

A good article about css selector specificity.

12
  • How does specificity override inline style declarations? Commented Aug 3, 2014 at 14:52
  • 5
    "... css rules in style tag ... overrides rules in css file ". I've seen this myth a few times recently. I don't know where it comes from, but it's nonsense.
    – Alohci
    Commented Aug 3, 2014 at 15:08
  • @jared I swapped the two first points. Is it this what you mean ? Commented Aug 3, 2014 at 15:10
  • Yes, that was what I meant. Commented Aug 3, 2014 at 15:11
  • 3
    @PakiPat :hover is not a class selector, but a pseudo-class selector. Commented Nov 22, 2017 at 16:14
79

Here's a compilation of CSS styling order in a diagram, on which CSS rules has higher priority and take precedence over the rest: CSS styling order

Disclaimer: My team and I worked this piece out together with a blog post (https://vecta.io/blog/definitive-guide-to-css-styling-order) which I think will come in handy to all front-end developers.

4
  • 14
    That diagram needs a special mindset. For me it is way more difficult to understand that diagram than style inheritance itself. Commented Dec 16, 2019 at 7:30
  • 3
    What does "left to right" and "top to bottom" ordering mean? Actually, later rules take precedence over earlier rules with the same specificity. So shouldn't it be bottom (highest) to top (lowest), since bottom takes precedence over top?
    – AlexPi
    Commented Feb 7, 2022 at 1:02
  • Why there are 2 "inline" items (inline attribute & inline style) in the graph? What do they refer to? What are the differences?
    – Grace
    Commented Nov 11, 2022 at 4:30
  • @Grace Inline attributes refer to HTML styling attributes except style. (e.g. height) In the diagram, the example code is below the explanation for inline attributes, not above. That confused me as well. Commented Jun 5 at 8:21
37

What we are looking at here is called specificity as stated by Mozilla:

Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied. Specificity is based on the matching rules which are composed of different sorts of CSS selectors.

Specificity is a weight that is applied to a given CSS declaration, determined by the number of each selector type in the matching selector. When multiple declarations have equal specificity, the last declaration found in the CSS is applied to the element. Specificity only applies when the same element is targeted by multiple declarations. As per CSS rules, directly targeted elements will always take precedence over rules which an element inherits from its ancestor.

I like the 0-0-0 explanation at https://specifishity.com:

enter image description here

Quite descriptive the picture of the !important directive! But sometimes it's the only way to override the inline style attribute. So it's a best practice trying to avoid both.

0
13

The order in which the classes appear in the html element does not matter, what counts is the order in which the blocks appear in the style sheet.

In your case .smallbox-paysummary is defined after .smallbox hence the 10px precedence.

0
8

First of all, based on your @extend directive, it seems you're not using pure CSS, but a preprocessor such as SASS os Stylus.

Now, when we talk about "order of precedence" in CSS, there is a general rule involved: whatever rules set after other rules (in a top-down fashion) are applied. In your case, just by specifying .smallbox after .smallbox-paysummary you would be able to change the precedence of your rules.

However, if you wanna go a bit further, I suggest this reading: CSS cascade W3C specification. You will find that the precedence of a rule is based on:

  1. The current media type;
  2. Importance;
  3. Origin;
  4. Specificity of the selector, and finally our well-known rule:
  5. Which one is latter specified.
7

Also important to note is that when you have two styles on an HTML element with equal precedence, the browser will give precedence to the styles that were written to the DOM last ... so if in the DOM:

<html>
<head>
<style>.container-ext { width: 100%; }</style>
<style>.container { width: 50px; }</style>
</head>
<body>
<div class="container container-ext">Hello World</div>
</body>

...the width of the div will be 50px

2
  • 2
    This is not really precedence, this is the Cascading part of Cascading Style Sheets.
    – TylerH
    Commented Oct 15, 2019 at 18:29
  • @TylerH, technically true, but I believe this answer has a place here because people arriving at this question might be conflating the difference.
    – Sablefoste
    Commented Dec 21, 2022 at 13:05
5

AS is state in W3: W3 Cascade CSS

the orden that different style sheet are applied is the following (quote from W3 cascading section):

  1. user agent declarations

  2. user normal declarations

  3. author normal declarations

  4. author important declarations

  5. user important declarations

More information about this in the referred W3 document

4
Element, Pseudo Element: d = 1 – (0,0,0,1)
Class, Pseudo class, Attribute: c = 1 – (0,0,1,0)
Id: b = 1 – (0,1,0,0)
Inline Style: a = 1 – (1,0,0,0)

Inline css ( html style attribute ) overrides css rules in style tag and css file

A more specific selector takes precedence over a less specific one.

Rules that appear later in the code override earlier rules if both have the same specificity.

2
  • Here's a good explanation: vanseodesign.com/css/css-specificity-inheritance-cascaade Commented Aug 11, 2016 at 17:02
  • 1
    @Mahi I waved your suggested edit through this time, but in the future, please do not add garbage to an edit just to make it long enough. Having a second edit that "undoes" the damage of a first one is very bad practice, very unintuitive to reviewers, and creates more work than necessary. Instead, just look for other things that could be improved (such as spelling or grammar).
    – Siguza
    Commented Feb 2, 2017 at 19:33
1

In a simple and short way, one should keep in mind the two things below:

  1. Inline CSS has a higher priority than embedded and external CSS.
  2. So final Order is: Value defined as Important > Inline > id nesting > id > class nesting > class > tag nesting > tag
0

Why are all the answers so Complicated ?

The ordering is as follows.

  1. Source Order

    1. Inline
    2. Internal
    3. External (Last Specificed CSS file)
  2. Important Keyword

  3. Specificity Score

    Score = Number of IDs + Number of Classes + Number of Elements.

Not the answer you're looking for? Browse other questions tagged or ask your own question.