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
Iban Dominguez
17,973 PointsUsing ID as selectors CSS
Hi there!
I have recently read a few articles where they mentioned that using id in your stylesheet as selectors is not a good idea.
I have been using them for a long time and never noticed any problem or at least I was not aware of.
Any suggestions?
Thanks beforehand! cheers!
6 Answers
Sadi Qevani
311 PointsHey Iban,
The problem with the ID selectors is that they are unique and they are not scalable/reusable since you use them only on one element, and they power over classes is 1000 times greater.
If you have to overwrite an ID rule is almost impossible :D.
Iban Dominguez
17,973 PointsHey Sadi!
Thanks for your answer! that's what I thought, I never reused and they always worked fine!
The reason why I asked this question is because I have just installed a plugin called linter for sublime text 2 that basically highlights syntax errors and it adds a warning flag whenever I use ID as selector in the stylesheet "Don't use ID's selectors" so I wondered if they were any other incoveniences apart from that, I guess that might be the only one!
Cheers!
Sadi Qevani
311 PointsHey Iban,
Well thats the only one, there are technically no other problems, its fine to use them, and its only a matter of style for using or not using them.
Usually you can use them when you have a GREATER STYLE which you wont overwrite later, such let say a Wrapper, you give, margin:0 auto; and some specific styles etc. But for other stuff many developers suggest you do it with Classes.
I highly encourage you to read about OOCSS, since it helps you a lot in your development process its scalable and you can reuse your classes, and mix them etc :)
Iban Dominguez
17,973 PointsHey Sadi!
Yeah I always used for #page-wrapper, #main-header, #main-content and #footer. I guess I freaked out a little bit when I got that warning flag from the sublime linter package.
I will def. have a look at OOCSS!
Thank you very much! happy coding!
Mike Bronner
16,395 PointsTypically I use IDs when they meet the following criteria:
- the element with the ID is and always will be unique on the page
- there will never be a need for another element like it -- this is mostly for structural use.
Use classes for:
- page structures that will be repeated
- to use combinations of classes to effectively style a page dynamically
Using IDs for page structure actually helps ensure that things that aren't meant to be duplicated aren't, as they will fail validation otherwise, so I think declaring the use of IDs as bad practice is incorrect. I would be highly suspicious of a plugin that throws an error on usage of IDs. Who knows what else they are assuming, or perhaps even wrong about!
I would bring this to the attention of the developer of the plugin and ask why they are throwing an error. See what his reasons are.
Here is an interesting article that discusses the flaws of CSSLint and similar software:
And here is another article with the opposite viewpoint:
Iban Dominguez
17,973 PointsThanks Mike!