KodingKaro
CSS · Advanced · Cascade Layers

Layer order and organization

Cascade layers let a stylesheet decide the priority of groups of rules before selector specificity is considered within those groups.

✓ What you will learn in this topic

  • Explain the key idea: Use a declared layer order to make stylesheet precedence intentional.
  • Apply the concept: The utility layer can take precedence over a more specific component rule because it appears later in the declared layer order.
  • Avoid this common mistake:

Concept explanation

Declare layer order with @layer reset, vendor, components, utilities;. Rules in later named layers have higher priority among normal declarations. This lets a low-specificity utility override a more specific component rule when the layer order says it should.

Important declarations reverse layer ordering, so avoid using important values as a routine override strategy. Keep the layer plan visible and documented for the team.

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.

@layer base, components, utilities;
@layer components {
  .demo p { color: #475569; }
}
@layer utilities {
  .text-brand { 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>
@layer base, components, utilities;
@layer components {
  .demo p { color: #475569; }
}
@layer utilities {
  .text-brand { color: #1d4ed8; }
}

Expected result: The utility layer can take precedence over a more specific component rule because it appears later in the declared layer order.

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>
@layer reset, vendor, app, overrides;
@layer app {
  .card .status { color: #475569; }
}
@layer overrides {
  .text-alert { color: #b91c1c; }
}

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

Assuming higher selector specificity always beats a declaration in another cascade layer.

Best practice

Declare a stable layer order early and keep unlayered styles intentional because they outrank normal layered declarations.

Practice exercise

Place reset, vendor, component, and utility rules into named layers and verify their intended order. Apply it to the example above and inspect the result at a narrow and a wide viewport.

Section summary

  • Use a declared layer order to make stylesheet precedence intentional.
  • Declare a stable layer order early and keep unlayered styles intentional because they outrank normal layered declarations.

Knowledge check

What does a named layer order control?

Reveal the explanation

Layers organize precedence; specificity is compared within the applicable cascade context.

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

Practical example

css
@layer base, components, utilities;
@layer components {
  .demo p { color: #475569; }
}
@layer utilities {
  .text-brand { color: #1d4ed8; }
}

Expected result:

The utility layer can take precedence over a more specific component rule because it appears later in the declared layer order.

Hands-on practice task

Place reset, vendor, component, and utility rules into named layers and verify their intended order.

In this section

Other lessons in Cascade Layers

Broaden your knowledge

Related learning topics