Sub topic 7
Set typography and color
This chapter covers the text and color choices that give the page its look. It assumes you can save files, open an HTML file in a browser, and write a basic CSS rule with a selector and declarations. It also assumes you have linked styles.css to your page. Typography and color come before custom properties and accessibility in this tutorial. The choices you make here are the ones you will later store in variables and check for contrast.
Choose a font stack
The font-family property takes a list of fonts called a font stack. The browser uses the first font it finds on the visitor's device. If none is found, it uses the last entry, a generic family such as sans-serif.
The system-ui keyword picks the operating system's own interface font. It loads instantly because nothing is downloaded, which suits a small personal site.
body {
font-family: system-ui, sans-serif;
font-size: 1rem;
line-height: 1.6;
}Size and spacing for readable text
Use rem units for font sizes. One rem equals the root font size, which is 16px by default. If a visitor raises their browser's default text size, everything sized in rem grows with it. Sizes written in px ignore that setting.
Set line-height as a number with no unit. A value of 1.6 means 1.6 times the element's own font size. Unitless values are inherited as the multiplier, so each child element calculates its own line height. Body text usually reads well between 1.4 and 1.7.
Keep lines from running too wide. Around 60 to 75 characters per line is easy to read. The max-width: 60rem on main from the previous chapters is a good start. For a tighter column, apply max-width: 65ch to paragraphs. The ch unit is about the width of the digit 0 in the current font.
h1 {
font-size: 2.5rem;
line-height: 1.2;
}
h2 {
font-size: 1.75rem;
line-height: 1.3;
}
p {
max-width: 65ch;
}Set color
Three properties cover most color work:
colorsets the text color.background-color(or thebackgroundshorthand) sets the fill behind an element.border-colorsets the border, though it only shows when a border style exists.
CSS accepts several color formats. Hex codes such as #1f2933 are the most common in design tools. Named colors like tomato are quick for tests. The rgb() and hsl() functions are also valid. HSL stands for hue, saturation and lightness, and it is easy to adjust by hand: raise or lower the lightness value to get a lighter or darker shade of the same color.
a {
color: #0b5cad;
text-decoration-thickness: 0.1em;
text-underline-offset: 0.2em;
}
a:hover {
color: hsl(210, 85%, 25%);
}Pick a small palette: one text color, one background, and one accent for links and highlights. A limited palette looks deliberate, and it is easy to move into custom properties in the next chapter.
Common errors
- Low contrast. Light gray text on white looks refined on your screen and is hard to read for many people. Check contrast before you commit to a color pair. The accessibility chapter explains the numbers.
- Unit-based line height. Writing
line-height: 1.6remfixes the height in absolute terms, and large headings can overlap their own lines. Use a unitless number. - Removing link underlines. Links that differ only by color are hard to spot for color-blind visitors. Keep the underline or add another visible cue.
- Too many fonts. More than two font families makes a page look inconsistent.
Exercise
A paragraph has font-size: 1.25rem and line-height: 1.5. A <small> element inside it has font-size: 0.8rem. What line height, in rem, does the text inside <small> use, and why would line-height: 1.5rem on the paragraph give a different result?
Show answer
Because 1.5 is unitless, the <small> element inherits the multiplier and calculates 1.5 × 0.8rem = 1.2rem. With line-height: 1.5rem on the paragraph, the computed length of 1.5rem is inherited as-is, so the <small> text would use 1.5rem, which is more spacing than its size calls for.
Exercise
Add rules to your own styles.css so that h1 uses a 2.5rem size with a line height of 1.2, links are underlined with a 0.2em offset, and paragraphs never exceed 65ch. Which unit do you use for each of the three values?
Show answer
Use rem for the heading size (font-size: 2.5rem), a unitless number for line height (line-height: 1.2), em for the underline offset (text-underline-offset: 0.2em), and ch for the paragraph width (max-width: 65ch).