Lesson 15 of 29 · CSS Basics
CSS Text and Fonts: font-family, font-size and line-height
Most of the web is text, so typography — the choice of fonts, sizes and spacing — decides whether people enjoy reading your page or give up after one paragraph. A handful of CSS properties make all the difference.
Choosing a font: font-family
font-family takes a font stack: a list of fonts separated by commas. The browser uses the first one that is installed on the visitor's device, so always end the list with a generic family — serif, sans-serif or monospace — as a safety net:
body {
font-family: Georgia, "Times New Roman", serif;
}
code {
font-family: Consolas, "Courier New", monospace;
}
Put font names that contain spaces in quotes. Because font-family is inherited, setting it once on body styles the whole page.
Size and weight
font-size sets the size of the letters, and rem is a great unit for it, as you saw in the last lesson. font-weight sets how thick the letters are: normal (the same as 400), bold (700), or any hundred from 100 to 900 — if the font comes in that weight.
Line height and alignment
line-height controls the space between lines. Write it without a unit: 1.6 means 1.6 times the font size, so it scales automatically for big and small text. Body text is comfortable between 1.4 and 1.7; the browser's default is a little cramped.
text-align lines text up on the left, on the right, in the center, or stretched to both edges with justify. Centring suits short headings and buttons; long paragraphs are easier to read left-aligned.
p {
font-size: 1.125rem;
line-height: 1.6;
}
h1 {
font-weight: 800;
text-align: center;
}
Decorating text
text-decoration adds or removes lines: underline, line-through or none. A classic use is an old price in a shop. To style just a few words, wrap them in a <span> — the inline twin of <div>, a wrapper without meaning:
<p>Healing potion: <span class="old-price">12 gold</span> 9 gold</p>
.old-price {
text-decoration: line-through;
}
You can also remove the underline from links with text-decoration: none — but then make them stand out another way, for example with a colour, or visitors won't know what's clickable.
Common mistakes
- No generic family at the end. If none of your fonts is installed, the browser falls back to its own default.
- The whole stack in one string.
font-family: "Georgia, serif"looks for a single font with that long, odd name. - A unit on line-height.
line-height: 1.6pxsquashes every line onto the one above it. - Centring long paragraphs. The ragged left edge makes every new line hard to find.
Assignment
Style the potion shop:
body: the font stackGeorgia, "Times New Roman", serifand aline-heightof1.6.h1: the font stackArial, Helvetica, sans-serif, and centred..lead: the font size1.25rem, and bold..old-price: crossed out withtext-decoration: line-through.
Premium lesson
Unlock the full Learn HTML & CSS course
- All 29 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The HTML & CSS cheat sheet has every topic of Learn HTML & CSS on one page.
Lesson last updated on .