SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

Welcome to CSS
Selecting ElementsCombinatorsAttribute SelectorsPseudo-ClassesPseudo-ElementsSpecificity and InheritanceResolving Style Conflicts Matching selectors and tracing the cascade
Selectors and the Cascade

Resolving Style Conflicts

Use cascade order, specificity, inheritance, and importance to explain which declaration wins a style conflict.

When more than one CSS declaration sets the same property on an element, the browser uses the cascade to decide which value applies. Understanding that decision helps you explain why a style appears and how to change it.

Where styles come from

A browser starts with its own user-agent stylesheet: default styles for HTML elements. For example, headings may appear bold and links may be blue and underlined. An author stylesheet contains styles written by the page's developers and can override browser defaults. Users can also supply styles to adjust a page's appearance, such as its font size or colors.

Browser defaults, author styles and user styles all feed into the cascade that determines an element’s styling.

The cascade considers the origin and importance of declarations before comparing selector specificity. For ordinary stylesheet declarations, important values outrank normal values. Important user styles can override important author styles, allowing a reader to enforce a preferred color or size. Among declarations that remain tied, other factors such as layers, specificity, and source order help determine the winner.

Compare competing rules

Predict the color of the header. All three author rules match it.

#header {
  color: blue;
}
.header {
  color: red;
}
header {
  color: green;
}

All three selectors match the same element and set color. The declarations are normal author styles in the same layer, so compare their specificity, a measure based on selector types. An ID selector is more specific than a class selector, which is more specific than an element selector. Therefore #header wins and the text is blue, even though the header rule appears later.

Comparing ID, class and type counts from left to right, #header wins with 1–0–0 over .header at 0–1–0 and header at 0–0–1.

For these selector types, specificity can be written as three counts: IDs, classes, and elements. For example, #header is 1,0,0, .header is 0,1,0, and header is 0,0,1. Compare the counts from left to right; the first difference decides the result. Attribute selectors and pseudo-classes count in the class column, while pseudo-elements count in the element column. Inline styles have higher priority than these normal selector rules.

The table compares selectors using the same three counts. A selector can include more than one type, so header .nav has one class count and one element count.

SelectorID, class, element countsRelative specificity
#header1,0,0Highest
header .nav0,1,1Higher than .nav
.nav0,1,0Higher than div
div0,0,1Lowest

The class counts for header .nav and .nav tie, so the element count makes header .nav more specific. The ID count makes #header more specific than either of them.

If importance, layer, and specificity are tied, source order decides. Here both selectors have the same specificity, so the later declaration makes the text green:

.example {
  color: red;
}
.example {
  color: green;
}

Use importance carefully

Adding !important to a declaration raises its priority over conflicting normal declarations:

.element {
  color: green !important;
}

The text is green: the important declaration wins over the normal inline color. Use !important sparingly; frequent use makes conflicts harder to debug and styles harder to maintain. When important declarations compete, the cascade still has to resolve the conflict.

Organize rules with layers

Cascade layers group styles and give you another way to control their priority within the same origin. For example:

@layer main, components;

@layer main {
  .button {
    font-size: 16px;
  }
}

@layer components {
  .button {
    font-size: 14px;
  }
}

Both rules now set font-size on the same button. For normal author rules in these layers, the later layer has priority, so the button’s text is 14px. If you mark both declarations important, layer priority reverses and the 16px value wins. Normal unlayered stylesheet rules outrank normal layered ones.

Cascade cheat sheet

The examples below assume both selectors match the same element and set the same property. Except where !important is shown, assume normal author declarations in the same layer. Specificity is shown as ID, class, element counts; combinators such as the space and > do not add a count.

ScenarioCompeting declarationsWinner and reason
Class and elementp (0,0,1) vs. .note (0,1,0).note: the class count is higher.
Pseudo-class.button (0,1,0) vs. .button:hover (0,2,0).button:hover when the button is hovered: the pseudo-class adds a class count.
Pseudo-element.note::after (0,1,1) vs. p::after (0,0,2).note::after: the class count decides before the element count. Both rules target the same ::after pseudo-element.
Descendant combinatorbutton (0,0,1) vs. .component button (0,1,1).component button: the class count is higher.
Child combinator.panel p (0,1,1) vs. .panel > p (0,1,1)If both match, the later declaration wins; the combinators add no specificity.
Media query.note (0,1,0) vs. .note (0,1,0) inside a later matching media queryThe later declaration wins while the media condition matches. The media query itself does not increase specificity.
Important declaration#header (1,0,0) vs. .header (0,1,0) with !importantThe important .header declaration wins over the normal ID declaration.
Inline style#header (1,0,0) vs. a normal inline styleThe inline style wins over the normal ID rule.

For the media-query example, the second rule only competes when its condition matches:

.note {
  color: red;
}

@media (min-width: 600px) {
  .note {
    color: green;
  }
}

At widths of at least 600px, both declarations match, and the later one sets the color to green. Otherwise, the first rule sets it to red.

Trace a conflict

When a style is unexpected, inspect the element in browser developer tools and identify the declarations that set the property. Check their origin and importance, then consider layer priority and specificity. If those checks tie, look at source order. Some properties can also inherit a value from a parent when the child has no value of its own.

For example, if both button and .component button set a normal color in the same layer, .component button wins because it is more specific. If the competing button declaration is marked !important, that important color wins instead. This sequence lets you identify the cause before changing the stylesheet.

Try it yourself

Try it yourself

Let the theme layer win

HTML + CSS

Loading code editor…

Live preview

Updating…

Final output

No comparison yetChecks after 3s without edits
Loading your draft…

Key Takeaways

  • Cascade: The browser resolves competing declarations using their priority, specificity, and order.
  • Origins and importance: Styles can come from the browser, the author, or the user; !important raises a declaration's priority over normal declarations.
  • Specificity: Compare selector types after earlier cascade checks are tied; source order resolves a remaining tie.
  • Layers: Group styles into layers to organize and control their priority.
  • Debugging: Inspect the matching declarations and trace the cascade before changing a rule.

Take the quiz · Matching selectors and tracing the cascade

How is this lesson?

Previous lessonSpecificity and InheritanceNext lesson Matching selectors and tracing the cascade
Edit on GitHubReport on GitHub
On this page
%

On this page

Where styles come fromCompare competing rulesUse importance carefullyOrganize rules with layersCascade cheat sheetTrace a conflictTry it yourselfKey Takeaways