Lesson 19 of 296
Text and fonts
What you will learn
- Choosing fonts and font stacks
- Size, weight, line height and spacing
- Alignment, decoration and case
- Loading web fonts correctly
- Cutting off long text with "..."
On this page
Font family and font stacks
body {
font-family: "Inter", Arial, Helvetica, sans-serif;
}This is a font stack. The browser uses the first font it has. If Inter fails to load, it tries Arial, then Helvetica, then any sans-serif font. Always end with a generic family:
| Generic family | Looks like |
|---|---|
serif | Letters with small feet, like Times |
sans-serif | Clean letters without feet, like Arial |
monospace | Every letter the same width, used for code |
system-ui | The default font of the user's operating system |
Put font names with spaces in quotes: "Open Sans".
The main text properties
| Property | Example | Notes |
|---|---|---|
font-size | 1rem | Body text is usually 16px (1rem) or a little more |
font-weight | 400 normal, 700 bold | Numbers from 100 to 900. The font must include that weight. |
font-style | italic | |
line-height | 1.5 | Space between lines. Use a number with no unit. |
letter-spacing | 0.02em | Space between letters |
text-align | left, center, right, justify | |
text-transform | uppercase, capitalize | Changes the look only, not the real text |
text-decoration | underline, none | Often used to remove link underlines |
color | #222 | Text colour |
Why line-height should have no unit
body { line-height: 1.5; }A number with no unit is multiplied by each element's own font size. So a 16px paragraph gets 24px line height and a 32px heading gets 48px.
If you write line-height: 24px on the body, every child inherits 24px exactly, so a 32px heading's lines overlap. Unitless values avoid this.
For body text, 1.4 to 1.6 is comfortable to read. Headings usually look better tighter, around 1.1 to 1.25.
Shorthand
font: italic 700 1.25rem/1.4 "Inter", sans-serif;style, weight, size/line-height, family. The size and family are required in this shorthand.
Web fonts
To use a font the user may not have installed, the browser downloads it.
From a service such as Google Fonts: add the <link> it gives you to the head, then use the font name in CSS.
From your own files, with @font-face:
@font-face {
font-family: "Inter";
src: url("/fonts/inter.woff2") format("woff2");
font-weight: 400;
font-display: swap;
}- Use the WOFF2 format. It is the smallest.
- Load only the weights you really use. Each weight is a separate file.
font-display: swapshows text straight away in a backup font, then swaps to the web font when it arrives. Without it, text can be invisible while the font loads.
Cutting long text with an ellipsis
To show "A very long product na..." on one line:
.title {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}All three lines are needed. The element must also have a limited width.
To cut after several lines:
.summary {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}Despite the -webkit- prefix, this works in all modern browsers.
Long words breaking the layout
A long URL or email address can push past the edge of a box on phones:
.content { overflow-wrap: anywhere; }This allows the browser to break the word.
Common mistakes
- No generic family at the end of the font stack.
- Using a weight (like 600) the font file does not include. The browser fakes it, and it looks bad.
line-heightwith px on the body.- Loading eight font files when the design uses two weights.
- Text set in all capitals in the HTML instead of with
text-transform. Screen readers may read real capitals letter by letter. - Centring long paragraphs. Centred text is hard to read beyond two or three lines.
How to say it in an interview
"I use a font stack ending in a generic family, rem font sizes and unitless line-height so it scales with each element's font size. For web fonts I use WOFF2, load only the weights I need, and set font-display to swap so text is visible while fonts load. For single-line truncation I combine white-space nowrap, overflow hidden and text-overflow ellipsis."
Key words
| Word | Meaning |
|---|---|
| Font stack | A list of fonts in order of preference |
| Generic family | A fallback type such as serif or sans-serif |
| Font weight | How thick the letters are, from 100 to 900 |
| Line height | The height of each line of text |
| @font-face | A CSS rule that loads a font file |
| font-display: swap | Show backup text until the web font loads |
| Ellipsis | The "..." at the end of cut text |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.