Skip to content
Guest
Learn CSS · Course Syllabus Show lessons
Visual Polish & Motion · 9 min read · Beginner

Native CSS Nesting & @layer Architecture

Write cleaner component styles with native CSS nesting (&) and eliminate specificity bugs forever using @layer cascade layers.

What you'll learn

  • Group component rules and states cleanly with native CSS nesting (&)
  • Organize stylesheets predictably with @layer reset, base, components, utilities
  • Understand why layer order beats selector specificity

Two of the biggest upgrades in modern CSS (Native Nesting and Cascade Layers (@layer))let you write clean, scalable stylesheets without Sass or !important.

Native CSS Nesting

Group child rules, hover states, and media queries right inside the parent selector:

.course-pill {
  background: #eff6ff;
  color: #1d4ed8;

  &:hover {
    background: #dbeafe;
  }

  & strong {
    font-weight: 800;
  }
}

Cascade Layers (@layer)

Declare your layer order once at the top of your stylesheet:

@layer reset, components, utilities;

Rules in a later layer (utilities) always beat rules in an earlier layer (components), even if the earlier layer used a more specific selector!

Fill-in-the-Blank Exercise

Cascade Layers & Nesting

Declare layers so 'utilities' always overrides 'base', and nest a :hover state using &.

@ base, utilities;

.card {
border: 1px solid #cbd5e1;
:hover {
  border-color: #2563eb;
}
}

Try It Yourself

Notice how .btn-highlight in @layer utilities overrides button.primary in @layer components cleanly!

Native CSS Nesting + @layer
Live Result

Check Your Understanding

Score: 0 / 1

1. Given @layer base, utilities; which rule wins when both target the same element?

Study Workspace

Finished this lesson?

Your progress is saved locally in your browser, no account needed.

Search courses, lessons, and references

Type to search across all courses, lessons, and quick references.

Press Esc to close Powered by Pagefind