KodingKaro
CSS · Advanced · Cascade, Inheritance, and Specificity

Specificity and conflict debugging

Specificity is one input to the cascade. Understanding its simple categories helps you avoid escalating selectors to force a style.

✓ What you will learn in this topic

  • Explain the key idea: Debug the winning declaration; avoid a specificity arms race.
  • Apply the concept: If both rules match the same paragraph, compare selector specificity and then cascade order to determine its color.
  • Avoid this common mistake:

Concept explanation

For ordinary selectors, IDs outweigh classes, attributes, and pseudo-classes; those outweigh type selectors and pseudo-elements. A universal selector adds no specificity. When specificity ties, later source order generally wins, subject to origin, importance, and layers.

Do not treat specificity as a contest. A long selector can make a design system brittle. Use devtools to identify the winning declaration, then simplify the conflicting rules.

Syntax

A CSS rule has a selector followed by declarations. Each declaration is a property and a value; the braces group the declarations that apply to matching elements.

.demo p {
  color: #475569;
}
.note {
  color: #1d4ed8;
}

Example 1: a small page element

Use this HTML as the target and place the CSS in a stylesheet linked to the page, or inside a <style> element while experimenting.

<main class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></main>
.demo p {
  color: #475569;
}
.note {
  color: #1d4ed8;
}

Expected result: If both rules match the same paragraph, compare selector specificity and then cascade order to determine its color.

Example 2: a practical interface card

The same idea can style a reusable card on a course page. The class selector keeps the rule attached to this component rather than changing every element of that type.

<section class="demo card-grid course-grid dashboard-panel course-card-list dashboard-grid card-layout"><h2>Learning card</h2><p>Clear styles make this information easier to scan.</p><nav class="site-nav"><a href="/learn">Courses</a></nav><label class="form-field"><span>Email</span><input type="email" aria-invalid="true"></label><article class="card course-card dashboard-card theme-dark"><span class="badge status">New</span><div class="course-card__image"></div><h2 class="course-card__title card__title">Course update</h2><p class="status">New lessons are available this week.</p><a href="/learn" aria-current="page">Read the details</a><div class="actions course-card__actions dashboard-panel__actions"><button type="button">View lessons</button><button type="button" disabled>Unavailable</button></div></article></section>
.card .status {
  color: #166534;
}
.status--warning {
  color: #92400e;
}

Expected result: The course update reads as a distinct interface card, and its content follows the spacing, color, and layout choices in the rule.

Common mistake

Adding !important to every conflict hides the real cascade and makes future changes harder.

Best practice

Inspect matched rules, reduce unnecessary specificity, and use source order intentionally.

Practice exercise

Given two competing component rules, predict the winner, then refactor the selectors so their intended priority is clear. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Debug the winning declaration; avoid a specificity arms race.
  • Inspect matched rules, reduce unnecessary specificity, and use source order intentionally.

Knowledge check

What is a good first step when a CSS value is crossed out in devtools?

Reveal the explanation

Inspect the computed style and matched rules before changing specificity.

Finish the module lessons, then take the server-scored module quiz to check your understanding.

Practical example

css
.demo p {
  color: #475569;
}
.note {
  color: #1d4ed8;
}

Expected result:

If both rules match the same paragraph, compare selector specificity and then cascade order to determine its color.

Hands-on practice task

Given two competing component rules, predict the winner, then refactor the selectors so their intended priority is clear.

In this section

Other lessons in Cascade, Inheritance, and Specificity

Broaden your knowledge

Related learning topics