liveBook · Manning

Chapter 9: Modular CSS and scope

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

The introduction explains why broad effects from CSS changes make organization important and identifies the modules layer as the place to organize most styles into manageable pieces.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why broad effects from CSS changes make organization important and identifies the modules layer as the place to organize most styles into manageable pieces.

Key terms: stylesheet, elements, layers, modules layer, modular pieces of code

9.1 Defining modules

This section defines modular CSS as a way to build a page from reusable, self-contained components whose styles do not interfere with one another.

Key terms: modular CSS, module, encapsulation, unique class name, DOM element, child elements, nest modules

9.1.1 Modules and global styles

The section establishes reset and global styles as a starting point for building modules while clarifying that modular CSS does not require cascade layers.

Key terms: resets, global styles, modules, layers, modular CSS, browser support

9.1.2 A simple module

A simple message module shows how a class-only selector makes CSS reusable and why that reuse improves consistency.

Key terms: module, message class, selector, ruleset, reusable module, consistent UI, visual consistency

9.1.3 Variations of a module

This section shows how modifier classes create reusable module variants and why they are preferable to selectors that change a module based on where it appears.

Key terms: module, modifier, module class, BEM, block, double-hyphen, size modifiers, color modifiers, em, font size

9.1.4 Modules with multiple elements

This section shows how to build a multi-element media module, combine its subelement classes with a variant, and choose selectors that remain useful as the module changes.

Key terms: media module, subelement, media__image, media__body, BEM, variant, media--right, descendant selectors, media__body > h4, generic tag types

9.2 Modules composed into larger structures

This section explains why CSS modules should have focused responsibilities and how to spot a module that may need splitting.

Key terms: module, responsibility, encapsulation, Single Responsibility Principle

9.2.1 Dividing multiple responsibilities among modules

The section builds a dropdown menu from separate dropdown and menu modules, then explains how to keep their responsibilities, state, positioning, and styles manageable.

Key terms: dropdown module, menu module, compose, standalone, containing block, position: relative, absolutely positioned, ::after pseudo-element, state class, is-open

9.2.2 Naming modules

This section explains how to name CSS modules and variants so their names remain useful across contexts and redesigns, and briefly contrasts naming conventions with CSS-in-JS approaches.

Key terms: module, media, running-tip, media-tile, headline-tile, variant classes, button--danger, button--success, button--large, button--20px

9.3 CSS scope

Introduces @scope as a browser-enforced way to keep a module’s styles within its part of the page, then shows how scoped selectors and their specificity work.

Key terms: scope, @scope, BEM, modular styles, scope selector, :scope, scope root, .media, specificity, img

9.3.1 Scope proximity

Explains how scope proximity resolves conflicts between nested CSS modules and why that helps preserve a module’s own styles.

Key terms: scope proximity, selector specificity, source order, innermost scope, modular CSS, modules, HTML structure

9.3.2 Scoping limit

This section explains how scoping limits keep a module’s selectors out of nested modules while retaining the flexibility of the CSS cascade.

Key terms: scoping limit, to keyword, upper bound, lower bound, donut scoping, proximity, conflicting styles, nested modules, dropdown module, tabs module

9.3.3 Implicit scope

This subsection explains how an embedded style tag can use implicit CSS scope and when that approach is useful.

Key terms: @scope rule, implicit scope, scope’s root element, one-off content, modules, reuse

9.3.4 Scope and layers

This section explains how scoping limits, selector specificity, and cascade layers can be combined to control which modular CSS styles take precedence.

Key terms: specificity, inner scope, outer scope, scoping limit, cascade layers, modules layer, sublayers, modules.base, modules.ui

9.4 Pattern libraries

This section explains how reusable CSS modules change page-building and why teams document those modules in a pattern library.

Key terms: modules, class names, stylesheet, pattern library, inventory, CSS module

9.4.1 Using a CSS-first workflow

This section explains a CSS-first workflow in which developers build and document reusable modules in a pattern library before using them on pages.

Key terms: CSS-first development, pattern library, module, variant, existing styles, consistent interface, reusable solution, CSS-first, well-structured HTML, documentation

9.4.2 Refactoring and breaking changes

The section explains how to change modular CSS without unexpectedly breaking existing pages, using deprecation, versioning, coordinated upgrades, and a pattern library.

Key terms: deprecate, module, pattern library, semver, major, minor, patch, versioning, CSS-first development, decoupled

9.5 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 8: Cascade layers and nesting Chapter 10: Container queries →

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.