CSS3 Fonts
CSS font properties control which typeface your text uses and how it looks: its size, weight, style and line spacing. Getting fonts right affects readability more than almost any other styling choice, so these are some of the first properties you set on a new site, usually on the body element so every child inherits them.
Choosing a typeface with font-family
The font-family property takes a list of fonts separated by commas. The browser uses the first one that is available and falls back down the list. Always finish with a generic family such as sans-serif, serif or monospace, so there is a sensible default when nothing else loads.
Font names that contain spaces should be wrapped in quotes, for example "Open Sans". A popular modern choice is the system font stack, which uses the operating system's own interface font and needs no download at all.
Size, weight, style and line height
font-sizesets the size. Preferremunits, which are relative to the root font size and respect the user's browser setting. A fixedpxbody size can override that preference.font-weightaccepts keywords likenormalandboldor numbers from 100 to 900. A number only works if that weight exists in the font you loaded.font-styleis usuallynormaloritalic.line-heightcontrols space between lines. A unitless value such as1.5is best, because it scales with each element's own font size.
The font shorthand combines these, but it requires at least a size and a family, and it resets any part you leave out. Many developers set the properties individually to avoid surprises.
Loading your own fonts with @font-face
To use a font that is not installed on the visitor's device, you declare it with @font-face and point to the font files. WOFF2 is the format to use; it is compressed and supported by all current browsers.
@font-face {
font-family: "Inter";
src: url("/fonts/inter-var.woff2") format("woff2");
font-weight: 100 900;
font-style: normal;
font-display: swap;
}
body {
font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
font-size: 1rem;
line-height: 1.6;
}
h1, h2 {
font-weight: 700;
line-height: 1.2;
}
code {
font-family: ui-monospace, Consolas, monospace;
}
The range 100 900 tells the browser that this one file is a variable font covering every weight in that range, instead of needing a separate file per weight.
The flash of invisible text and how to avoid it
While a web font downloads, some browsers hide the text for a moment. On a slow connection, the page looks empty even though the content has arrived. The font-display: swap descriptor fixes this by showing a fallback font immediately and swapping in your font once it is ready.
The swap can cause the text to shift slightly if the two fonts have different widths. Choosing a fallback with similar proportions, and loading only the weights you actually use, keeps that shift small.
Fake bold and italic
If you set font-weight: 700 but only loaded the regular file, the browser draws a "synthetic" bold by thickening the letters. It often looks smudged. The same happens with italic. Load the real bold and italic files, or a variable font, when you need them.
Good defaults to remember
- Set the font family, size and line height once on
body. - Use
remfor sizes and unitlessline-height. - Serve WOFF2 with
font-display: swapand end every stack with a generic family.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.