Sub topic 8
Font weight and font family
This chapter covers the two utility groups that choose which glyphs the browser draws: font-* for weight and font-* for family. Both use the same prefix, but they set different CSS properties. I assume you know the class-to-CSS mapping from the first chapter and the difference between a font file and a font stack. The examples work on Tailwind v3.4 or later, and the theme section shows both v3 and v4 syntax.
Two utility groups that share a prefix
Tailwind uses font- for both font-weight and font-family. It tells them apart by the value after the prefix.
| Class | CSS declaration |
|---|---|
| `font-bold` | `font-weight: 700` |
| `font-sans` | `font-family: ui-sans-serif, system-ui, sans-serif, ...` |
| `font-mono` | `font-family: ui-monospace, SFMono-Regular, Menlo, ...` |
| `font-medium` | `font-weight: 500` |
Weight names (bold, medium) and family names (sans, serif, mono) never overlap, so there is no ambiguity until you use arbitrary values. That case is covered near the end.
Font weight utilities
Tailwind maps nine named weights to the numeric scale defined by CSS.
| Class | `font-weight` |
|---|---|
| `font-thin` | 100 |
| `font-extralight` | 200 |
| `font-light` | 300 |
| `font-normal` | 400 |
| `font-medium` | 500 |
| `font-semibold` | 600 |
| `font-bold` | 700 |
| `font-extrabold` | 800 |
| `font-black` | 900 |
<p class="font-light">Light, 300</p>
<p class="font-normal">Normal, 400</p>
<p class="font-medium">Medium, 500</p>
<p class="font-semibold">Semibold, 600</p>
<p class="font-bold">Bold, 700</p>In practice, most interfaces use only three or four of these. A common pattern is font-normal for body text, font-medium for labels and buttons, font-semibold for headings, and font-bold for emphasis. Using fewer weights keeps the page consistent and reduces the number of font files you load.
Headings are not bold by default
Tailwind's base layer (Preflight) resets headings so that h1 through h6 inherit font-size and font-weight from their parent. An <h1> therefore looks like body text until you style it.
<h1>Looks like a paragraph</h1>
<h1 class="text-3xl font-bold">Looks like a heading</h1>This is intentional. Visual hierarchy comes from your classes, not from the element name. Choose the element for document structure and the classes for appearance.
A weight only appears if the font has it
font-extrabold sets font-weight: 800, but the browser can only show what the font provides. If the loaded font family has files for 400 and 700 only, the browser picks the closest available weight. For some requests it also synthesizes a fake bold by smearing the glyphs, which looks heavier but not correct.
- A request for 600 with only 400 and 700 available renders as 700. The rule for weights above 500 is to look at heavier weights first.
- A request for 300 with only 400 and 700 available renders as 400.
- If a font has no bold file at all, the browser may synthesize one.
When two weight classes look identical on screen, check which font files you actually load before you suspect Tailwind.
Variable fonts and in-between weights
A variable font ships one file that covers a range of weights, for example 100 to 900. With one, every named class renders distinctly, and you can request values the scale lacks.
<p class="font-[450]">Weight 450 from a variable font</p>Tailwind sees a bare number in brackets and treats it as a weight. If the font is not variable, 450 snaps to a nearby loaded weight as described above.
Font family utilities
Tailwind ships three font stacks. A font stack is a comma-separated list of families. The browser uses the first one it can find and falls back through the rest.
| Class | Stack begins with | Typical use |
|---|---|---|
| `font-sans` | `ui-sans-serif, system-ui, sans-serif` | Interface and body text |
| `font-serif` | `ui-serif, Georgia, Cambria, "Times New Roman", Times, serif` | Editorial text |
| `font-mono` | `ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, ...` | Code, tabular figures |
The stacks are lists of system fonts, so they need no download. ui-sans-serif and system-ui ask the operating system for its interface font, which is San Francisco on Apple devices and Segoe UI on Windows. The result is fast to render but looks different on each platform.
Preflight applies font-sans to the html element. Everything on the page is sans-serif until you change it, and you only add font-sans to undo an override.
<article class="font-serif">
<h2 class="font-sans text-xl font-semibold">Sans heading inside a serif article</h2>
<p>This paragraph inherits the serif stack.</p>
<p>Run <code class="font-mono text-sm">npm run build</code> to compile.</p>
</article>Font family is an inherited CSS property. Set it once on a container and all descendants follow it, unless a child sets its own.
The last entry in a stack should always be a generic family (sans-serif, serif, or monospace). That guarantees some font is chosen even when none of the named ones exist.
Adding your own font family
Adding a font involves two separate steps, and people often do only one of them.
- Make the font available to the browser, with
@font-faceor a<link>to a font host. - Give the font a utility class through the theme.
Tailwind does not load fonts. It only writes the font-family declaration.
Tailwind v3
Use theme.extend.fontFamily. Each value is an array of family names, and names containing spaces need inner quotes.
// tailwind.config.js (Tailwind v3)
module.exports = {
theme: {
extend: {
fontFamily: {
display: ['"Playfair Display"', 'Georgia', 'serif'],
},
},
},
}This creates font-display. If you extend the existing sans key instead, your array replaces the default stack. In that case, include fallbacks yourself:
// tailwind.config.js (Tailwind v3)
module.exports = {
theme: {
extend: {
fontFamily: {
sans: ['Inter', 'ui-sans-serif', 'system-ui', 'sans-serif'],
},
},
},
}Because Preflight sets html to font-sans, redefining sans changes the whole site's default font with no class changes.
Tailwind v4
Use a --font-* variable inside @theme. The part after --font- becomes the class name.
/* app.css (Tailwind v4) */
@import "tailwindcss";
@theme {
--font-display: "Playfair Display", Georgia, serif;
}This creates font-display. Defining --font-sans in the same way replaces the default sans stack.
A complete example
The following page loads a font from Google Fonts and uses it through the v3 config above. The <link> handles step 1. The config handles step 2.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<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=Playfair+Display:wght@600;700&display=swap"
rel="stylesheet"
/>
<link href="/dist/output.css" rel="stylesheet" />
</head>
<body class="p-8">
<h1 class="font-display text-4xl font-bold">Display heading</h1>
<p class="mt-4 font-sans font-normal">Body text in the default sans stack.</p>
</body>
</html>The URL requests only weights 600 and 700, so a font-light heading in this family would fall back to the nearest loaded weight. Request the weights you use.
Arbitrary values for family and weight
For a font you use once, bracket syntax avoids a theme change. Spaces become underscores.
<p class="font-['Open_Sans']">One-off family</p>
<p class="font-[450]">One-off weight</p>When the bracket holds a CSS variable, Tailwind cannot tell whether it is a family or a weight. Add a type hint before the value.
<p class="font-[family-name:var(--brand-font)]">Family from a variable</p>
<p class="font-[weight:var(--brand-weight)]">Weight from a variable</p>Use brackets sparingly. A font that appears in more than one place belongs in the theme, as covered in the theme customization chapter.
Related utilities
A few neighboring utilities often appear in the same class list.
italicandnot-italicsetfont-style. They are useful for undoing italics at a breakpoint or inside a component.tabular-numssetsfont-variant-numeric: tabular-nums, which makes digits equal width. Use it in tables and counters so columns of numbers line up.antialiasedsmooths glyph rendering on macOS and iOS. It has no effect on other platforms.
Using weight and family at breakpoints
Both groups accept variant prefixes, so you can change them like any other utility.
<h2 class="text-xl font-medium md:text-2xl md:font-bold">
Heavier and larger from the md breakpoint
</h2>Heavier text is easier to read on small, low-density screens, and lighter weights suit large display sizes. Light weights at small sizes are a common legibility problem, so pair font-light with larger text such as text-3xl and up.
Common mistakes
- Expecting every weight class to look different without checking which font files are loaded.
- Overriding
sansin the theme without fallback families, which leaves no safe default if the custom font fails to load. - Forgetting that headings have no built-in weight, then wondering why
<h2>looks plain. - Naming a font in the config but never loading it.
- Writing
font-[var(--x)]withoutfamily-name:orweight:, which can produce no CSS or the wrong property.
Exercise
A page uses font-semibold on a heading in the custom family "Inter", but the heading looks identical to font-bold. The page loads Inter with family=Inter:wght@400;700. Why does this happen, and what are two ways to fix it?
Show answer
The page only loads Inter at weights 400 and 700. A request for 600 has no matching file, so the browser uses the nearest heavier loaded weight, 700, which looks the same as font-bold. Tailwind is writing font-weight: 600 correctly. The first fix is to request the missing weight, for example family=Inter:wght@400;600;700. The second fix is to load Inter as a variable font, such as family=Inter:[email protected], so any weight renders distinctly.
Exercise
You want a font-display utility for "Playfair Display" in a Tailwind v4 project, and you also want it to fall back to a generic serif font. Write the CSS, and state what else the page needs for the font to appear.
Show answer
Add this to your main CSS file:
@import "tailwindcss";
@theme {
--font-display: "Playfair Display", Georgia, serif;
}
The page also needs to load the font itself, either with a <link> to a font host or an @font-face rule. The theme variable only creates the class and its font-family declaration. It does not download the font.