Sub topic 3
Building a Simple Type Scale
This chapter assumes you know basic HTML elements, CSS selectors, and the properties font-size, font-weight, and color. Here you will define a small set of text sizes and apply them to headings, body text, captions, and labels.
What a type scale is
A type scale is a fixed list of font sizes that you reuse across a page. Instead of picking a new size for every element, you choose from the list. This gives you two benefits:
- Sizes differ by clear steps, so readers can tell a heading from body text at a glance.
- The page looks consistent, because the same role always gets the same size.
A scale of five or six sizes covers most pages. More than that usually means you are choosing sizes by feel instead of by role.
Why use rem units
Pixels (px) fix a size regardless of the reader's settings. The rem unit is relative to the root element's font size, which defaults to 16px in most browsers. If a reader raises their default text size in the browser, every rem value grows with it.
| Value | Computed size at the default 16px root |
|---|---|
| `0.875rem` | 14px |
| `1rem` | 16px |
| `1.25rem` | 20px |
| `1.5rem` | 24px |
| `2rem` | 32px |
Avoid setting html { font-size: 62.5%; } or a pixel value on the root. Both change what 1rem means and can override the reader's preference.
Defining the scale
Store each size in a CSS custom property. A custom property is a named value, written with a leading --, that you read with var(). Declaring them on :root makes them available everywhere on the page.
:root {
--text-sm: 0.875rem;
--text-base: 1rem;
--text-lg: 1.25rem;
--text-xl: 1.5rem;
--text-2xl: 2rem;
--leading-body: 1.5;
--leading-heading: 1.2;
}The names describe position on the scale (small, base, large), not the role. That keeps the scale reusable: the same --text-lg could style an h3 today and a lead paragraph tomorrow.
The line-height values have no unit. A unitless line-height is multiplied by each element's own font size, so it stays proportional when sizes change.
Applying the scale to roles
Now map each text role to a step. The headings get the larger sizes and a tighter line height. Body text gets the base size and a looser line height.
body {
font-size: var(--text-base);
line-height: var(--leading-body);
}
h1 { font-size: var(--text-2xl); line-height: var(--leading-heading); }
h2 { font-size: var(--text-xl); line-height: var(--leading-heading); }
h3 { font-size: var(--text-lg); line-height: var(--leading-heading); }
p { max-width: 65ch; }
figcaption {
font-size: var(--text-sm);
color: #595959;
}Notice that p does not set a font size. It inherits 1rem from body. Setting a size only where a role differs from the default keeps the stylesheet short.
The 65ch limit on paragraphs keeps lines from growing too long to read comfortably. A ch is roughly the width of the digit "0" in the current font.
Labels fit into the same scale. They stay at the base size and gain weight, so they read as names for their fields, not as headings.
label {
display: block;
font-size: var(--text-base);
font-weight: 600;
margin-bottom: 0.25rem;
}How the scale maps to roles
Common errors
- Skipping steps for emphasis. If
h2andh3are only a few pixels apart, the difference looks like a mistake. Keep steps visibly distinct. - Choosing the element for its size. Do not use an
h4because you like its default size. Pick the element for its meaning, then set its size with the scale. - Using
pxfor font sizes. Pixel values ignore the reader's default text size setting. - Using a unit on
line-height. A value like1.5remis fixed, and it can overlap lines when a child element has a larger font. - Making captions too small. Below about
0.875rem, text becomes hard to read for many people. Also check that the caption color has enough contrast, which a later chapter covers.
Try it
Apply the scale to this markup and open it in a browser. Then raise your browser's default font size and confirm that every piece of text grows.
<main>
<h1>Trail Guide</h1>
<p>A short introduction to planning a multi-day hike.</p>
<h2>Gear</h2>
<p>Start with footwear, then add layers.</p>
<figure>
<img src="boots.jpg" alt="A pair of leather hiking boots on a rock">
<figcaption>Leather boots after a week on the trail.</figcaption>
</figure>
<h2>Food</h2>
<h3>Breakfast</h3>
<p>Oats keep well and cook quickly.</p>
</main>Exercise
Why does the scale use rem units and a unitless line-height instead of px and a fixed line height?
Show answer
A rem size is relative to the root font size, so it grows when the reader raises their browser's default text size, while px stays fixed. A unitless line-height is multiplied by each element's own font size, so line spacing stays proportional when sizes change, while a fixed length does not.
Exercise
A teammate wants a caption to look like the h3 size, so they change the figcaption to an h3. What should they do instead?
Show answer
Keep the figcaption element, because it expresses the role of the text, and set its size with font-size: var(--text-lg); in CSS. The element describes meaning and the scale describes appearance.