Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade

Steven Bradley

by Steven Bradley
on Tuesday, June 9th, 2009
in CSS

«

»

Have you ever run into the situation where you’re trying to apply a css style to an element, but it won’t take? Your page it seems to be ignoring your css, but you can’t figure out why. Maybe you found yourself using !important or adding an inline style as a last resort. There’s a good chance the problem you encountered was one of css precedence.

A better understanding of which css styles take precedence can lead to less frustration with css, cleaner code, and more organized css so let’s look at three things that control which css rule applies to a given html element:

  • Specificity Calculations
  • Inheritance
  • The Cascade

Learning these rules will take you to the next level in your css development.

Specificity Calculations

Imagine your html contains a paragraph with a class of “bio” applied to it. You also have the following two css rules:

p {font-size: 12px}
p.bio {font-size: 14px}

Would you expect the text in your paragraph to be 12px or 14px? You can probably guess in this case it will be 14px. The second line of css (p.bio) is more specific than the first when it comes to your class=”bio” paragraph. However, sometimes the specificity isn’t so easy to see.

For example consider the following html and css

<div id="sidebar">
<p class="bio">text here</p>
</div>
div p.bio {font-size: 14px}
#sidebar p {font-size: 12px}

The first line of css might seem more specific at first glance, but it’s actually the second line above that would be more specific to the font-size of your paragraph. Why is that?

To answer the question we need to consider the rules of specificity.

Specificity is calculated by counting various components of your css and expressing them in a form (a,b,c,d). This will be clearer with an example, but first the components.

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

An id is more specific than a class is more specific than an element.

You calculate specificity by counting each of the above and adding 1 to either a,b,c, or d. It’s also important to note that 0,0,1,0 is more specific than 0,0,0,15. Let’s look at some examples to make the calculation clearer.

  • p: 1 element – (0,0,0,1)
  • div: 1 element – (0,0,0,1)
  • #sidebar: 1 id – (0,1,0,0)
  • div#sidebar: 1 element, 1 id – (0,1,0,1)
  • div#sidebar p: 2 elements, 1 id – (0,1,0,2)
  • div#sidebar p.bio: 2 elements, 1 class, 1 id – (0,1,1,2)

Let’s look again at the example above

div p.bio {font-size: 14px} - (0,0,1,2)
#sidebar p {font-size: 12px} - (0,1,0,1)

The second has the higher specificity and thus takes precedence.

One last point before we move on. Importance trumps specificity, When you mark a css property with !important you’re overriding specificity rules and so

div p.bio {font-size: 14px !important}
#sidebar p {font-size: 12px}

means the first line of css above takes precedence instead of the second. !important is still mostly a hack around the basic rules and is something you should never need if you understand how the rules work.

Inheritance

The idea behind inheritance is relatively easy to understand. Elements inherit styles from their parent container. If you set the body tag to use color: red then the text for all elements inside the body will also be red unless otherwise specified.

Not all css properties are inherited, though. For example margins and paddings are non-inherited properties. If you set a margin or padding on a div, the paragraphs inside that div do not inherit the margin and padding you set on the div. The paragraph will use the default browser margin and padding until you declare otherwise.

You can explicitly set a property to inherit styles from it’s parent container, though. For example you could declare

p {margin: inherit; padding: inherit}

and your paragraph would then inherit both from it’s containing element.

The Cascade

At the highest level the cascade is what controls all css precedence and works as follows.

  1. Find all css declarations that apply to the element and property in question.
  2. Sort by origin and weight. Origin refers to the source of the declaration (author styles, user styles, browser defaults) and weight refers to the importance of the declaration. (author has more weight than user which has more weight than default. !importance has more weight than normal declarations)
  3. Calculate specificity
  4. If two rules are equal in all of the above, the one declared last wins. CSS embedded in the html always come after external stylesheets regardless of the order in the html

#3 above is likely the one you’ll need to pay attention to most. With #2 just understand that your styles will override how a user sets their browser unless they set their rules to be important.

Also realize that your styles will override the browser defaults, but those defaults do exist and is often what leads to cross browser issues. Using a reset file like Eric Meyer’s CSS Reset or Yahoo’s YUI Reset CSS helps take the default styles out of the equation.

Summary

Hopefully the above helps sort out some of your css precedence issues. Most of the time if you have a conflict in styles the issue will come down to specificity. At times when you haven’t declared some css, but an element is behaving in a way you don’t expect it’s likely that element has inherited some css from a parent container or a default style of the browser.

