The Cascade: Origins, !important, Inline Styles and Source Order
The full algorithm for choosing between conflicting declarations: origins, !important and why it reverses origin order, shadow DOM context, inline styles, layers, specificity, scope proximity and source order, plus animations and HTML attributes.
kvi5hnuCode Sprout team member10 min readOn this page
Every element can be matched by many rules, from your stylesheets, the browser's defaults and sometimes the user's own settings. When several declarations set the same property on the same element, the cascade picks one: the cascaded value. Specificity (article 15) is one step of that process, but it isn't the first, and knowing the whole order explains almost every "why doesn't my CSS apply?" question.
The steps
For each property of each element, the browser collects every declaration that applies, then compares them by these criteria in order. The first criterion that differs decides, and the later ones aren't considered:
- Origin and importance: where the declaration comes from, and whether it's
!important. - Context: whether it comes from the page or from a shadow tree (article 14).
- Element-attached styles: whether it's in a
styleattribute. - Cascade layers: which
@layerit's in (article 20). - Specificity of the selector (article 15).
- Scope proximity: for
@scoperules, how close the scope root is (article 22). - Order of appearance: the later declaration wins.

If no declaration applies at all, there's no cascaded value, and the property is inherited or takes its initial value (article 16).
Origins
Declarations come from three origins:
- User-agent styles: the browser's default stylesheet. It gives headings their sizes, lists their bullets, links their colors and
display: noneto<head>. - User styles: settings chosen by the person using the browser, such as a minimum font size, or a custom stylesheet added with an extension.
- Author styles: everything the page provides, in
<link>,<style>,@importandstyleattributes. These are yours.
For normal declarations, author beats user, and user beats user agent. That's why one line of your CSS overrides the browser's default margin on <h1>, and why the browser's defaults still apply to every property you don't set.
/* author */
h1 { margin-block: 0 0.5rem; } /* beats the browser's default margin */
The cascade compares each property separately: your rule replaces the default margin of h1, but the default font-size and font-weight still apply.
!important
Adding !important after a value makes a declaration important:
.visually-hidden { position: absolute !important; }
- It goes after the value, before the semicolon. In shorthands, it applies to every longhand:
margin: 0 !important. - It applies to one declaration, not the whole rule.
- Whitespace is allowed (
! important), and the keyword is case-insensitive, but write it as!important. - The
!doesn't mean "not". It's just CSS's marker for the flag.
Important declarations form a separate, stronger group for each origin. And for important declarations, the order of origins is reversed:
| Rank | Declarations |
|---|---|
| 1 (strongest) | transitions |
| 2 | user-agent !important |
| 3 | user !important |
| 4 | author !important |
| 5 | animations |
| 6 | author normal |
| 7 | user normal |
| 8 (weakest) | user-agent normal |
The reversal exists to protect people. A user who needs large text or high contrast can mark their settings !important, and no page can override them, however hard it tries. The browser's important declarations are things pages must never change, such as some styles of form controls and of top-layer elements.
Within the same rank, the rest of the steps apply as usual: between two author !important declarations, specificity and order decide.
When to use !important
Rarely. Each one can only be overridden by another !important with a stronger position, so they escalate. Reasonable uses:
- Utility classes that must always win, such as
.visually-hiddenor.hidden { display: none !important; }. - Overriding inline styles you can't change, such as those set by a third-party script.
- Respecting user preferences, for example turning off animations in a
prefers-reduced-motionblock.
For ordering whole groups of styles, cascade layers (article 20) are the modern replacement. Important declarations in layers have their own reversed order, which article 20 explains.
Context: shadow trees
For elements that can be styled both from the page and from a shadow tree, such as a web component's host or its slotted children, the cascade then compares context. For normal declarations, the page wins. For important declarations, the shadow tree wins (article 14).
Inline styles
Declarations in a style attribute are element-attached. They beat every normal declaration from the author's stylesheets, whatever the selector's specificity or layer:
<p id="intro" class="lead" style="color: #2f7d4f;">Green, whatever the stylesheets say</p>
An !important declaration in a stylesheet beats a normal inline style, because importance is compared first. An !important inline style beats all author declarations, important or not. Animations also beat normal inline styles, because they rank above all normal declarations.
#a.lead { color: #b3261e; } /* loses: the inline style wins */
.b { color: #2f7d4f !important; } /* wins: important beats normal inline */
.c { color: #b3261e !important; } /* loses: important inline beats it */
p { font: 1rem system-ui, sans-serif; }
Scripts that set element.style.color write inline styles too, which is why components that set styles with JavaScript are hard to override. Toggling classes keeps styles in the stylesheet, where the cascade works normally.
Layers, specificity and scope proximity
If origin, importance, context and attachment are equal, the browser compares cascade layers: for normal declarations, the later layer wins, and styles outside any layer beat all layers (article 20). Then specificity decides. Then, for rules in @scope blocks, scope proximity: the rule whose scope root is fewer generations away from the element wins (article 22).
Order of appearance
Finally, if everything else is equal, the last declaration wins. Order is the order in which the browser sees the CSS:
- Within a stylesheet, from top to bottom.
- Between stylesheets, in the order of the
<link>and<style>elements in the document. - An
@imported stylesheet counts as if its contents were written at the position of the@import, so the importing stylesheet's own rules come after it. - Within a rule, a later declaration of the same property replaces an earlier one. That's what makes fallbacks work:
width: 90%; width: min(90%, 60rem);(article 6).
Order matters for link states. a:link, a:visited, a:focus-visible, a:hover and a:active have equal specificity, so if a visited link is hovered, the rule that comes last applies. Write them in the order link, visited, focus, hover, active (article 11), so each later state overrides the earlier ones.
Animations and transitions
CSS animations and transitions behave like extra origins:
- Animations override all normal declarations, including inline styles, but lose to
!importantones.!importantinside@keyframesis invalid, and the declaration is ignored. - Transitions beat everything, even
!important, but only while they run. The transition goes toward whatever value the cascade picked, so it can't make a losing value win.

Each panel adds one source, and each one moves up a rank in the table above: inline style, then animation, then an important author declaration.
Presentational hints
Some HTML attributes affect rendering: width and height on images and tables, bgcolor, align, border on tables. Browsers treat them as author styles with zero specificity, placed before all other author styles. Any CSS rule that sets the same property overrides them:
<img src="cover.jpg" width="400" height="600" alt="">
<style> img { width: 100%; height: auto; } </style> <!-- wins over width="400" -->
The hidden attribute is just as weak. The HTML specification implements it as display: none in the browser's default stylesheet, and Chrome treats it like a presentational hint; either way, a rule such as .card { display: flex; } overrides it, and the element appears. Many resets add [hidden] { display: none !important; } for that reason.
![Three renders. An img with width="400" and the rule img { width: 100% } is drawn 259px wide, the width of its container, not 400px. A card with the hidden attribute and a .card { display: flex } rule is visible above the text that follows it. With [hidden] { display: none !important } added, the card disappears and only the following text remains](https://code-sprout-content.laxmi.solutions/asset/2026/11/css-presentational-hints-render-lc3e34ifklrucdzj.png)
The middle panel is a real bug in many sites: a component's display rule silently undoes hidden.
What's current
- The full cascade order, including layers and scope proximity, is defined by the CSS Cascade specification, level 6, and works this way in all current browsers.
- Scope proximity comes after specificity. Early drafts put it before, so some 2022–2023 articles describe the old order.
- Chrome has no built-in user stylesheet, though extensions add one. Safari (Settings, Advanced, Style sheet) and Firefox (
userContent.css) support them natively. Browser settings such as minimum font sizes and forced colors (article 35) affect pages much like user styles.
Common questions
Why does the browser's style win over mine?
It usually doesn't: author styles beat browser defaults for normal declarations. Most often, your rule doesn't match the element, or it sets a different property. For example, the browser's a:link color applies to the link itself, while your color on its parent only reaches it by inheritance (article 16). The Styles pane in DevTools shows every rule that matches.
Does !important make a rule more specific?
No. It moves the declaration into a stronger group that's compared before specificity. Between two important declarations, specificity decides as usual.
Can I override an inline style without changing the HTML?
Only with !important in a stylesheet, or with an animation. That's one reason to avoid inline styles in reusable code.
Why are important declarations in reverse order?
So users keep control. Without the reversal, a page's !important could defeat a user's !important accessibility settings.
What wins between two <link> stylesheets with the same selectors?
The one linked later in the document, if their declarations are otherwise equal.
Summary
- The cascade compares origin and importance, context, inline attachment, layers, specificity, scope proximity and order, and the first difference decides.
- Normal declarations: author beats user, and user beats the browser's defaults.
!importantdeclarations beat normal ones, and their origin order is reversed, which protects users' settings.- Inline styles beat normal stylesheet declarations of any specificity.
- Animations override normal declarations, and transitions override everything while they run.
- Presentational HTML attributes act as zero-specificity author styles and lose to any CSS rule.
- Use
!importantrarely: for utilities, overriding inline styles you don't control, and user preferences.
Practice questions
A link has style="color: navy", and the stylesheet has the rule below. What color is the link, and why?
#footer a.link { color: gray; }
Answer
Navy. Inline declarations are element-attached, which is compared before specificity, so the inline style beats any normal declaration from a stylesheet, however specific.
A user's own stylesheet and the page both set the body's font size with !important. Which applies?
/* the user's stylesheet */
body { font-size: 24px !important; }
/* the page's stylesheet */
body { font-size: 16px !important; }
Answer
24px, the user's. For important declarations, the origin order is reversed, so user !important beats author !important. The reversal ensures users can always enforce their needs.
Two rules have the same specificity, and both set color. One is in base.css, imported with @import at the top of site.css, and the other is written in site.css. Which wins?
Answer
The rule written in site.css. An imported stylesheet counts as if its contents were at the position of the @import, which is before the rest of site.css, so the later rule wins.
An element has the hidden attribute and the class toolbar, styled by the rule below. Is it visible? How do you make hidden reliable?
.toolbar { display: flex; }
Answer
It's visible. The hidden attribute only provides a weak display: none (from the browser's default styles, or as a presentational hint), so any author rule that sets display overrides it. Add [hidden] { display: none !important; } to the base styles.
Previous: Inheritance: Which Properties Pass Down and Why
Next: Global Keywords: inherit, initial, unset, revert, revert-layer and all

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.