Sub topic 3
Building a simple type scale
This chapter assumes you can write basic HTML and CSS selectors. It also assumes you know what font-size, font-weight, and color do. The previous chapters covered why text needs a hierarchy and why you pick elements by role. Here you decide which sizes the page uses and how to express them in CSS.
What a type scale is
A type scale is a short, fixed list of font sizes. Every piece of text on the page takes its size from that list. You do not choose a new number each time you style something.
Without a scale, sizes pile up one at a time: 15px for a caption, 17px for a label, 19px for a lead paragraph, 22px for a subheading. These sizes are close enough that readers can't tell them apart, and different enough that the page looks uneven. A scale fixes this in two ways:
- Each step is clearly larger than the one before it, so sizes carry meaning.
- You reuse the same few values everywhere, so similar elements look the same.
A scale supports the hierarchy from the first chapter. Size is one of the main signals of importance, and it only works if readers can see a difference between levels.
Pixels, em, and rem
A scale has to be written in some unit. The choice affects whether users can resize your text.
| Unit | Relative to | Behavior |
|---|---|---|
| `px` | A fixed CSS pixel | Ignores the browser's default font-size setting |
| `em` | The font size of the current element's parent (or the element itself, when used in `font-size`'s sibling properties like `padding`) | Compounds when nested |
| `rem` | The font size of the root `html` element | Same value everywhere, scales with the user's setting |
The rem unit is the usual choice for a scale. Browsers set the root font size to 16px by default, so 1rem is 16px, 1.25rem is 20px, and 0.875rem is 14px. A user who raises the default size to 20px in their browser settings gets 1rem = 20px, and every size in your scale grows proportionally.
Two details are easy to get wrong.
First, em in a font-size declaration is relative to the parent's font size. That is why it compounds. Here is a small page that shows it:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>em compounding</title>
<style>
ul { font-size: 1.2em; }
</style>
</head>
<body>
<ul>
<li>Level one (19.2px)
<ul>
<li>Level two (23.04px)
<ul>
<li>Level three (27.65px)</li>
</ul>
</li>
</ul>
</li>
</ul>
</body>
</html>Each nested list multiplies the size by 1.2 again, so the text grows at every level. With rem, all three levels would be the same size. This is the main reason scales use rem for font sizes.
Second, a font size in px does not always block zooming. Browser zoom (Ctrl and +) scales pixels too. What a px value blocks is the user's default font size preference, which many people with low vision set instead of zooming. A rem-based scale respects both.
Choosing a base size
The base size is the size of ordinary paragraph text, and it anchors the whole scale. Use 1rem (16px by default). Body text below about 16px strains many readers, and going much larger leaves little room to separate body text from headings.
Set it once on body and leave html alone:
body {
font-size: 1rem;
}Setting html { font-size: 100% } is also fine. It means the same thing as the default and makes the intent explicit. Never put a pixel value on html.
Choosing the steps
There are two common ways to produce the other sizes.
Multiply by a ratio. Pick a ratio, then multiply the base repeatedly to go up and divide to go down. Common ratios have names from music theory:
| Ratio | Name | Feel |
|---|---|---|
| 1.125 | Major second | Subtle, good for dense interfaces |
| 1.200 | Minor third | Moderate, good for most content pages |
| 1.250 | Major third | Clear contrast, good for articles |
| 1.333 | Perfect fourth | Strong contrast, good for marketing pages |
With a base of 1rem and a ratio of 1.25, the steps are:
| Step | Calculation | Size in rem | Size at 16px |
|---|---|---|---|
| -1 | 1 / 1.25 | 0.8 | 12.8px |
| 0 | 1 | 1 | 16px |
| 1 | 1 × 1.25 | 1.25 | 20px |
| 2 | 1.25 × 1.25 | 1.5625 | 25px |
| 3 | 1.5625 × 1.25 | 1.953 | 31.25px |
| 4 | 1.953 × 1.25 | 2.441 | 39.06px |
A ratio gives you a consistent rhythm without having to judge each size by eye.
Pick values by hand. For a small site you don't need every step. The scale used in this tutorial's examples was chosen by hand and rounded to friendly numbers:
| Token | Size | Pixels at default | Ratio to previous |
|---|---|---|---|
| `--text-sm` | 0.875rem | 14px | n/a |
| `--text-base` | 1rem | 16px | 1.14 |
| `--text-lg` | 1.25rem | 20px | 1.25 |
| `--text-xl` | 1.5rem | 24px | 1.2 |
| `--text-2xl` | 2rem | 32px | 1.33 |
The ratios between steps are not identical, and that is acceptable. What matters is that each step is visibly larger than the last and that you stop at five or six sizes. Sizes that differ by less than about 1.1 times are hard to tell apart, so avoid them.
Naming the steps
Name each size by where it sits on the scale, not by which element uses it. The scale above uses --text-sm, --text-base, --text-lg, --text-xl, and --text-2xl.
Avoid names such as --caption-size or --h2-size. Those names tie a size to one role. When the design changes and captions need the same size as labels, the name becomes misleading. A size name describes the size. The mapping to roles belongs in the selectors, where it can change without renaming anything.
Chapter 9 covers custom properties as a way to keep styles consistent. For now, treat them as named values you can reuse.
Putting the scale to work
Define the sizes once in :root, then use them in the rules for each role. This is the full stylesheet:
: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;
}
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 what the stylesheet does and does not do:
- Heading levels map to scale steps in order: h1 is the largest, h3 is one step above body text.
- Body text sits on the base step. Captions sit one step below it.
- No rule contains a raw size such as
1.3rem. If a size is not on the scale, you either pick the nearest step or add a step deliberately. - Line height is unitless (
1.5, not24px). A unitless value is multiplied by each element's own font size, so a heading with a different size gets the right spacing automatically. Headings get a tighter value because large text needs less extra space between lines. Chapter 5 returns to line height for body text.
Here is the markup these rules style, from the trail-guide example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Trail Guide</title>
<link rel="stylesheet" href="scale.css">
</head>
<body>
<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>
</body>
</html>Save the CSS as scale.css next to the HTML file and open the page. You should see a 32px title, 24px section headings, a 20px subsection heading, 16px paragraphs, and a 14px grey caption.
Common errors
Styling the element, not the role. An h3 is not "the small heading". If the design needs a heading that looks smaller than its level, change the CSS, not the element. Skipping from h2 to h4 to get a smaller size breaks the outline that screen reader users navigate by. The previous chapter covered this split.
Mixing units. Using rem for headings and px for captions means the captions stay put when a user raises their default size, while the headings grow. The hierarchy gets distorted at larger settings. Use rem for every font size.
Too many steps. Eight or nine sizes usually means some are only slightly different. If you can't explain why two sizes both exist, drop one.
Steps that are too close. A 1rem body next to a 1.05rem lead paragraph looks like a mistake, not a choice. Make the difference clear or use weight or color instead.
Using size for everything. Size is one signal among several. Weight, spacing, and color also separate roles, and they let you keep the number of sizes small. Labels, for example, are body-sized but bolder, and a later chapter covers them.
Fixed px on html or body. This overrides the user's default size and defeats the point of using rem below it.
Limits of a simple scale
This scale has one fixed set of sizes. Large headings that fit a wide desktop screen may be too big on a narrow phone, where a 32px title wraps onto several lines. A later chapter on responsive text covers clamp(), which lets sizes change between a minimum and maximum. The scale you build here still decides which sizes exist and what they are called. Responsive techniques only adjust the values behind those names.
Exercise
A base size of 1rem and a ratio of 1.2 give a scale. Calculate the sizes for steps 1, 2, and 3 in rem, rounded to three decimal places, and give each in pixels at the default 16px root size.
Show answer
Step 1 is 1 × 1.2 = 1.2rem, or 19.2px. Step 2 is 1.2 × 1.2 = 1.44rem, or 23.04px. Step 3 is 1.44 × 1.2 = 1.728rem, or 27.648px.
Exercise
A stylesheet sets html { font-size: 16px; } and every heading and paragraph size in rem. A user changes their browser's default font size to 24px, but nothing on the page changes size. What is the cause, and what is the fix?
Show answer
The px value on html overrides the browser's default, so 1rem stays 16px no matter what the user sets. Remove the declaration, or replace it with font-size: 100%, so the root follows the user's setting and every rem value scales with it.
Exercise
A nested menu has ul { font-size: 1.1em; }. The second-level list looks larger than the first. Explain why, and give a rule that avoids the problem.
Show answer
An em font size is relative to the parent's font size, so the nested list multiplies 1.1 on top of the already enlarged parent (1.1 × 1.1 = 1.21 times the base). Use rem, as in ul { font-size: 1.1rem; }, or reset nested lists with ul ul { font-size: 1em; }. With rem, every level gets the same size.