Fonts & Typography
Most of the web is text, so typography (the choice and arrangement of type) has a huge effect on how professional and readable a site feels. CSS gives you control over the font, its size, weight and style, and the spacing between lines and letters.
Choosing a font: font-family
font-family takes a list of fonts called a font stack. The browser uses the first one that is available on the device and falls back to the next if it isn't. Always end with a generic family so there is a sensible last resort.
body {
font-family: "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
code {
font-family: Consolas, "Courier New", monospace;
}
Font names that contain spaces need quotes. The main generic families are serif (with small strokes at the ends of letters, like Times), sans-serif (clean, like Arial), monospace (every character the same width, used for code) and system-ui, which uses the operating system's own interface font. system-ui, sans-serif is a fast, attractive default because nothing needs to be downloaded.
Web fonts
To use a font that visitors may not have installed, the browser must download it. The quickest way is a service such as Google Fonts, which gives you a <link> to paste into your head:
<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=Poppins:wght@400;700&display=swap" rel="stylesheet">
body {
font-family: "Poppins", system-ui, sans-serif;
}
You can also host font files yourself with @font-face. The woff2 format is the smallest and is supported everywhere.
@font-face {
font-family: "Kuza Sans";
src: url("fonts/kuza-sans.woff2") format("woff2");
font-weight: 400;
font-display: swap;
}
font-display: swap shows the text in a fallback font immediately and swaps to the web font once it arrives, so visitors on slow mobile data are never left staring at invisible text.
Every font weight you load is a separate download. Two weights (regular and bold) of one family is usually plenty. Data bundles cost money, and your users will thank you for a light page.
Size, weight and style
font-size: useremso text respects the user's settings, for examplefont-size: 1.125rem(18px at the default root size).font-weight:400is normal and700is bold. Values run from 100 (thin) to 900 (black), but only weights the font actually provides look different.font-style: italicfor italics.
Spacing and alignment
| Property | Example | Effect |
|---|---|---|
line-height | 1.6 | Space between lines; 1.5 to 1.7 is comfortable for body text. Unitless values scale with the font size. |
letter-spacing | 0.05em | Space between letters; small positive values help uppercase labels. |
text-align | left, center, right, justify | Horizontal alignment of lines. |
text-transform | uppercase, capitalize | Changes letter case visually without changing the HTML. |
text-decoration | none, underline | Lines under, over or through text; often used on links. |
max-width | 65ch | Keeps paragraphs to a readable line length. |
There's also a font shorthand (font: italic 700 1.25rem/1.4 Georgia, serif;), but it resets every font property you don't mention, so beginners usually find the individual properties safer.
Avoid long paragraphs in text-align: justify on narrow phone screens: the browser stretches the spaces between words, leaving distracting gaps. Left-aligned text is easier to read.
Try it
body {
font-family: system-ui, sans-serif;
font-size: 1.125rem;
line-height: 1.6;
color: #1f2937;
}
h1 {
font-family: Georgia, "Times New Roman", serif;
font-size: 2.5rem;
font-weight: 700;
line-height: 1.2;
letter-spacing: -0.01em;
}
p {
max-width: 60ch;
}
a {
text-decoration: none;
border-bottom: 2px solid #f59e0b;
}
button {
text-transform: uppercase;
letter-spacing: 0.08em;
font-weight: 700;
}
The heading switches to a large, bold serif with tight line spacing, while body text uses the system font at 18px with relaxed spacing and a comfortable line length. The link loses its default underline and gains a thicker amber line beneath it instead, and the button text becomes bold capitals with a little extra space between letters.
Key points
font-familytakes a fallback stack; always end with a generic family.- Load web fonts with a link or
@font-face; usewoff2andfont-display: swap. - Use
remfor font sizes and a unitlessline-heightaround 1.5 to 1.7. - Keep lines short with
max-widthinchfor readability.
Tip: use the ← and → keys to move between pages.
