CSS Academy · Lesson

@layer Declaration and Ordering

Declare named cascade layers and control their priority order with @layer statements.

Lesson 1 of 413 steps

@layer Declaration and Ordering is a free CSS Academy lesson on CoddyKit. This is lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the CSS Academy learning path, and your progress syncs across the web and the CoddyKit app. The CSS Academy course includes 4 lessons in total.

What is @layer?

CSS cascade layers (@layer) provide a mechanism to explicitly control the priority of CSS rules in the cascade — independent of specificity. Lower-priority layers lose even when they have higher specificity selectors.

Why @layer Matters

Without layers, every CSS rule competes using specificity and source order. This leads to specificity wars when reset, component, and utility styles all fight for priority. Layers give each group a fixed, predictable priority.

Declaring Layers

Declare layers upfront in a single statement. The order of declaration determines priority — later layers win over earlier ones.

<p>This scene only declares layer order — see the CSS panel:</p>
<p style="color:#888;font-size:0.9em;">@layer reset, base, components, utilities;</p>

Layer Declaration Position

The @layer declaration should be at the top of your stylesheet (or main import file). You can add styles to layers in any order after the declaration — the priority is determined by the declaration order, not where styles are added.

Why Source Order Does Not Matter for Layers

Because layer priority is determined at declaration time, you can add styles to a layer anywhere — even before it has been declared, by forward-referencing. The declared order is what matters.

Layer Priority Rule

A declaration in a later layer always wins over a declaration in an earlier layer, regardless of specificity:

<p>Reset layer !important paragraph (red)</p>
<div id="header">Components layer header (blue)</div>

Unlayered Styles

Styles placed outside any layer are called unlayered. They have the highest priority — even higher than the last declared layer. Use this for emergency overrides that should always win.

The Priority Stack

Complete priority order (highest to lowest for author styles):

  1. Unlayered styles (outside any @layer)
  2. Last declared layer (e.g., utilities)
  3. Second-to-last (e.g., components)
  4. Earlier layers (base, reset)

!important Reversal in Layers

!important in layers is tricky: it reverses layer order for that declaration. !important in the earliest layer wins over !important in later layers. This matches the original cascade behavior for user vs author styles.

Nested Layers

Layers can be nested using dot notation:

<p>This scene declares nested layers with dot notation — see the CSS panel.</p>

Practical Setup Example

A realistic project layer setup:

<p>A realistic project layer order — see the CSS panel.</p>

Quick Check

Layers are declared as: @layer base, components, utilities. Which layer's styles win in a conflict?

Recap

@layer establishes explicitly ordered cascade layers. Declare layer names in priority order (lowest first). Later layers always win over earlier ones, independent of specificity. Unlayered styles have the highest priority. !important reverses layer priority order for that declaration.

Free to start

Learn CSS with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “@layer Declaration and Ordering” lesson free?

Yes — the full text of “@layer Declaration and Ordering” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the CSS Academy course, upgrade to CoddyKit PRO. The CSS Academy course includes 4 lessons in total.

What will I learn in “@layer Declaration and Ordering”?

Declare named cascade layers and control their priority order with @layer statements. You practise CSS Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start CSS Academy?

No prior experience is required. CSS Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

How long does the “@layer Declaration and Ordering” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this CSS Academy lesson?

Yes. Every CSS Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. @layer Declaration and Ordering
  2. Adding Styles to Layers
  3. Layer Priority and !important Interaction
  4. Layers in Third-Party Code
← Back to CSS Academy