CSS course menu
CSS Fonts: font-family, Font Stacks, Web Fonts, font-size and font-weight
Learn CSS fonts: font-family and fallback font stacks, generic families, system fonts, loading web fonts with @font-face and Google Fonts, font-display, font-size, font-weight, font-style, and the font shorthand.
On this page
Typography is most of a web page's design. CSS gives you control over which font is used and how big, heavy and slanted it is.
font-family and font stacks#
font-family takes a list of fonts. The browser uses the first one that is available and falls back to the next:
.serif { font-family: Georgia, "Times New Roman", serif; }
.sans { font-family: Arial, Helvetica, sans-serif; }
.mono { font-family: "Courier New", monospace; }
.system { font-family: system-ui, sans-serif; }
p { font-size: 18px; }<p class="serif">Georgia, a serif font</p>
<p class="sans">Arial, a sans-serif font</p>
<p class="mono">Courier New, a monospace font</p>
<p class="system">system-ui, your operating system's interface font</p>- Always end with a generic family:
serif,sans-serif,monospace,cursive,system-ui. The browser always has one. - Put font names that contain spaces in quotes:
"Times New Roman". system-uiuses the operating system's own font (Segoe UI on Windows, San Francisco on Apple devices, Roboto on Android). It loads instantly and looks native.
font-family is inherited, so you usually set it once on body.
Web fonts#
To use a font that visitors may not have installed, load it from a file with @font-face:
@font-face {
font-family: "Inter";
src: url("/fonts/inter-var.woff2") format("woff2");
font-weight: 100 900; /* a variable font covering all weights */
font-display: swap;
}
body {
font-family: "Inter", system-ui, sans-serif;
}- WOFF2 is the compressed web font format to use today.
font-display: swapshows text immediately in the fallback font and swaps to the web font once it loads, instead of showing invisible text while waiting.- Check the font's licence: the fonts on Google Fonts are open source and free to use on websites, but commercial fonts often need a separate web licence.
Google Fonts#
Google Fonts generates the code for you. Choose a font, and it gives you a <link> to put in your HTML head and the font-family value to use:
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap">body { font-family: "Poppins", sans-serif; }Every web font is an extra download. Load only the weights you use, and consider hosting the files yourself, which avoids sending visitors' requests to another company.
font-size#
html { font-size: 100%; } /* respect the user's setting, usually 16px */
h1 { font-size: 2rem; }
p { font-size: 1rem; }
small { font-size: 0.875rem; }<h1>2rem heading</h1>
<p>1rem paragraph <small>and 0.875rem small text</small></p>Use rem for font sizes so text scales with the user's preferences (see Units). Body text of 1rem (16px by default) is a good minimum on most sites. For sizes that grow smoothly with the screen, see clamp() in calc, min, max and clamp.
font-weight and font-style#
p { font-family: system-ui, sans-serif; margin: 4px 0; }<p style="font-weight: 300">300 light</p>
<p style="font-weight: 400">400 normal</p>
<p style="font-weight: 600">600 semibold</p>
<p style="font-weight: 700">700 bold</p>
<p style="font-style: italic">italic</p>font-weighttakesnormal(400),bold(700) or numbers from 1 to 1000. A weight only looks different if the font has that weight available; otherwise the browser picks the nearest one (and may fake bold).font-style: italicuses the font's italic version.
The font shorthand#
p {
font: italic 600 1rem/1.6 Georgia, serif;
/* style weight size/line-height family */
}Size and family are required, and must come last. Like other shorthands, font resets anything you leave out, which can be surprising. Many developers prefer separate properties for clarity.
Practice#
Practice exercise
Set the page to use the system interface font with a sans-serif fallback, headings to use Georgia with a serif fallback at weight 700, and paragraph text at 1.0625rem.
Show solution
body { font-family: system-ui, sans-serif; }
h1, h2 { font-family: Georgia, serif; font-weight: 700; }
p { font-size: 1.0625rem; }<h1>A Georgia heading</h1>
<p>A paragraph in the system font, slightly larger than 1rem.</p>Key takeaway#
Finished this lesson?