A general rule of thumb when declaring your css is to declare properties with the least specificity needed to style your elements. Use #sidebar instead of div#sidebar for example. I admit to breaking this general rule far more than I should, but by using the least specificity needed it will make it easier for you to override a style by declaring a style more specific.

If you use the most specificity you may run into problems later and find yourself having to add unnecessary html in order to be able to add more specificity or you may find yourself falling back on using !important or declaring inline styles. Start with the least specificity and add more only as needed.

Spread some karma These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Sphinn
  • del.icio.us
  • Mixx
  • StumbleUpon

Subscribe to TheVanBlog | Email This Post Email This Post

62 Responses to “Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade”

MyAvatars 0.2
2009-06-09 13:15:49

This helps answer a few things I have come across when doing CSS. I can usually work my way through them, but its good to understand the reason thing happen.

 
MyAvatars 0.2
2009-06-09 21:45:36

Glad I could help Bryan. The specificity rules are out there, but I think quite a few people miss them. I know at times I’ve added some css and couldn’t figure out why it didn’t change how things displayed. Usually the answer is making the new rule more specific.

 
MyAvatars 0.2
2009-06-12 06:50:29

The information that you have shared in this post is really valuable and the description is also easy to understand. It is really great.

MyAvatars 0.2
2009-06-23 15:30:11

Thanks Amiee. I’m glad you found the post helpful.

 
 
MyAvatars 0.2
2009-06-18 00:24:50

Great post! Thank you very much for given this post…

 
MyAvatars 0.2
Len
2009-06-18 23:23:52

Nicely written.
Should this be (0,0,1,2) from the article?
“div p.bio {font-size: 14px} – (0,0,1,1)”

MyAvatars 0.2
2009-06-23 15:28:02

Yep Good catch. I’ll fix that now.

Thanks.

 
 
MyAvatars 0.2
2009-06-19 06:24:50

Great article. I always wanted to try to figure it out by myself but I think you just described it the best way it could.

MyAvatars 0.2
2009-06-23 15:29:37

Glad I could help.

 
 
MyAvatars 0.2
2009-09-15 03:36:48

Straight to the point. Complex problem explained very simply.Thanks.

 
MyAvatars 0.2
2009-11-06 00:19:19

Very Valuable article on on Inheritance in CSS.

 
MyAvatars 0.2
2009-11-17 23:35:46

Nice Explanation on CSS Style Precedence.

 
MyAvatars 0.2
2009-11-18 01:17:44

Thanks all. I’m glad you liked the post and found it helpful.

 
MyAvatars 0.2
Hitesh Chavda
2010-03-11 08:59:41

Simple and to the point article.

Thanks Steven,

MyAvatars 0.2
2010-03-15 13:28:19

Thanks Hitesh.

 
 
MyAvatars 0.2
arjay
2010-03-12 09:12:42

A wonderfully helpful article, thank you so much.

MyAvatars 0.2
2010-03-15 13:28:35

Thanks arjay.

 
 
MyAvatars 0.2
2010-03-12 19:23:57

Nice Article, at least for a beginner like me :)

MyAvatars 0.2
2010-03-15 13:29:25

Hopefully it can help a few people past the beginner stage too. This stuff wasn’t always clear to me when I first started learning css and writing the post actually cleared up a few things for me.

 
 
MyAvatars 0.2
2010-03-13 08:28:21

very helpful… specificity is something i pay far too little attention when writing css. thanks a lot!

MyAvatars 0.2
2010-03-15 13:30:26

It can be easy to ignore and work around, but if you do pay attention to it you can end up writing leaner code and you’ll likely have a few less headaches here and there wondering why one of your styles won’t take.

 
 
MyAvatars 0.2
Bhadra
2010-03-14 22:50:25

Awesome article!!

MyAvatars 0.2
2010-03-15 13:30:47

Thanks Bhadra.

 
 
MyAvatars 0.2
2010-03-15 02:10:40

Well, i used to change things and see if they work. I had this problem and i didn’t know that was the case. I kept trying and it didn’t worked until i specified the highest ID from the HTML structure.

MyAvatars 0.2
2010-03-15 13:32:45

I used to do that too. Sometimes the specificity of things can be confusing or difficult to see so you add more and more higher level selectors.

