Sub topic 3
Origins and !important: Who Wrote the Rule
When two declarations set the same property on the same element, the cascade picks one. Its first test is not specificity or source order. It asks who wrote each declaration and whether the author marked it !important. This chapter covers that first test.
This chapter assumes you can write CSS rules with selectors and declarations, and that you know how to inspect an element in browser developer tools. It does not assume you know how specificity is calculated. The next chapter covers that.
The three origins
Every declaration the browser considers comes from one of three origins:
- User-agent origin. This is the browser's built-in default stylesheet. It is why a
<div>is a block, a<h1>is large and bold, and<body>has an 8px margin even when you have written no CSS. - User origin. These are styles set by the person using the browser. Examples are a forced minimum font size, a high-contrast mode, or a custom stylesheet from an extension. Most visitors never use this origin, but it exists mainly for accessibility.
- Author origin. These are the styles you write:
<link>stylesheets,<style>elements andstyleattributes.
The author origin is the only one you control. You still need to understand the other two, because they explain why your own CSS sometimes seems to be overridden by something you never wrote.
Normal declarations: the author wins
For ordinary declarations, the origin that is closest to the page's creator beats the others. The order from strongest to weakest is author, then user, then user-agent.
This is why your CSS can change the browser defaults without any special effort. The default stylesheet colors links with a built-in value. You write one rule and it takes over:
a { color: darkslategray; }The browser's rule for links has a more specific selector than a. It still loses, because the comparison never reaches specificity. The origins differ, and the author origin outranks the user-agent origin. The cascade only compares specificity between declarations that tie on every earlier criterion.
What !important does
Adding !important to a declaration moves it into a stronger group. Compare these two rules:
p { color: red !important; }
#intro { color: green; }With the markup <p id="intro">Hello</p>, the paragraph is red. The #intro selector is far more specific than p. Its declaration is normal and the other is important, so the importance test decides the result before specificity is considered.
Some syntax details:
!importantgoes after the value and before the semicolon.- It is case-insensitive, and whitespace after the
!is allowed, but write it as!important. - A misspelling such as
!imporantmakes the whole declaration invalid, and the browser discards it.
Important reverses the origin order
The surprising part is that important declarations reverse the order of the origins. For normal declarations the author is strongest. For important declarations the user-agent is strongest and the author is weakest of the three.
The reason is control. A user who needs a high-contrast color scheme or a larger font must be able to override whatever an author wrote. If authors could always win, an author could lock out the user's accessibility settings. Marking a declaration important lets the user protect it from the author. The browser can protect its own critical styles in the same way.
The full order, from strongest to weakest, is:
| Rank | Group | Typical example |
|---|---|---|
| 1 | Transitions | A property currently animating through a CSS transition |
| 2 | Important, user-agent | A browser rule the browser itself marks important |
| 3 | Important, user | A user's forced accessibility setting |
| 4 | Important, author | Your `color: red !important` |
| 5 | Animations | Values produced by `@keyframes` while an animation runs |
| 6 | Normal, author | Almost all the CSS you write |
| 7 | Normal, user | A user's custom stylesheet without `!important` |
| 8 | Normal, user-agent | The browser's default styles |
The cascade works down this list. When two competing declarations belong to different groups, the one in the higher group wins immediately. Later criteria matter only when both declarations are in the same group.
Animations and transitions
Animations and transitions have their own places in the order, shown in the table.
An animation sits above normal author declarations. While an animation runs, its keyframe values override your ordinary rules, whatever their specificity:
.badge {
background: gray;
animation: flash 1s infinite;
}
#header .badge {
background: black;
}
@keyframes flash {
50% { background: gold; }
}Even though #header .badge is more specific than .badge, the badge flashes gold. If you add !important to the background declaration in a normal rule, you move that declaration above the animation and the badge stops flashing. The animation keeps running but has no visible effect on that property.
The reverse does not work. A declaration inside @keyframes that has !important is ignored entirely:
@keyframes fade {
from { opacity: 1 !important; } /* ignored by the browser */
to { opacity: 0.4; }
}Transitions are at the top. While a transition is in progress, its in-between values override everything, including important declarations. This lets a smooth change play out even when the end state is marked !important.
Important declarations within one origin
!important does not end the contest. It only places a declaration in a group. Two declarations in the same group are still compared by the remaining criteria: cascade layers, then specificity, then source order.
.btn { color: white !important; }
#checkout .btn { color: black !important; }Both declarations are important and both are author origin. They tie on origin and importance, so the cascade compares specificity. #checkout .btn is more specific, so the button text is black. If the two selectors were equally specific, the later rule would win.
This is why !important tends to start an arms race. Once someone uses it to win a conflict, the only way to override it is another !important with higher specificity or a later position.
Important and the style attribute
An inline style attribute is author origin and normally beats any stylesheet rule. An important declaration in a stylesheet moves above a normal inline declaration:
<style>
.banner { display: none !important; }
</style>
<div class="banner" style="display: block;">Hidden</div>The banner stays hidden. This is a legitimate use of !important. A script or third-party widget has written inline styles you cannot edit, and you need to override them from a stylesheet. An inline declaration that is itself !important would win over this rule.
Shorthands expand to every longhand
When you put !important on a shorthand property, every longhand it sets becomes important. This causes surprises later:
.box { margin: 0 !important; }
.box.wide { margin-left: 2rem; }margin: 0 !important sets margin-top, margin-right, margin-bottom and margin-left, all with importance. The second rule has a more specific selector but only a normal declaration, so .box.wide keeps margin-left: 0. You must make the second declaration important too, or restructure the rules.
Common errors
- Using
!importantto fix a specificity problem. It works once, then every later fix needs it too. Find out why the competing selector wins. Chapter 12 shows how to do this with developer tools. - Expecting
!importantto beat an animation or transition from the other direction. Important beats animations. Nothing important beats a running transition. - Putting
!importantin@keyframes. The declaration is discarded, so the animation silently lacks that property. - Forgetting that shorthands expand. An important shorthand also locks the longhands you did not mean to lock.
- Assuming your styles are the strongest. An important user or user-agent declaration beats your important author declaration. If your styles never apply and your DevTools show no competing author rule, the winner may come from another origin.
Checking the origin in developer tools
In the Styles panel, the browser lists rules from several origins. Author rules show the stylesheet file and line number. Browser defaults appear under the label "user agent stylesheet". A declaration that has lost the cascade appears struck through. If a property you set is struck through and the winning rule says "user agent stylesheet", you have a case where an important browser declaration outranks your normal one. That is rare, but it is the first place to look when no author rule seems to compete.
Exercise
A page has these rules, with the markup <p id="note" class="alert">Text</p>. What color is the paragraph, and why? p { color: blue !important; } .alert { color: orange !important; } #note { color: green; }
Show answer
The paragraph is orange. The #note declaration is normal, so it loses to both important declarations at the importance step, even though its selector is the most specific. The two important author declarations tie on origin and importance, so specificity decides, and .alert (class) is more specific than p (element).
Exercise
A third-party script sets style="color: red" on a heading, and you cannot edit the script. How can you make the heading blue from your stylesheet, and what is the risk?
Show answer
Write a rule with an important declaration, for example h1.title { color: blue !important; }. An important author declaration in a stylesheet beats a normal inline declaration. The risk is that the only way to override your rule later is another important declaration with equal or higher specificity or later position, so keep the selector narrow and add a comment explaining why !important is there.
Exercise
A user turns on a browser setting that forces high-contrast text, implemented with an important user-origin declaration. Your stylesheet sets color: #777 !important on body text. Whose color is shown, and why does the order work this way?
Show answer
The user's color is shown. For important declarations the origin order is reversed, so important user declarations beat important author declarations. The design lets users protect accessibility settings from authors.