How do class and ID selectors differ in specificity and use case?

Your grasp of CSS specificity weights and selector architecture.
IDs are 1-0-0 and meant for unique elements; classes are 0-1-0 and reusable.
Saying order alone decides specificity or using IDs for repeated components.
What's really being asked
This question checks two things. First, do you know how browsers calculate specificity using the ID-CLASS-TYPE weight categories. Second, do you understand the architectural difference between unique and reusable selectors, which matters for building scalable design systems and avoiding specificity wars.
The full answer
A strong response starts with the numeric specificity values from the cascade algorithm. An ID selector like #example contributes 1-0-0 to the weight, while a class selector like .myClass contributes 0-1-0. That means a single ID outweighs any number of classes, type selectors, or pseudo-classes combined in the CLASS and TYPE columns. Next, the candidate should explain intended use. IDs are meant to identify a single unique element per document, such as a main navigation container or a modal root. Classes are meant for repeatable styling patterns, such as buttons, cards, or form inputs that appear many times. A senior candidate might also note that specificity is only compared within the same cascade origin and layer, and that when specificity is equal, order of appearance wins.
The mistakes people make
Watch out for answers that say the only difference is that IDs are unique in the HTML while classes are not. That is true for the DOM, but the question asks about CSS specificity and use case, so missing the 1-0-0 versus 0-1-0 scoring is a gap. Another red flag is claiming that inline styles or important rules are part of the ID versus class comparison; those are separate concepts in the cascade. Finally, saying you always use IDs for styling because they are stronger is an anti-pattern; it makes overrides difficult and breaks component reusability.
What usually comes next
An interviewer might ask how you would override an ID selector without using important. They could also ask about the specificity of attribute selectors, pseudo-classes, or pseudo-elements. Another common pivot is asking why many CSS methodologies like BEM avoid IDs entirely, or how cascade layers and important interact with specificity.
A concrete example
Imagine a page with a header that has an ID of site-header and a class of sticky. A rule targeting #site-header has specificity 1-0-0. A rule targeting .sticky has specificity 0-1-0. Even if the sticky class rule appears later in the stylesheet, the ID rule will win if they set the same property, because 1-0-0 is greater than 0-1-0. If you later want to reuse that sticky behavior on a sidebar, you should use the class selector rather than adding another ID, keeping the specificity low and the pattern reusable.
Interview question
An element has both id='banner' and class='active'. If #banner sets margin-top: 10px and .active sets margin-top: 20px, which value applies?
- a.10px, because IDs must be unique in the HTML document
- b.10px, because an ID selector has higher specificity than a class selectorCorrect
- c.20px, because multiple classes can outweigh a single ID
- d.20px, because the class rule appears later in the stylesheet
Why? this is the answer
An ID selector contributes 1-0-0 to specificity while a class contributes 0-1-0, so #banner wins even if .active appears later. IDs being unique in the DOM is an HTML constraint, not the reason the cascade favors the ID selector.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #css
- #specificity
- #selectors
- #design-systems
- #frontend
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.
See open roles