Blog post

🌸 Write Predictable CSS

Rekha Kumari

-
July 31, 2025
CSS
HTML
Web Development
Front End Development
CSS3

CSS controls how styles are applied to HTML elements, but when multiple rules target the same element, which style wins? That’s where The Cascade and Specificity come into play. They are core concepts vital for writing predictable, maintainable CSS.

Trying to fix layout with CSS…

1. What is The CSS Cascade?

  • The cascade determines which rules apply when multiple rules conflict.
  • CSS rules come from various sources with different origins:
    - User agent (browser defaults)
    - User stylesheets (Custom style provided by user)
    - Author stylesheets (your CSS)
    - Inline styles in HTM
  • When rules conflict, browsers apply them based on:
    - Origin and Importance: !important rules override normal ones.
    - Specificity: More specific selectors take precedence.
    - Source Order: If rules have equal specificity and origin, the later one wins

In short, the cascade is the browser’s decision-making process to resolve conflicting styles.

2. What is Specificity?

  • Specificity is a scoring system CSS browsers use to decide which selector is “stronger” or more specific.
  • Calculated based on selector types involved in the rule:
Specificity point of selectors

Specificity is counted as a four-level value like inline styles, IDs, classes/attributes/pseudo-classes, elements compared lexicographically.

  • If two selectors have the same specificity, the one appearing last in the CSS wins.

3. How Cascade and Specificity Work Together

  • The cascade first sorts rules by origin and importance.
  • Within rules of the same origin and importance, specificity is the decider.
  • If specificity ties, then source order decides.

Example:

{
color : blue; } /* Specificity: 0-0-0-1 */ .intro {
color : red; } /* Specificity: 0-0-1-0 */ #main {
color : green; } /* Specificity: 0-1-0-1 */

For a <p class="intro" id="main">:

  • #main p wins since its specificity (ID selector) is highest.

4. Why Does This Matter?

  • Avoid accidentally overridden styles by understanding specificity.
  • Prevent using unnecessary !important declarations that hurt maintainability.
  • Write cleaner CSS that behaves predictably without juggling order or hacks.
  • Debug CSS issues faster by knowing why certain styles apply.

5. Quick Tips to Master Cascade & Specificity

  • Use IDs sparingly — they have very high specificity.
  • Prefer class selectors for styling since they strike a good balance.
  • Order your CSS logically, but rely on specificity to control overrides.
  • Avoid deep selector nesting; it increases specificity and complexity.
  • Use inline styles only when absolutely necessary.
  • Remember, !important overrides almost everything — use it rarely.

✨ TL;DR for Experts

  • Cascade resolves conflicts based on origin, specificity, and order.
  • Specificity assigns weight to selectors: Inline > ID > Class/Attribute/Pseudo > Element.
  • The highest specificity rule applies; if tied, later rule wins.
  • Understanding both is essential for predictable, maintainable CSS and efficient debugging.

Related Blog Posts