Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS How to Make a Website CSS: Cascading Style Sheets What is CSS?

CSS Specificity

The video lightly mentioned specificity, can you elaborate on it?

4 Answers

Laura Cressman
Laura Cressman
12,548 Points

I think this article does the topic much better justice than I ever could, but here is my two cents. http://www.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/

Specificity allows you to specify which properties you wish to apply to which elements. Basically, there are two parts to CSS rules. There's the part of what you want it to look like (for example, background: green;) as well as what you want that look to apply to (for example, h1, .nav, #wrapper, etc.). Specificity uses a set of rules outlined in the article above to determine which elements get the looks you want applied to them.

A big part of specificity is the order in which the styles are applied. All else equal, if you accidentally apply two styles to the same element, then the last style wins. For example, consider the code:

div {
   background: red;
}

div {
   background: green;
}

In this case, the background of the divs will be green because this rule is applied second. You can get around this with the !important flag (background: red !important; will make it red) but be careful when using it, and I try to avoid it. Here is more detail on CSS tricks as well: http://css-tricks.com/specifics-on-css-specificity/

Hope that helps! Let me know if you have further questions.

Smiles :) Laura

Hi Kendra, here's some additional resources for you on CSS specificity and the cascade.

CSS Specificity and the Cascade

Treehouse Blog

Chris Shaw
Chris Shaw
26,676 Points

Hi Kendra,

Smashing Magazine have a great article on this which I recommend you read.

http://www.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/

Thanks for the fast response! I will definitely check those resources out.