3 ms·
>Isn't the whole point of an ID that it is like a class, but unique? The argument against IDs is to encourage a mindset for making reusable rules. Obviously, i
by huckfinnaafb 15y ago
>Isn't the whole point of an ID that it is like a class, but unique?
The argument against IDs is to encourage a mindset for making reusable rules. Obviously, in any project of even basic complexity, you're going to run into elements that only appear once per document. Having a class rule that's used only once is not only acceptable, but probably an inevitability.
In my projects, I use IDs purely for Javascript identification. I never use IDs to style things because 1) they complicate CSS specificity rules and 2) I get an urge to couple the element with JS, which creates this weird bind between JS and CSS that shouldn't exist.
The other end of the argument will suggest that it makes for better code readability: an element with an ID guarantees the author intended for this element to appear only once in this document.
There's the whole "IDs are faster than classes" argument; the renderer has to do a substring match instead of a full string match on classes, which you can imagine with today's processors is a relatively tiny hurdle given the scale of an average HTML document.
The whole thing is a trivial matter, but there exists a group of people who focus a good portion of their career on, specifically, CSS, and like any master of the craft, arguments at their end of the knowledge spectrum is going to boil down to things that seem like minor details to laymen. So don't worry about IDs vs. classes too much.
- jacobr 15y agoSpeaking of specificity, maybe using !important should also give a warning.
- huckfinnaafb 15y agoJust submitted this as an issue on GitHub, clearly !important is a bad practice for application-native CSS.
- Ameero123 15y agoGreat points huckfinnaafb thanks for sharing.