liveBook · Manning

Chapter 8: Cascade layers and nesting

What you will learn in chapter 8 of CSS in Depth, Second Edition

The introduction explains why maintainable CSS needs more than working layouts and introduces newer CSS features as tools for organizing styles and controlling the cascade.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why maintainable CSS needs more than working layouts and introduces newer CSS features as tools for organizing styles and controlling the cascade.

Key terms: code organization, brittle, first-class features, cascade, naming patterns

8.1 Manipulating the cascade with layers

This section shows how selector specificity can prevent a CSS rule from taking precedence even when the author intends it to.

Key terms: selector specificity, source order, a:any-link, .button

8.1.1 Defining layers

This section shows how to define cascade layers and explains their priority, browser-support implications, and naming options.

Key terms: @layer, cascade layers, layer priority, origin, inline styles, selector specificity, unlayered styles, named layers, anonymous layers, browser support

8.1.2 Layer order and priority

This section explains how cascade-layer order sets style priority, how !important reverses that order, and how nested layers provide finer organization.

Key terms: cascade layers, priority, @layer, @layer at-rule, precedence, unlayered styles, highest-priority layer, !important, prefers-reduced-motion, reset layer

8.1.3 The revert-layer keyword

Explains how revert-layer restores values from earlier cascade layers and how the all property can apply that reset to multiple properties.

Key terms: revert-layer, revert, current layer, previous layers, global layer, theme layer, all, initial, inherit, unset

8.2 A recommended organization for cascade layers

Introduces a flexible template for organizing cascade layers by priority, with later layers taking precedence over earlier ones.

Key terms: cascade layers, layer organization, priority, reset

8.2.1 Reset layer

This section explains why a reset layer is still useful and why it should contain small, reusable adjustments to browser defaults rather than site-specific design.

Key terms: reset stylesheet, user-agent styles, border box sizing, reset layer, site-specific design decisions

8.2.2 Theme layer

Explains what belongs in a low-priority theme layer, why site-wide values go there, and when custom properties should stay with a specific component.

Key terms: theme layer, custom properties, colors, accent-color, color-scheme, site-wide custom properties, layout layer, modules layer, --image-radius

8.2.3 Global layer

This subsection explains the global layer’s role in setting site-wide default styles and shows how it uses theme-layer values.

Key terms: global layer, base layer, default values, tag selectors, custom properties, theme layer, responsive behavior

8.2.4 Layout layer

Explains what belongs in a layout layer and how to apply different page layouts while managing selector specificity and reusable styles.

Key terms: layout layer, high-level layout, homepage ID, article ID, selector specificity, layer priority, ID, class name, l- prefix, modules layer

8.2.5 Modules layer

This section identifies the modules layer as the main home for reusable page components and gives brief guidance on naming, portability, and organizing its styles.

Key terms: modules layer, modules, components, module, unique name, nested layers

8.2.6 Utilities layer

This section explains when to use narrowly focused utility classes and why placing them in a high-priority cascade layer makes them reliable helpers.

Key terms: utility classes, modules, utilities layer, !important, high-priority layer, utility class

8.2.7 Additional layers

This section presents an optional layer for module states and emphasizes that the recommended layer structure can be adapted.

Key terms: states, modules, sublayer

8.3 The :is() and :where() pseudo-classes

Introduces :is() as a way to shorten repetitive selectors, then explains its matching, specificity, and pseudo-element restriction.

Key terms: :is(), pseudo-class, selectors, specificity, pseudo-element selectors, pseudo-classes

8.3.1 More forgiving selectors

The section shows how :is() lets a valid selector continue to work when another selector is unsupported.

Key terms: :is(), :user-invalid, .invalid, progressive enhancement

8.3.2 Manipulating specificity

This subsection shows how :where() and :is() can lower or raise selector specificity, and recommends keeping specificity low when possible.

Key terms: :where(), specificity, same layer, :is(), #fake-id

8.4 Nesting

Introduces CSS nesting by showing how it groups related rules, preserves their targets and specificity, and calls for care with nesting depth and browser support.

Key terms: nesting, parent rule, child rule, selector, selector specificity, final assembled selector, CSS nesting, usage statistics

8.4.1 Using the nesting selector

This section explains how the CSS nesting selector (&) changes where an outer selector appears in a nested selector, along with a limitation and an obsolete syntax restriction.

Key terms: nesting selector, compound selector, descendant combinator, &:hover, &::after, selector order, context, partial class names, tag selector, & h3

8.4.2 Understanding the nuances of the nesting selector

This section explains how CSS nesting translates to ordinary selectors and why that translation can affect specificity and which elements match.

Key terms: nesting, syntactic sugar, desugars, nesting selector, :is(), selector list, specificity, parent selector, &, nested selector

8.4.3 Nesting media queries and other at-rules

This section shows how nested at-rules keep related styles together and why ordinary declarations should come before nested blocks.

Key terms: nesting, @media, @layer, @supports, parent selector, nonnested declarations, nested blocks, source order, cascade, graceful degradation

8.5 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 7: Responsive design Chapter 9: Modular CSS and scope →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.