However the more you think about specificity as you’re writing your css, the more you won’t have to work your way back up the stack and the leaner you can make your code.

 
 
MyAvatars 0.2
2010-03-16 04:21:50

That really sorted me out!

So simple and yet so vital.

Thank you very much.

Seun, Nigeria.

MyAvatars 0.2
2010-03-16 22:26:48

Glad I could help.

 
 
MyAvatars 0.2
2010-04-15 03:24:49

Very interesting , completely understood what css specificity means :)

 
MyAvatars 0.2
2010-06-01 17:46:50

Superb article; it’s helped me immensely to understand how CSS rules get assigned precedence, which has been bugging me a lot recently.

One small question: in the section on The Cascade, which source gets higher precedence? In the article, you said author > user > user agent. From my reading (albeit novice) of the w3school spec, it seemed to me that user stylesheets get the final say over author specs. Am I having reading difficulty? (It’s pretty likely..)

MyAvatars 0.2
2010-06-02 09:54:06

Thanks Andy.

It could very well have been me misreading things. From my understanding the author stylesheet would take precedence unless the user has given their own stylesheet extra importance.

I opened up Firefox’s preferences. On the content tab you set the fonts. If you click the advanced button, there’s a checkbox to allow pages to choose their own fonts, instead of the selections set. I’m pretty sure that’s checked by default and so the author style ends up taking precedence.

If I uncheck the box then my font choices were displayed. I think unchecking the box is equivalent to setting !important on the user stylesheet which would then give the user styles more weight than the author styles.

I’m pretty sure that’s how it works in all browsers, but again it could be me misreading things. I think I had to read over the section at w3.org a few times myself. :)

 
 
MyAvatars 0.2
David
2010-07-21 22:48:44

Very useful, thanks !

 
MyAvatars 0.2
2010-08-19 17:31:17

Fantastic! I was scratching my head, I didn’t realize id took precedence over class. Very well done article.

MyAvatars 0.2
2010-08-22 21:55:04

Glad I could help Bradford. Usually when you hit those scratching your head moments about why some css value isn’t taking hold, it’s almost always an issue with the precedence.

 
 

What others are saying about this post

links for 2009-06-11 « innovations in higher education
Twitted by VizionThree
uberVU - social comments
Understanding Style Precedence in CSS: Specificity, Inheritance, and the Cascade | Dev Loom
Precedence in CSS « WWWti.me
Understanding CSS Style Precedence | Van SEO Design « Netcrema – creme de la social news via digg + delicious + stumpleupon + reddit
Most Tweeted Articles by Wordpress Experts
Notable Tech Posts – 2010.03.14 | The Life of Lew Ayotte
2010-03-12 유용한 링크 | We are connected
CSS Specificity And Inheritance - Smashing Magazine
CSS Specificity And Inheritance
CSS Specificity And Inheritance | Web Design Cool
CSS Specificity And Inheritance | Easybranches.com™
Wordpress Blog Services - CSS Specificity And Inheritance
TG Designer » CSS Specificity And Inheritance
ArticleSave :: Uncategorized :: CSS Specificity And Inheritance
CSS Specificity And Inheritance | DesignerLinks | Home to Web design news, jQuery Tutorials, CSS tutorials, Web Designing tutorials, JavaScript tutorials and more!
CSS Specificity And Inheritance | CMS Code
Barker Design | Graphic & Web Development » Blog Archive » CSS Specificity And Inheritance
AMB Album » CSS Specificity And Inheritance
CSS Specificity And Inheritance | Webreweries.com | Tips | Photoshop | Java | Illustrator | Dreamweaver | After Effects | Graphics | Animation | Design
News Update » CSS Specificity And Inheritance
CSS Specificity And Inheritance | jiggymotto.com
CSS Specificity And Inheritance | Communication et marketing
CSS Specificity And Inheritance » blog.lanche86.com
CSS Specificity And Inheritance | Retail Marketing Strategy
Thoughts On Building A Typographic Stylesheet
Everything Is Crap » Blog Archiv » CSS is crap
CSS Vererbung?/Formatierungsproblem - php.de


Name (required)
E-mail (required - never shown publicly)
URI
Your Comment

Subscribe

Search TheVanBlog

Recommended

Small Business Forum Teaching Sells Free Report

SEO Book Training

Popular Posts

Proud member of the Smashing Network