liveBook · Manning

Chapter 2: Working with relative units

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

Introduces CSS lengths and explains the basic difference between absolute and relative units.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces CSS lengths and explains the basic difference between absolute and relative units.

Key terms: absolute unit, relative unit, pixels, em, rem, length, percentages

2.1 The power of relative units

The section explains why web layouts cannot assume fixed dimensions: CSS is combined with content after authoring, and users can change how a page renders.

Key terms: late binding, stylesheet, final rendering, constraints, layout

2.1.1 The rise of responsive design

This section explains why CSS must adapt to changing screen sizes, how responsive design addresses that need, and how relative units help.

Key terms: browser window, rendered, constraints, responsive design, relative units, base font size, pixel (px), CSS pixel, absolute length units

2.2 Ems and rems

This section explains how em lengths are computed from an element’s font size and how they let related dimensions scale together.

Key terms: em, font size, computed value, ems, padding, border radius, size modifiers

2.2.1 Using ems to define font-size

This section explains how em-based font sizes are calculated, why ems behave differently on other properties, and how nested em-based font sizes can compound.

Key terms: em, font-size, inherited font size, padding, current element’s font size, nested lists, compounding

2.2.2 Using rems for font-size

This section explains how rem-based font sizes use the root element as a stable reference, when to choose rems over other units, and why relative font sizes matter for accessibility.

Key terms: rem, root em, root element, computed value, Document Object Model (DOM), root node, <html>, :root, specificity, rems

2.3 Stop thinking in pixels

This section argues against resetting the root font size to simplify pixel-to-rem conversion and encourages choosing relative sizes by how they relate and look rather than by their exact pixel equivalents.

Key terms: root font size, 62.5%, rem, pixels, ems, inherited font size, base font size, rems

2.3.1 Setting a sane default font size

This section sets a page-wide font size at the root, then uses that inherited size and relative units to style a panel.

Key terms: root, <html>, ems, inherited value, browser’s default, rem, px, panel-body, root font size, direct descendant combinator

2.3.2 Making the panel responsive

The section shows how media queries can change the root font size so a panel styled with relative units scales across screen sizes without changing the panel’s styles.

Key terms: media query, @media rule, root, em, rem, base font size, relative units, pixels, media queries

2.3.3 Resizing a single component

The section shows how to resize one panel with relative units and explains why CSS is a living standard rather than a single numbered specification.

Key terms: ems, rems, font-size, panel, large class, border radius, living standard, modules, CSS3, W3C specifications

2.4 Viewport-relative units

Introduces viewport-relative CSS units, shows how they size elements against the visible browser area, and explains why mobile viewport changes complicated the use of vh.

Key terms: viewport, viewport-relative units, vh, vw, vmin, vmax, largest possible viewport size, layout thrashing

2.4.1 Selecting from the newer viewport units

This section explains how to choose among large, small, and dynamic viewport units, including their trade-offs and limitations.

Key terms: large viewport, small viewport, lvh, svh, lvw, svw, dynamic viewport, dvw, dvh, dvmin

2.4.2 Using viewport units for font size

This section shows how to make font sizes scale smoothly with the viewport while keeping text within usable, accessible bounds.

Key terms: viewport-relative units, svw, breakpoint, calc(), ems, rems, font settings, clamp(), minimum value, preferred value

2.5 Unitless numbers and line-height

This section explains where CSS permits unitless numbers and why a unitless line-height usually behaves better when inherited.

Key terms: unitless values, unitless 0, length values, line-height, unitless number, computed value, inheritance

2.6 Custom properties (aka CSS variables)

This section shows how to declare and use CSS custom properties to reuse values, and explains fallback and invalid-value behavior.

Key terms: custom property, var(), --main-font, :root, single source of truth, fallback value, --secondary-color, invalid value, initial value, padding

2.6.1 Changing custom properties dynamically

The section shows how inherited custom properties let the same panel styling produce different colors in different parts of a page.

Key terms: custom properties, :root, cascade, inherit, dark container

2.7 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 1: Cascade, specificity, and inheritance Chapter 3: Document flow and the box model →

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.