Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS CSS Basics (2014) Basic Selectors Reusing Classes

Jason Ruby
Jason Ruby
2,867 Points

ID selector vs. Class selector: Order of Precedence

I am just looking to confirm that I completely understand the application of defined styles as they are applied to ID and Class selectors in the case when an element in an HTML file is identified by both an ID and a Class.

Assuming there are no other styles that are affecting the element in the HTML file the styles applied via the ID will override any style applied through reference of the Class specific to that element.

Is that correct?

3 Answers

Steven Parker
Steven Parker
216,057 Points

Yes, assuming that only the ID or class are being used as the sole selectors and not combined with anything.

:point_right: The general rules for CSS specificity are:

  • inline is more specific than id
  • id is more specific than class
  • class is more specific than element
Kevin Korte
Kevin Korte
28,109 Points

The bottom of this article does a great job visually showing you how specificity is calculated.

https://css-tricks.com/specifics-on-css-specificity/

Jason Ruby
Jason Ruby
2,867 Points

Very cool article and really handy. Thank you, Kevin! This might be a good resource to include in Teacher Notes for one of the applicable courses or corresponding lessons. It may be a bit too advanced for the lesson from which my questions was initiated. None the less, it is still some solid information and a great reference. Thank you again!

Jason Ruby
Jason Ruby
2,867 Points

Thank you, Steven!