Sub topic 7
Font size utilities and their default line heights
What this chapter assumes
You know basic HTML and the class attribute, you understand CSS units such as px and rem, and you have a project with Tailwind CSS installed. The earlier chapters covered utility-first styling and the spacing scale. This chapter covers the text-* size utilities and the line height each one sets.
One class sets two properties
In plain CSS, font-size and line-height are separate declarations. In Tailwind, a font size utility sets both:
<p class="text-lg">Eighteen pixels of text with a 28 pixel line box.</p>text-lg produces this CSS:
.text-lg {
font-size: 1.125rem;
line-height: 1.75rem;
}The pairing is deliberate. Each size comes with a line height tuned for it, so you can set a size and get readable text without a second class. The sizes are in rem, so they scale with the root font size. By default that is 16px, which gives text-lg an 18px size.
The default scale
The default pairs are the same in Tailwind v3 and v4. In v4 the line heights are stored as unitless ratios, such as calc(1.75 / 1.125) for text-lg, but they resolve to the same rendered values.
| Class | Font size | Line height | Pixels at 16px root |
|---|---|---|---|
| `text-xs` | 0.75rem | 1rem | 12 / 16 |
| `text-sm` | 0.875rem | 1.25rem | 14 / 20 |
| `text-base` | 1rem | 1.5rem | 16 / 24 |
| `text-lg` | 1.125rem | 1.75rem | 18 / 28 |
| `text-xl` | 1.25rem | 1.75rem | 20 / 28 |
| `text-2xl` | 1.5rem | 2rem | 24 / 32 |
| `text-3xl` | 1.875rem | 2.25rem | 30 / 36 |
| `text-4xl` | 2.25rem | 2.5rem | 36 / 40 |
| `text-5xl` | 3rem | 1 | 48 / 48 |
| `text-6xl` | 3.75rem | 1 | 60 / 60 |
| `text-7xl` | 4.5rem | 1 | 72 / 72 |
| `text-8xl` | 6rem | 1 | 96 / 96 |
| `text-9xl` | 8rem | 1 | 128 / 128 |
Two patterns are worth remembering:
- From
text-xsthroughtext-4xl, the line height is a fixed length that grows more slowly than the font size. Smaller text gets proportionally more room (text-smis about 1.43 times its size,text-4xlabout 1.11 times). - From
text-5xlupward, the line height is1, equal to the font size. Large display text looks loose with generous leading, so Tailwind removes it.
Overriding the line height
You have two ways to change the line height while keeping the size.
The first is a separate leading-* utility, which a later section of this tutorial covers in depth:
<p class="text-sm leading-6">14px text on a 24px line.</p>
<p class="text-lg leading-tight">18px text with a tight line height.</p>The second is a slash modifier on the size class itself. It needs Tailwind v3.3 or later:
<p class="text-sm/6">14px text on a 24px line.</p>
<p class="text-lg/tight">18px text with a tight line height.</p>
<p class="text-base/[1.7]">16px text with a 1.7 ratio.</p>The number after the slash comes from the spacing scale, so /6 means 1.5rem. A named value such as tight means a unitless ratio. Square brackets accept any CSS value.
Class order in the class attribute does not decide the winner. Tailwind generates the leading-* rules so they take precedence over the line height bundled with text-*, whether you write text-lg leading-tight or leading-tight text-lg. Because the slash form keeps both settings in one token, it is harder to separate them by accident during a refactor.
<article class="max-w-prose">
<p class="text-xs text-gray-500">Published 2 December</p>
<h2 class="mt-1 text-2xl/8 font-semibold">Reading the type scale</h2>
<p class="mt-3 text-base/7 text-gray-700">
Body text at 16px on a 28px line is more open than the default 24px.
</p>
</article>Gotchas
Headings have no size until you give them one
Tailwind's base layer (Preflight) resets headings so that h1 through h6 inherit the parent's font size and weight. An <h1> with no classes looks like body text. This is intentional, since your type scale lives in classes and not in element defaults. Give every heading an explicit size, such as text-3xl font-bold.
<h1>Looks like a paragraph</h1>
<h1 class="text-3xl font-bold">Looks like a heading</h1>`text-*` can mean size or color
Both text-lg and text-red-500 start with text-. Tailwind tells them apart by the value after the dash. Sizes are the keywords in the table above, and colors are palette names. Both can sit on one element:
<p class="text-lg text-red-500">Large red text</p>The ambiguity matters for arbitrary values. In text-[18px] Tailwind infers a length from the unit, but text-[var(--size)] is unclear. Add a type hint: text-[length:var(--size)] for a size, or text-[color:var(--tone)] for a color.
Arbitrary sizes have no default line height
A named size sets a line height. An arbitrary size does not. text-[18px] sets only font-size, so the line height is whatever the element inherits. That is often the body default, which is too tight for large text and too loose for small text. Pair arbitrary sizes with a leading value:
<p class="text-[18px]/7">18px text on a 28px line.</p>Nested elements inherit line height as a length
A fixed length such as 1.75rem is computed once and inherited as that length. If a text-lg parent contains a <small class="text-xs">, the child gets its own text-xs line height because the class sets one. A child with no size class, such as a plain <span> that is only recolored, inherits the parent's 28px line height. This only causes surprises when you change a child's size by some route other than a text-* class, such as an inline style or a <small> default in a prose stylesheet.
`rem` follows the user's settings
Sizes in rem scale when users change their browser's base font size, which is good for accessibility. If you set html { font-size: 62.5%; } to make 1rem equal 10px, every Tailwind size shrinks by that factor. That also changes spacing, width, and everything else built on rem, so avoid it unless you plan to compensate.
Choosing sizes in practice
A small set of sizes covers most interfaces. These are common starting points, not rules:
| Role | Typical classes |
|---|---|
| Caption, metadata | `text-xs` or `text-sm` |
| Body copy | `text-base` (or `text-base/7` for long reading) |
| Lead paragraph | `text-lg` or `text-xl` |
| Section heading | `text-2xl` or `text-3xl` |
| Page title | `text-4xl`, growing to `text-5xl` or `text-6xl` at wider breakpoints |
Sizes combine with responsive variants. The type usually grows with the viewport:
<h1 class="text-3xl font-bold md:text-4xl lg:text-5xl">
Scales with the screen
</h1>At lg and above, the line height becomes 1 through text-5xl. A heading that wraps to two lines will look dense. Add leading-tight (1.25) or a slash modifier such as lg:text-5xl/tight if you expect wrapping.
Changing the scale
If the defaults do not fit, you can change the pairings in the theme instead of overriding them on every element. The details come in a later chapter, but the shape is worth seeing now. In v3, a font size entry can carry its line height:
// tailwind.config.js (Tailwind v3)
module.exports = {
theme: {
extend: {
fontSize: {
lead: ['1.375rem', { lineHeight: '2rem' }],
},
},
},
}In v4, you set a --text-* variable and an optional matching --line-height variable:
/* app.css (Tailwind v4) */
@import "tailwindcss";
@theme {
--text-lead: 1.375rem;
--text-lead--line-height: 2rem;
}Either way, text-lead now sets both properties, like the built-in sizes.
Summary of the working model
- A named
text-*size setsfont-sizeandline-heighttogether. - Sizes from
text-xstotext-4xluse fixed line heights, andtext-5xland up use1. leading-*and thetext-lg/7slash form replace the default line height, regardless of class order.- Arbitrary sizes such as
text-[18px]set only the size, so add a line height. - Preflight removes heading sizes, so every heading needs a class.
Exercise
A card title is written as <h3 class="text-[22px]">Title</h3> and wraps onto two lines that look cramped. Identify the cause, and give two different ways to fix it that keep a 22px size and a 32px line height.
Show answer
The arbitrary size text-[22px] sets only font-size, so no default line height is applied and the heading inherits the body's line height, which is too tight for it. Fixes: use a slash modifier, text-[22px]/8 (8 × 0.25rem = 2rem = 32px), or add a separate leading utility, text-[22px] leading-8. Either gives a 22px size on a 32px line.
Exercise
What are the font size and line height of text-4xl and text-5xl at a 16px root, and why does the line-height ratio change so much between them?
Show answer
text-4xl is 2.25rem with a 2.5rem line height (36px on a 40px line, ratio about 1.11 in practice relative to size). text-5xl is 3rem with a line height of 1 (48px on a 48px line). From text-5xl up, Tailwind sets the line height equal to the font size because large display text looks loose with generous leading.