Sub topic 8
Font weight and font family
This chapter assumes you know basic HTML and CSS, have a project with Tailwind CSS installed, and have already used the font size utilities. Here you will set how heavy text looks (weight) and which typeface it uses (family).
Font weight utilities
Tailwind maps each font-* weight class to a CSS font-weight declaration. The weight is a number from 100 to 900, and the class names describe it.
| Class | CSS declaration |
|---|---|
| `font-thin` | `font-weight: 100` |
| `font-light` | `font-weight: 300` |
| `font-normal` | `font-weight: 400` |
| `font-medium` | `font-weight: 500` |
| `font-semibold` | `font-weight: 600` |
| `font-bold` | `font-weight: 700` |
| `font-black` | `font-weight: 900` |
Tailwind also includes font-extralight (200), font-extrabold (800), and the other steps in between. Browsers only apply a weight if the font has it. If a font ships only 400 and 700, then font-medium and font-semibold fall back to the nearest available face.
<h2 class="text-2xl font-semibold">Pricing</h2>
<p class="text-base font-normal">Plans start at five dollars a month.</p>
<p class="text-sm font-medium text-gray-600">Billed annually</p>A common pattern is to use font-semibold or font-bold for headings and font-medium for small labels. Body text usually stays at font-normal.
Font family utilities
Tailwind provides three font family classes. Each one sets font-family to a stack, which is an ordered list of fonts. The browser uses the first font it can find.
| Class | Meaning |
|---|---|
| `font-sans` | A system sans-serif stack |
| `font-serif` | A system serif stack |
| `font-mono` | A system monospace stack |
<p class="font-sans">Interface text</p>
<p class="font-serif">Editorial text</p>
<code class="font-mono text-sm">npm run build</code>In a normal Tailwind setup, font-sans is applied to the page by default, so you only add it when you want to switch back from another family.
Adding your own font
The default stacks use fonts already on the user's device. To use a custom typeface, load it first (for example with a @font-face rule or a font provider's stylesheet), then register it in your theme. After that you can use it like a built-in family.
In Tailwind v3, add a key under theme.extend.fontFamily:
// tailwind.config.js (Tailwind v3)
module.exports = {
theme: {
extend: {
fontFamily: {
display: ['Inter', 'system-ui', 'sans-serif'],
},
},
},
}In Tailwind v4, define a --font-* variable in an @theme block:
/* app.css (Tailwind v4) */
@import "tailwindcss";
@theme {
--font-display: "Inter", system-ui, sans-serif;
}Both versions give you a font-display class. Keep generic fallbacks such as system-ui and sans-serif at the end of the list, so text stays readable while the custom font loads or if it fails to load.
Common errors
- The weight does not change. The font file for that weight is not loaded. Check that your font import includes the weights you use.
- Your custom family has no effect. In v3, putting the key under
theme.fontFamilyinstead oftheme.extend.fontFamilyreplaces the defaults, sofont-sansloses its stack. - Mixing up weight and family classes.
font-boldandfont-serifshare thefont-prefix but set different properties. Tailwind tells them apart by name, so a typo such asfont-bldsilently does nothing.
Exercise
A heading uses class="text-2xl font-semibold", but it looks identical to font-normal text. The page loads a font that provides only weights 400 and 700. What explains this, and what are two ways to fix it?
Show answer
The browser cannot find a 600 weight in the font, so it falls back to the nearest available face. Sometimes that face is the 400 weight, so the heading looks unchanged. Fix it by loading a font file that includes weight 600, or by switching the heading to font-bold (700), which the font provides.
Exercise
Write the v4 CSS and the HTML class needed to apply a custom serif font named "Lora" to a blockquote, with serif as the fallback.
Show answer
In the CSS, add @theme { --font-quote: "Lora", serif; } after @import "tailwindcss";. In the HTML, use <blockquote class="font-quote">...</blockquote>.