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

Matthew Jones
Matthew Jones
999 Points

Workspace Id's

For some odd reason, when I assign a <li> item an id and I try to text-align it to the center, it does not work. Everything else works, including the color change, pixel amount, and font size. However, if I was more specific and I climbed down the tree (EX: header, nav, div, ul, li) the text-align property works. Does anyone have an answer for me?

Hey Matthew,

could you please post your css code here?

2 Answers

Ethan Lowry
PLUS
Ethan Lowry
Courses Plus Student 7,323 Points

What you've described is common - the more specific you are with the number of selectors you use, the higher priority that selector has.

So in some cases you may need to use more selectors to have your new style rule 'override' some existing one,by having a higher priority than it. In this case, it might be that the text-align style is set somewhere else, but the other values you're setting aren't, which is why only text-align is being affected by this.

If you want more detail, selector priority follows a points system to determine the selector's overall value. Think of it this way: id's are worth 100 points, classes are worth 10, and elements are worth 1.

So this:

#container .nice-label { color: 'red' }

Has an ID and a class, so is worth 110 points, and would override this:

.nice-label a { color: 'blue' }

...which has only 11 points (a class and an element).

Hope that helps!

Matthew Jones
Matthew Jones
999 Points

Thank you Ethan, I had no idea that you still had to be specific even with an ID or Class label. That changes the whole game.

Ethan Lowry
Ethan Lowry
Courses Plus Student 7,323 Points

No problem - as a general rule you should still always aim to be as un-specific as you can get away with, to make your CSS easier to read and understand - just so you don't start writing monstrosities like #big_div #small_div .cool_form fieldset input :P

James Barnett
James Barnett
39,199 Points

as a general rule you should still always aim to be as un-specific as you can get away with

Think of specificity as a resource, you should always try to conserve specificity to allow for future changes that will require a more specific selector. Careless use of overly specific selectors leads to what is know as a selector war.