liveBook · Manning

Chapter 3: Document flow and the box model

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

The introduction explains why document flow and the box model are foundational to understanding page layout.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why document flow and the box model are foundational to understanding page layout.

Key terms: document flow, box model, layout

3.1 Normal document flow

Introduces normal document flow and explains why width and height behave differently in a page layout.

Key terms: normal document flow, inline elements, block-level elements, constrained width, unlimited height, parent element, child elements

3.1.1 Centering content horizontally

This section shows how to center a page’s main content and align its header using constrained inner containers and automatic horizontal margins.

Key terms: higher-level DOM elements, outside in, double-container pattern, auto, container, custom property, max-width, viewport, horizontal scrolling

3.1.2 Using logical properties

This section explains how CSS logical properties describe size and spacing relative to writing direction, so layouts can adapt to different languages and writing modes.

Key terms: logical properties, inline base direction, block flow direction, inline-size, block-size, writing modes, start, end, padding-inline-start, padding-inline-end

3.1.3 Adopting useful shorthand logical properties

This section shows how logical shorthand properties simplify axis-specific CSS without changing the page’s appearance.

Key terms: margin-inline, margin-block, start, end, padding-inline, padding-block, double-container pattern, max-width, max-inline-size, authoring convenience

3.2 The box model

This section explains how the default box model determines an element’s rendered size, then distinguishes outlines and a special case for inline elements.

Key terms: box model, content area, padding area, border area, margin area, outline, border, outline-offset, inline elements, line-height

3.2.1 Avoiding magic numbers

The section explains why guessing a smaller width is unreliable and shows how to account for padding with a calculated width instead.

Key terms: magic number, calc(), padding, --columns-width

3.2.2 Adjusting the box model

Explains how changing box-sizing from content-box to border-box makes specified dimensions include padding and borders, correcting a heading’s alignment.

Key terms: box-sizing, content-box, border-box, padding, border, width

3.2.3 Using universal border box sizing

This section recommends a universal border-box rule to make element sizing predictable, while warning that adding it to an existing stylesheet can cause bugs.

Key terms: universal selector (*), pseudo-element, border-box fix, default content box model, existing stylesheet

3.3 Element height

Explains why element height is usually content-driven in normal document flow and why explicit heights can be troublesome.

Key terms: normal document flow, height, block size, explicit height

3.3.1 Controlling overflow behavior

This section explains why constrained content can overflow, how CSS controls its visibility and scrolling, and why nested scrollbars should be used carefully.

Key terms: overflow, document flow, visible, hidden, clip, scroll, auto, horizontal overflow, vertical overflow, overflow-x

3.3.2 Using alternatives to percentage-based heights

Explains why percentage-based heights often fail and identifies better approaches for filling a screen and making equal-height columns.

Key terms: percentage-based heights, containing block, explicitly defined height, viewport-relative units, 100 svh/lvh, height: 100%, flexbox, grid

3.3.3 Using min-height and max-height

This section explains how minimum and maximum size properties let elements size naturally within limits, and what happens when content exceeds those limits.

Key terms: min-height, min-block-size, overflow, max-height, max-block-size, min-width, max-width, min-inline-size, max-inline-size

3.4 Negative margins

Explains how negative margins affect an element or nearby content, and how they can make a block-level element extend beyond its container.

Key terms: negative margin, document flow, inline elements, block-level element, negative right margin, negative left margin, bleed, container, browser viewport, negative margins

3.5 Collapsed margins

This section introduces collapsed margins as the reason a gap can appear below a header even when no margin was applied to the header or its container.

Key terms: adjoining margins, collapsing, top and bottom margins

3.5.1 Collapsing between text

This section explains why margins between adjacent blocks of text overlap and how to determine the resulting gap.

Key terms: collapsed margins, top margin, bottom margin

3.5.2 Collapsing multiple margins

This section explains how multiple adjacent margins collapse across nested elements and why that makes vertical spacing predictable.

Key terms: adjacent top and bottom margins, collapsed margins, collapsed margin, bottom margin, top margin

3.5.3 Collapsing outside a container

Explains why a heading’s margins can collapse outside its header and how to prevent the resulting spacing problems.

Key terms: margin collapsing, container, user-agent styles, padding, overflow: auto, border, flexbox, grid layout

3.6 Spacing elements within a container

The section shows how to space a series of links inside a padded container without adding unwanted space above the first link.

Key terms: container padding, margin-top, adjacent sibling combinator (+), button-link, block elements, container

3.6.1 Considering changing content

This subsection shows why adding margins to individual sidebar items becomes fragile when the content changes.

Key terms: top margin, sidebar, container

3.6.2 Creating a more general solution

The section shows how to space elements vertically with a reusable container class instead of setting margins for the page’s current contents.

Key terms: lobotomized owl selector, universal selector, adjacent sibling combinator, stack, child combinator, top margins, direct descendants

3.7 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 2: Working with relative units Chapter 4: Flexbox →

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.