specificitymedium
How does CSS decide which rule wins?
Answer
First by origin and importance, then specificity, then source order. Specificity counts inline styles, then ids, then classes/attributes/pseudo-classes, then elements — a single id outranks any number of classes. If specificity ties, the later rule wins. !important overrides the normal cascade and is usually a sign the selector should have been more specific instead.
Points an interviewer listens for
- Order: importance → specificity → source order
- inline > id > class/attribute/pseudo-class > element
- Ties broken by whichever comes last
- !important is an escape hatch, not a tool
Reported at
Similar questions have been reported by candidates at Aaizel International Technologies Private Limited, Cognizant, Fabulous Media, Sapiens, Sebstrone Industries.
Now try answering it out loud
Interview practice mode times your spoken answer and checks it against the points above.
Practise this question