Sub topic 3
Origins and !important: who wrote the rule
This chapter assumes you can write CSS rules with selectors and declarations, and that you can inspect an element in browser developer tools. The previous chapter showed that the cascade picks one winner when several declarations target the same property of the same element. Here you will learn the first criterion it checks: where a declaration came from, and whether it is marked !important.
The three origins
Every declaration belongs to an origin, which identifies who wrote it.
- User-agent origin: the browser's built-in stylesheet. It is why links are blue and underlined and why
h1is large and bold before you write any CSS. - User origin: styles set by the person using the browser, such as a forced minimum font size or a custom stylesheet from an accessibility setting.
- Author origin: the styles you write, in
<link>stylesheets,<style>elements, andstyleattributes.
For normal declarations, the author wins over the user, and the user wins over the browser. Your site's CSS overrides browser defaults, and it also overrides most user preferences.
What !important changes
Adding !important to a declaration moves it into a separate, higher-priority tier. Among important declarations, the order of origins is reversed: the user-agent comes first, then the user, then the author.
This reversal protects users. Someone who sets an important rule for readability or contrast can't have it overridden by a site's important rules. The browser's own important rules, which are rare, win over everything.
p { color: red !important; }
#intro { color: green; }
/* <p id="intro">Hello</p> renders red.
Both rules are author origin, but only the first is important,
so specificity is never compared. */The id selector has far higher specificity, but it loses. The cascade compares importance and origin first, and it only looks at specificity among declarations in the same tier.
The full order
Here is the order of precedence from highest to lowest, as defined by the CSS Cascade specification.
| Rank | Tier |
|---|---|
| 1 | Transitions (while a transition is running) |
| 2 | Important user-agent declarations |
| 3 | Important user declarations |
| 4 | Important author declarations |
| 5 | Animations (`@keyframes` values while animating) |
| 6 | Normal author declarations |
| 7 | Normal user declarations |
| 8 | Normal user-agent declarations |
Only tied declarations move to the next step. If one declaration is the sole survivor at any step, the process ends there.
Inline styles and !important
A style attribute is author origin. A normal inline declaration beats any normal declaration from a stylesheet, because inline styles outrank selectors in specificity. An !important declaration in a stylesheet still beats a normal inline declaration, because importance is checked first.
/* In the stylesheet: */
p { color: red !important; }
/* In the HTML: <p style="color: green">Hello</p>
The paragraph is red. The important rule is in a higher tier
than the normal inline declaration. */An important inline declaration, style="color: green !important", would beat the stylesheet rule, since both are important and inline wins on specificity.
Animations and transitions
Declarations produced by @keyframes animations override normal author declarations while the animation runs. They don't override important ones. Any !important inside a keyframe is ignored, so it is dropped, not applied. Transitions sit at the top. While a transition runs, its value beats even important declarations, which keeps animated changes smooth.
Common errors
- Using
!importantto fix a specificity fight. It works at first. Later, the only way to override it is another!importantwith equal or higher specificity, and the stylesheet escalates. Prefer a more precise selector or a cascade layer. - Assuming the user can't override you. A user's important declaration beats your important one. Your
!importantdoes not stop the browser's accessibility settings. - Expecting
!importantto inherit. It affects only the cascade for that element. A child doesn't receive the importance, only the computed value, and only for inherited properties. - Forgetting the shorthand effect.
margin: 0 !importantmarks all four longhand properties important.
Exercise
A stylesheet contains a { color: blue !important; } and #nav a { color: orange; }. A user's browser has an important user-origin rule a { color: black !important; }. What color is a link inside #nav, and why?
Show answer
The link is black. Importance is checked before specificity, so both normal declarations lose to the important ones. Among important declarations, the user origin outranks the author origin, so the user's black beats the author's blue.
Exercise
Why does p { color: red !important; } beat <p style="color: green">?
Show answer
Both are author origin, but the stylesheet declaration is important and the inline one is normal. The cascade checks importance before specificity, so the important declaration is in a higher tier and wins.