Font Size and Scale — UI/UX

Use a type scale, a set of font sizes calculated with the formula size = base × ratio to the power of the step. For example, ratio 1.25 from 16 gives 20, 25…

Open the CSS of a project that has been running for a year and search for font-size (the CSS rule for the size of the letters). You will almost certainly find 13px, 14px, 15px, 17px, 22px and 23px. Each number was added by a different person because it looked about right. Now nobody is sure whether the title of a card should be 22 or 23.

The cure is a type scale: a set of font sizes calculated in advance from one base size and one ratio (a number you multiply by). So you don't pick a random size every time you write CSS. Think of clothing sizes: S, M, L, XL. Every shop has a few fixed sizes that go up evenly, and nobody makes a "half M" for one customer. A type scale works the same way: your page only needs 5 to 7 fixed font sizes, and they are all related because they come from the same numbers.

Base size: 16px

Everything starts from the size of the body text (the main text, such as paragraphs). 16px is the browser's default font size, and it is also the ideal size for body text. Don't go smaller for body text: 14px text on a phone already makes people squint. On an iPhone, if an input field (a box you type into) has a font smaller than 16px, Safari even enlarges the page automatically (zooms in) when the field is tapped.

Calculating a type scale

The formula: size = base size × ratio to the power of the step. Step 1 is one level above the base size, step 2 is two levels, and so on. Going up one step means multiplying by the ratio once more. Here are the results from 16px for five common ratios (their names are borrowed from the distances between notes in music):

Ratio Name Step 1 Step 2 Step 3
1.125 Major Second 18 20.25 22.78
1.2 Minor Third 19.2 23.04 27.65
1.25 Major Third 20 25 31.25
1.333 Perfect Fourth 21.33 28.43 37.9
1.5 Perfect Fifth 24 36 54

Take ratio 1.25: 16 × 1.25 = 20, then 20 × 1.25 = 25, then 25 × 1.25 = 31.25. A small ratio (1.125) suits dense pages such as dashboards (pages full of numbers and tables). A big ratio (1.5) makes headings stand out strongly. It suits pages with little text, like Mrs. Sri's booking page.

rem, not px

Here is a ratio 1.5 type scale from 16px for the top of the Mist Homestay page. It has a heading (<h1>), a subheading (<h2>) and a paragraph (<p>), then the CSS inside a <style> tag. The sizes are written in rem:

<h1>Mist Homestay</h1>
<h2>Terrace Room</h2>
<p>A first-floor room with a wooden terrace facing the potato garden.</p>

<style>
  h1 { font-size: 2.25rem; font-weight: 700; } /* 16 × 1.5 × 1.5 = 36px */
  h2 { font-size: 1.5rem; font-weight: 600; }  /* 16 × 1.5 = 24px */
  p { font-size: 1rem; line-height: 1.6; }     /* 16px, each line 25.6px */
</style>

Before going on, take a guess. A guest raises the default font size in their browser settings from 16px to 20px. How many px is the title written as font-size: 2.25rem in that code now?

Why rem? Because rem follows the user's setting. If a guest raises the browser's default font size to 20px, 1rem becomes 20px too. Every size grows in proportion: p becomes 20px, h2 30px, and h1 2.25 × 20 = 45px. That is the answer to the guess. Sizes written in px don't change, so users who need bigger text get no help. (Browser zoom is different: zoom enlarges px and rem alike.)

Line height and line length

The font size is only half of what matters. Two more things decide whether text is comfortable to read:

Limit the number of sizes: 5 to 7

Most websites need only 5 to 7 different sizes. Mrs. Sri's page uses four: 14px (the check-in note), 16px (the body), 24px (the room name), 36px (the homestay name). Keep the sizes as CSS variables (names that hold one value, starting with --) so the whole project uses the same numbers. Variables on :root (the <html> element) can be used on the whole page:

:root {
  --text-sm: 0.875rem; /* 14px */
  --text-base: 1rem;   /* 16px */
  --text-lg: 1.5rem;   /* 24px */
  --text-xl: 2.25rem;  /* 36px */
}

⚠️ Numbers that quietly break your scale

TL;DR: Use a type scale, a set of font sizes calculated with the formula size = base × ratio to the power of the step. For example, ratio 1.25 from 16 gives 20, 25, 31.25, and ratio 1.5 gives 24 and 36. The base size for body text (the main text) is 16px: the browser default, equal to 1rem. Write font sizes in rem, a unit relative to the font size of the <html> element (the outermost element of the page), so they follow the user's setting: with a 20px default, 2.25rem becomes 45px. Give body text a line height of 1.5 to 1.7 without a unit, and a line length of 45 to 75 characters (max-width: 65ch). 5 to 7 sizes per website is enough.

⚡ One base, one ratio

Meaning: A type scale is a set of font sizes calculated with the formula size = base × ratio to the power of the step. Going up one step means multiplying by the ratio once more. From 16px with ratio (multiplier) 1.25 you get 20, 25, 31.25. Ratio 1.5 gives 24 and 36. Every new size comes from the scale, not from a guess.

⚡ Write it in rem

Write:

<h1>Mist Homestay</h1>
<h2>Terrace Room</h2>
<p>A first-floor room with a wooden terrace facing the potato garden.</p>

<style>
  h1 { font-size: 2.25rem; font-weight: 700; } /* 16 × 1.5 × 1.5 = 36px */
  h2 { font-size: 1.5rem; font-weight: 600; }  /* 16 × 1.5 = 24px */
  p { font-size: 1rem; line-height: 1.6; }     /* 16px, each line 25.6px */
</style>
Guess first: 1rem equals the browser's default font size, usually 16px. A guest raises that size to 20px. How many px is the title with font-size: 2.25rem now?

45px, because 2.25 × 20. Sizes written in px don't change at all.

Meaning: rem is a multiple of the font size of <html> (the outermost element of the page), 16px by default. To turn px into rem, divide by 16: 36px = 2.25rem. In this code, font-size sets the size of the letters, font-weight how heavy they are, and line-height the height of a line.

⚡ Lines that are easy to read

Meaning: Body text (the main text) gets a line height of 1.5 to 1.7, written without a unit. Its line length is 45 to 75 characters: limit it with max-width: 65ch (ch is the width of one "0" character).

⚡ 5 to 7 sizes is enough

Write:

:root {
  --text-sm: 0.875rem; /* 14px */
  --text-base: 1rem;   /* 16px */
  --text-lg: 1.5rem;   /* 24px */
  --text-xl: 2.25rem;  /* 36px */
}

Meaning: Most websites only need 5 to 7 font sizes. Keep them as CSS variables (names that hold one value, such as --text-base) so the whole project uses the same numbers. Variables on :root (the <html> element) can be used on the whole page.

What you will learn