CSS fonts and text: font-family, size, weight and line-height
Make text comfortable to read with font stacks, web fonts, sizes, weights, line height and sensible line length.
In this lesson you will
- Write font stacks with fallbacks
- Add a web font
- Set size, weight and line height
- Keep lines a readable length
About 14 minutes
Text is most of the web
People read, far more than they look. Most pages are 90 percent words. That is why nothing improves a plain page faster than good typography: a comfortable font, sensible sizes and room between the lines. Think of a printed novel. Nobody notices the type, and that is the point. Good type simply disappears so the words can be read.
font-family and fallbacks
body {
font-family: "Manrope", Arial, sans-serif;
}
Computers can only use fonts they have. So you write a stack, a list of fonts in order of preference. The browser uses the first one it can find. The last item should be a generic family, which always exists: serif (with little feet), sans-serif (without), monospace (every letter the same width) or cursive. Names with spaces go in quotes.
Adding a web font
To use a font that visitors do not have installed, load it from a font service. Google Fonts is free. You pick a font, copy the link it gives you into your head, and then use the name in your CSS.
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Manrope:wght@400;700&display=swap" rel="stylesheet">
Only request the weights you need, such as 400 and 700. Every extra weight is more to download. Two font families, one for headings and one for body text, are enough for nearly any design.
Font size and units
p { font-size: 1rem; } /* 16px by default */
h1 { font-size: 2.5rem; } /* 40px */
Sizes can be written in pixels (px), but a flexible unit is kinder. rem means “times the root font size”, which is 16 pixels in most browsers. Because rem respects the visitor’s own settings, someone who sets their browser to larger text gets larger text on your site too. You will learn about all units in a later lesson. For now, remember: body text at 1rem to 1.125rem is comfortable.
Weight and style
h1 { font-weight: 800; }
.light { font-weight: 300; }
em { font-style: italic; }
font-weight takes numbers from 100 (thin) to 900 (black), where 400 is normal and 700 is bold. The words normal and bold work too. font-style: italic slants text. You can only use weights that the font actually provides.
Line height, the secret of readability
p { line-height: 1.6; }
Line height is the vertical room given to each line. Write it as a plain number with no unit, which means “times the font size”. Between 1.5 and 1.7 suits body text. Tight, cramped lines are tiring, and very loose ones fall apart. Headings usually want less, around 1.2.
Make the paragraph easier to read: set a font-size of 1.125rem, a line-height of 1.7 and a darker grey colour. Compare it to the cramped paragraph above.
<style>
.cramped { line-height: 1; font-size: 14px; }
.better { /* your rules here */ }
body { font-family: Arial, sans-serif; max-width: 460px; }
</style>
<p class="cramped">Cramped text is tiring to read, especially when it goes on for many lines in a row without any breathing room at all.</p>
<p class="better">Comfortable text is far easier to read, especially when it goes on for many lines in a row with plenty of breathing room.</p>
Add font-size: 1.125rem; line-height: 1.7; color: #3B3858; to .better.
<style>
.cramped { line-height: 1; font-size: 14px; }
.better { font-size: 1.125rem; line-height: 1.7; color: #3B3858; }
body { font-family: Arial, sans-serif; max-width: 460px; }
</style>
<p class="cramped">Cramped text is tiring to read, especially when it goes on for many lines in a row without any breathing room at all.</p>
<p class="better">Comfortable text is far easier to read, especially when it goes on for many lines in a row with plenty of breathing room.</p>
Alignment, decoration and spacing
text-alignacceptsleft,center,rightandjustify. Left alignment is easiest to read in left-to-right languages.text-decorationadds or removes underlines:none,underline,line-through.text-transformchanges case:uppercase,lowercaseorcapitalize.letter-spacingadds space between letters. A touch looks good on small uppercase labels.max-widthon a text block, such as65ch, keeps lines from becoming too long to follow.
.eyebrow {
text-transform: uppercase;
letter-spacing: .08em;
font-size: .8rem;
font-weight: 700;
}
article p { max-width: 65ch; }
The ch unit is roughly the width of the zero character. A line of about 60 to 75 characters is the comfortable length for reading. Beyond about 90, eyes struggle to find the start of the next line.
The font shorthand
h2 { font: 700 1.75rem/1.2 "Sora", sans-serif; }
The shorthand packs several properties into one. It is compact, but easy to get wrong, so many developers write the long form while learning.
Removing the underline from every link with text-decoration: none and giving them no other clue makes links invisible inside paragraphs. If you remove underlines, make sure links differ in colour and weight, and show a clear hover and focus style.
Practise: a heading and body pair
Give the heading a bold serif font (Georgia), the paragraph a sans-serif font, a line-height of 1.7 and a maximum width of 60ch.
<style>
h1 { }
p { }
</style>
<h1>The art of slow baking</h1>
<p>Good bread takes time. The longer the dough rests, the more flavour it develops, and the easier it is to digest for many people. Plan your day around the dough, not the other way round.</p>
h1 { font-family: Georgia, serif; font-weight: 800; } p { font-family: Arial, sans-serif; line-height: 1.7; max-width: 60ch; }
<style>
h1 { font-family: Georgia, serif; font-weight: 800; }
p { font-family: Arial, sans-serif; line-height: 1.7; max-width: 60ch; }
</style>
<h1>The art of slow baking</h1>
<p>Good bread takes time. The longer the dough rests, the more flavour it develops, and the easier it is to digest for many people. Plan your day around the dough, not the other way round.</p>
A serif heading with a sans-serif body is a classic pairing. Contrast between the two fonts makes the page feel designed rather than accidental.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
Why write a font stack with several fonts?
- To load them all
- So a fallback is used when the first is missing
- To make text larger
- To change the colour
The browser uses the first font it can find.
Which is a generic font family?
- Manrope
- Arial
- sans-serif
- Sora
sans-serif always exists on every device.
What does line-height: 1.6 mean?
- 1.6 pixels
- 1.6 times the font size
- 1.6 percent
- Sixteen lines
A unitless number multiplies the font size.
Which unit respects the visitor’s own text size setting?
pxremptcm
rem is relative to the root font size.
What helps keep text lines a comfortable length?
- A tiny font
max-widthin ch units- Capital letters
- Justified text
A max-width of around 65ch keeps lines readable.
Summary
- Use a font stack ending in a generic family.
- rem sizes and a line-height near 1.6 give comfortable body text.
- Limit fonts and weights.
- Keep lines around 65 characters.
Questions
How many fonts should a site use?
One or two families. Many fonts make a page look messy and slow it down.
Are Google Fonts free for commercial use?
Yes, the fonts there are open licensed, but check the licence of each font family you pick.
Why does my font look different on another computer?
Without loading the font, a device substitutes its own. Load a web font to make it consistent.
