CSS specificity & the cascade
- type
- concept
- applies to
- resolving conflicts between rules
- inherits
- n/a
- common values
- inline > id > class > element, roughly, before !important
Where it goes
You do not write it. It is how the browser reads everything you have already written. It explains almost every "my CSS is not working" bug.
Example
/* both match the same <a>, the id wins regardless of order */
#nav a { color: red; }
.nav a { color: blue; }Open this example in the editor →
Common mistakes
- Reaching for !important to force a rule to win. It works, but it breaks the normal order for everyone who edits the file after you, including future you.
- Assuming the rule further down the file always wins. An id selector earlier in the file still beats a class selector later. Order only breaks ties.