Open two news websites. Both are mostly text, but one is easy to read and the other makes your eyes tired. The difference is often only the letters: the typeface, size, weight and the space between lines.
Setting how letters look is called typography. In CSS, typography is set with properties whose names start with font-, plus line-height.
Styling a paragraph's letters
This rule sets three things for the letters of a paragraph:
p {
font-family: Georgia, serif;
font-size: 20px;
font-weight: 400;
}
pis the selector: the rule is for every<p>. Each line inside{ }is one declaration.font-familypicks the typeface (the font).Georgia, serifmeans: use the Georgia font, and if Georgia isn't there, use any serif font. Serif letters have small strokes at their ends, like in a newspaper.font-size: 20px;sets the font size to 20 pixels. Write the number and its unit with no space:20px, not20 px.font-weightsets how thick the letters are, from100(thin) to900(the boldest).400is the same asnormal,700the same asbold.
Backup fonts
font-family can hold several font names, separated by commas. The browser uses the first font in the list that is installed on the visitor's device. The rest are backups. End the list with one generic family, which names a kind of letter and not one font. The common ones are serif, sans-serif (without the small strokes) and monospace (every letter the same width, like in a code editor). A font name that contains a space goes in quotes: "Times New Roman".
If the first font isn't installed, does the text disappear? No. The browser goes to the next name in the list. If none of them are there, the browser uses its own default font.
A single name is fine too, like font-family: Arial;.
Be careful with fonts you installed yourself. If you write font-family: Poppins; and Poppins is only installed on your laptop, other visitors see a backup font. For them to see Poppins too, their browser has to download the font file. Your options:
| Way | Details |
|---|---|
font-family: system-ui, sans-serif; |
The visitor's own system font. Nothing to download, so it is the fastest. |
| Google Fonts | A free collection of fonts from Google. Copy one <link> line from its site into the <head>, then write the font name in font-family. |
| Your own font file | @font-face (a CSS rule that adds a font file) with a .woff2 file (a font format for the web) and font-display: swap, so the text shows in a backup font while the font downloads. |
The examples in this lesson use fonts that are already installed, so nothing is downloaded. If you download fonts, one or two families are enough: each family means extra files to download.
Space between lines with line-height
line-height sets the height of each line of text, and so the space between lines. A number without a unit means a multiple of the font size. For example, with font-size: 18px, line-height: 1.6 makes each line 18 times 1.6, which is 28.8 pixels tall. Long paragraphs are comfortable to read at around 1.5 to 1.6.
Size units: px, em, rem
px(pixels): a fixed size.em: when used infont-size, calculated from the parent's font size.2eminside a 16px parent is 32px (2 times 16). In other properties, such aspadding,emis calculated from the element's own font size:padding: 1emon an element with 20px text is 20px.rem(root em): calculated from the font size of the<html>element, which is 16px by default. So1.5remis 24px.
Some users increase the text size in their browser settings. A size in px does not grow, so that setting does not help them. A size in rem grows with it. So use rem for font sizes in real projects. The exercises here use px so the numbers are easy to check.
⚠️ Numbers, units and font names that miss
- A space before the unit, or no unit. The browser ignores both
32 px(with a space) andfont-size: 32;(no unit). Write32px. Onlyline-heightis normally written without a unit. - A typo in the font name. No font is called
Georgai, so the browser silently uses a backup font. emmultiplying.1.5eminside1.5emis 1.5 times 1.5, which is 2.25 times the original size.
TL;DR: font-family picks the typeface (the font). Write several font names separated by commas: the browser uses the first one that is installed, and the rest are backups. End with a kind of letter: serif, sans-serif or monospace. font-size sets the letter size (32px, no space before the unit). font-weight sets the thickness (bold is 700). line-height sets the space between lines: a number with no unit (around 1.5) means a multiple of the font size. The rem unit is calculated from the font size of <html>, em from the parent's font size (in font-size) or from the element's own font size (in other properties). A size in rem grows when users enlarge the text in their browser settings.
⚡ Typeface, size, weight
Write:
p {
font-family: Georgia, serif;
font-size: 20px;
font-weight: 400;
}
Meaning: font-family picks the typeface (the font), here Georgia. font-size sets its size, here 20 pixels. font-weight sets its thickness (400 normal, 700 bold). Write the number and its unit with no space: 20px, not 20 px.
⚡ Backup fonts
Guess first: if Georgia isn't installed on the visitor's device, does the text disappear?
No. The browser goes to the next name in the list Georgia, serif, which is any serif font (letters with small strokes at their ends).
Meaning: In font-family, write several font names separated by commas. The browser uses the first one that is installed, and the rest are backups. End the list with one generic family (a kind of letter): serif, sans-serif (without the small strokes) or monospace (every letter the same width).
⚡ Space between lines
Write:
p {
line-height: 1.6;
}
Meaning: line-height sets the height of each line, and so the space between lines. A number without a unit is a multiple: 1.6 means 1.6 times the font size. Long paragraphs are comfortable to read at around 1.5 to 1.6.
⚡ Letters that grow
Guess first: a user increases the text size in their browser settings. Does 20px text grow?
No. A size in px is fixed, so it does not follow that setting. A size in rem grows.
Meaning: rem is a unit calculated from the font size of the <html> element. That size is 16px by default, so 1.5rem is 24px. Use rem for font sizes in real projects.
The exercises here use px so the numbers are easy to check.