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 CSS Basics (2014) Basic Selectors Class Selectors

Michelle Vanwagenen
Michelle Vanwagenen
5,614 Points

According to W3c in html, to reference a class or id of a paragraph they use p.class or p#id

Treehouse is teaching this as .class and #id, no p. Is this a difference of writing css in html vs a css doc? is treehouse using a shortcut?

Jason Butler
Jason Butler
5,732 Points

The difference is one of specificity, and what elements your CSS rules are being applied to.

By writing .class, you will affect every element that has that class applied to it. If you add the p for p .class, it will affect p elements that have the class applied; an h1 or a div with the same class won't be changed by that particular declaration. Same with p #id vs. just #id (though in theory there should only be one element per page with that #id, but that's another discussion).

2 Answers

Shawn Boyd
Shawn Boyd
14,345 Points

.className or #idName will select any HTML elements with that class or id. Adding an HTML selector, such as p, before the class or ID selector provides more specificity - selecting only paragraph elements with that class or ID, even if a div, span, etc. somewhere else shares the same class.

If you don't have any other elements with the same class name elsewhere you don't need to specify the HTML selector.

Michelle Vanwagenen
Michelle Vanwagenen
5,614 Points

Thank you Jason and Shawn! I thought that might be the case, but couldn't find any specifics to clarify.