Skip to content
Back to all posts
Technology

Understanding CSS Specificity: How the Cascade Picks Styles

Understand CSS specificity, source order, cascade layers, :is(), :where(), and practical ways to debug overriding styles.

3 min read

TechSilo

Technology writer & developer

When two CSS rules set the same property, the browser uses the cascade to choose a winner. Specificity is only one step. Origin, importance, cascade layer, scope proximity, and finally source order can all matter.

Start with an example

html
<button class="action" id="submit">Save</button>
css
button { color: blue; }
.action { color: green; }
#submit { color: purple; }

With these ordinary author rules in the same layer, the ID selector has higher specificity than the class or element selectors, so the button text is purple.

A quick specificity mental model

| Selector | ID | Class / attribute / pseudo-class | Type / pseudo-element |

| --- | ---: | ---: | ---: |

| button | 0 | 0 | 1 |

| .action | 0 | 1 | 0 |

| button.action | 0 | 1 | 1 |

| #submit | 1 | 0 | 0 |

Compare columns from left to right. An ID beats any number of classes in this simplified comparison. Only compare specificity after the cascade has resolved higher-priority considerations such as origin and cascade layers.

Newer pseudo-classes

:where() always contributes zero specificity, even when its arguments contain selectors with IDs.

css
:where(article, section) p { line-height: 1.6; }

:is(), :not(), and :has() take the specificity of their most specific matching selector-list arguments, rather than adding a fixed pseudo-class value. These can make an otherwise convenient selector unexpectedly difficult to override.

Source order and !important

For declarations in the same origin and layer, with equal importance and specificity, a later declaration wins:

css
.notice { color: navy; }
.notice { color: teal; } /* wins */

Avoid using !important as a routine fix. It changes cascade priority and can make future changes harder. When you inherit a stylesheet with many !important rules, diagnose the competing origins and layers rather than adding more exceptions.

Use cascade layers to control priorities

css
@layer reset, components, utilities;

@layer components {
  .card { padding: 1rem; }
}

@layer utilities {
  .p-0 { padding: 0; }
}

For normal declarations, a later layer has precedence over earlier layers. Layer ordering for !important declarations is reversed, so learn both rules before introducing important overrides.

Debug an unexpected style

1. Inspect the element in browser DevTools.

2. Find every declaration for the disputed property.

3. Check whether a rule is crossed out because of a more important origin, layer, or selector.

4. Verify selector matching and media/container queries.

5. Prefer simple classes or layers to escalating specificity.

Specificity explains which matching declaration takes precedence—it doesn't explain why a selector failed to match. Keep those debugging questions separate.

Practical recommendation

Create a small set of predictable class names, keep component styles local, and use :where() for low-specificity defaults. If your design needs a consistent palette, experiment with TechSilo's [Color Tools](/tools/color-tools).

Want to put this into practice?

Explore TechSilo's free developer tools for practical utilities you can use directly in your browser.

Related blog posts