8 Cascade layers and nesting

 

This chapter covers

  • Organizing CSS in a logical manner
  • Controlling the cascade using layers
  • Using the :is() and :where() pseudo-classes
  • Grouping related styles with nesting

In parts 1 and 2, we looked at the intricacies of CSS and the tools it provides for laying out elements on the page. We’ve made sense of the box model, margin collapsing, stacking contexts, flexbox, and grid. You’ll need these skills, particularly when you first set out on a new project. In the world of software development, however, there’s more to programming than just making your code work. It’s also important to organize code in a way that’s understandable and allows for future changes without making it more brittle. In CSS, this surfaces an entirely new set of difficulties.

In the past, the only way developers have been able to address these problems is with strictly prescribed methodologies and naming patterns, but CSS has some brand-new features to help us in this regard. These are first-class features that provide better means of code organization and explicit control over the cascade.

8.1 Manipulating the cascade with layers

8.1.1 Defining layers

8.1.2 Layer order and priority

8.1.3 The revert-layer keyword

8.2 A recommended organization for cascade layers

8.2.1 Reset layer

8.2.2 Theme layer

8.2.3 Global layer

8.2.4 Layout layer

8.2.5 Modules layer

8.2.6 Utilities layer

8.2.7 Additional layers

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

8.3.1 More forgiving selectors

8.3.2 Manipulating specificity

8.4 Nesting

8.4.1 Using the nesting selector

8.4.2 Understanding the nuances of the nesting selector

8.4.3 Nesting media queries and other at-rules

Summary