liveBook · Manning

Chapter 10: Container queries

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

This introduction explains why viewport-based media queries can fall short for reusable modules and introduces the two kinds of container queries.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction explains why viewport-based media queries can fall short for reusable modules and introduces the two kinds of container queries.

Key terms: module, viewport, @media query, container, container size queries, container style queries, custom property

10.1 A basic example of a container query

A media module in a narrow sidebar shows why viewport-based media queries can fail for reusable components and why container size is a better basis for their responsive layout.

Key terms: media module, media queries, viewport, sidebar, container queries, modular design, container

10.1.1 Using container size queries

This subsection shows how to define a container and use its width to adapt a media module’s descendants without tying the module’s styles to the sidebar’s styles.

Key terms: container-type, container-name, container, @container, media module, sidebar, breakpoints, container query, descendant elements, not

10.2 A closer look at containers

This section introduces a closer examination of container names, types, and restrictions without yet explaining how they work.

10.2.1 Container types

Explains how containment prevents container-query feedback loops and how each container type affects which dimensions can be queried.

Key terms: containment, container query, container-type, normal, inline-size, inline size, flex-basis, flex-grow, size, full-size containment

10.2.2 Container names

This section explains how container names determine which ancestor a container query uses, including what happens when names are omitted.

Key terms: container name, matching name, container ancestor, DOM, container-type, container shorthand property, container query

10.2.3 Containers and modular CSS

This section shows how consistently defined containers let nested CSS modules respond to their available space without requiring a wrapper around every module.

Key terms: modular CSS, container, media__body, layout styles, flex-grow, container queries, media queries, media-container, container-type: inline

10.3 Container-relative units

This section defines container-relative units and shows how they make card dimensions respond to their containers without requiring an @container query.

Key terms: container-relative units, query container, cqw, cqh, cqi, cqb, inline-size container, size type container, block-direction units, clamp()

10.4 Container style queries

This section introduces container style queries as a way to conditionally style descendants based on an inherited custom property, and notes their limited browser support.

Key terms: container style queries, style(), custom property, descendant elements, container size queries, size containment, progressive enhancement, experimental, browser support

10.4.1 Decoupling a module from its container

Shows how a container style query lets a race-detail card adapt to a sidebar without tying the card’s styles to that particular page layout.

Key terms: style query, --sidebar, race-detail, inline size, modular CSS, module variant, selector specificity

10.4.2 Reducing code duplication

This subsection uses a dark-mode example to show how container style queries can share CSS across multiple theme triggers without duplicating rules, while noting browser-support limits.

Key terms: style query, custom property, code duplication, prefers-color-scheme, --color-theme, data-theme, light mode, dark mode, container style queries, progressive enhancement

10.5 Summary

Concepts explained in this chapter

← Chapter 9: Modular CSS and scope Chapter 11: Color and contrast →

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.