Sub topic 4
Typography utilities
Typography utilities control the size, weight, spacing, alignment, and color of text. This chapter covers what each family does, how the text- prefix is interpreted, how to extend the type scale, and the mistakes that cause most surprises.
What you need before starting
This chapter assumes you know:
- The CSS properties
font-size,line-height,font-weight,letter-spacing, andtext-align. - How to add Tailwind CSS v4 to a project, as covered in the setup chapter.
- The basic shape of a utility class, such as
mt-4orbg-white.
The working model
Each typography utility maps to one CSS property, with one exception. A font-size utility such as text-lg also sets line-height, because the default theme pairs each size with a matching line height. Tailwind's preflight stylesheet also removes the browser's default heading sizes and weights. An <h1> with no classes looks like body text, so every heading needs its own utilities.
| Family | Example classes | CSS property |
|---|---|---|
| Size (and line height) | `text-sm`, `text-xl`, `text-4xl` | `font-size`, `line-height` |
| Family | `font-sans`, `font-serif`, `font-mono` | `font-family` |
| Weight | `font-light`, `font-medium`, `font-bold` | `font-weight` |
| Line height | `leading-tight`, `leading-relaxed`, `leading-7` | `line-height` |
| Letter spacing | `tracking-tight`, `tracking-wide` | `letter-spacing` |
| Alignment | `text-left`, `text-center`, `text-right` | `text-align` |
| Color | `text-slate-600`, `text-indigo-600/80` | `color` |
Font size and line height
The default scale runs from text-xs through text-sm, text-base, text-lg, text-xl, and on up to text-9xl. text-base is 1rem, usually 16px. Sizes use rem, so they scale with the user's browser font setting.
To change only the line height, add a slash and a value after the size. This is called a line-height modifier. You can also use a separate leading-* utility.
<p class="text-sm/6 text-slate-600">
Size small, line height 1.5rem (6 on the spacing scale).
</p>
<p class="text-lg leading-loose text-slate-600">
Size large, line height 2 from the leading-loose utility.
</p>A leading-* utility overrides the line height that comes from a size utility. This holds whatever order the classes appear in the class attribute, because the stylesheet decides precedence and not the attribute.
Line heights come in two kinds. Named ones such as leading-tight (1.25), leading-snug, leading-normal (1.5), leading-relaxed, and leading-loose are unitless multipliers of the font size. Numeric ones such as leading-6 are fixed lengths from the spacing scale. Use multipliers for text that may change size, and fixed lengths when you need rows to line up on a grid.
Font family and weight
font-sans, font-serif, and font-mono map to font stacks defined in the theme. Weights are named for their numbers:
| Class | 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 |
A weight only changes the rendering if the font has that weight available. If your page loads only the 400 and 700 files of a font, font-semibold renders as one of those two, or as a synthesized bold. Load the weights you use.
Letter spacing, alignment, and transforms
Tracking utilities run from tracking-tighter to tracking-widest. Large headings usually look better with tracking-tight. Small uppercase labels usually look better with tracking-wide.
Alignment uses text-left, text-center, text-right, and text-justify. Case and style have their own utilities: uppercase, lowercase, capitalize, normal-case, italic, and not-italic. uppercase only changes how text is drawn. The text in the HTML stays as written, and screen readers read the original.
Decoration is split across several classes:
<a href="/docs" class="underline decoration-indigo-500 decoration-2 underline-offset-4 hover:decoration-4">
Documentation
</a>underline turns the line on. decoration-* sets its color and thickness. underline-offset-* moves it away from the text. Use no-underline to remove it.
Wrapping and overflow
Long text needs explicit rules. These are the utilities you will use most:
truncatekeeps one line and ends it with an ellipsis. It appliesoverflow: hidden,text-overflow: ellipsis, andwhite-space: nowrap.line-clamp-3cuts text after three lines and adds an ellipsis.text-balanceevens out line lengths, which suits headings.text-prettyavoids a single short word on the last line, which suits paragraphs.break-wordslets long strings such as URLs wrap instead of overflowing.whitespace-pre-wrapkeeps line breaks and spaces from the source text.
truncate only works when the element has a constrained width. Inside a flex row, the child also needs min-w-0, because flex items default to min-width: auto and refuse to shrink below their content.
<div class="flex w-72 items-center gap-3">
<img src="/avatar.png" alt="" class="size-10 shrink-0 rounded-full">
<p class="min-w-0 truncate font-medium">
A very long display name that would otherwise push the layout wider
</p>
</div>Numbers in tables or prices line up better with tabular-nums, which gives every digit the same width.
Text color
Color utilities use the text- prefix with a palette name and a shade: text-slate-900, text-indigo-600. Add an opacity after a slash to blend with the background, for example text-slate-900/70. For readable body text on white, text-slate-600 through text-slate-900 is a common range. Check contrast when you go lighter. Light gray text on white is a frequent accessibility failure.
How `text-` is resolved
The prefix text- serves three jobs: size, alignment, and color. Tailwind picks the right one from what follows the prefix.
These three can be combined on one element without conflict, because they set different properties. The same prefix sharing means that a typo is not always an error. text-centre matches nothing, so Tailwind generates no CSS and the text stays left-aligned with no warning.
Extending the type scale in v4
In Tailwind v4 you customize typography in CSS with the @theme directive. Variables in the --font-* namespace create font family utilities. Variables in the --text-* namespace create size utilities, and a matching --line-height variable sets the paired line height.
@import "tailwindcss";
@theme {
--font-display: "Fraunces", serif;
--text-tiny: 0.625rem;
--text-tiny--line-height: 1rem;
}This gives you font-display and text-tiny. The font itself must still be loaded, either with a <link> in the page head or an @font-face rule. Naming the family in @theme does not download anything.
Common errors
- Unstyled headings. After preflight,
<h1>and<h2>have no size or weight. Addtext-3xl font-boldor similar. - Assembled class names. `
text-${size}` is invisible to Tailwind's scanner, so the CSS is never generated. Write each full class name, as covered in the dynamic classes chapter. - Unloaded weights. A
font-mediumthat looks identical tofont-normalusually means the font file for 500 was never loaded. - Truncation that does nothing.
truncateon an element with no width limit, or on a flex child withoutmin-w-0, never triggers. - Fighting line height. Setting
text-xlafterleading-6in your mind is not the issue. The issue is expectingtext-xlto leave line height alone. Usetext-xl/6or addleading-*to set both intentionally. - Text size in
px. Arbitrary values liketext-[14px]ignore the user's font size preference. Prefer the scale, orremvalues such astext-[0.875rem].
A complete example
This article header combines the families above. Heading, label, and body each get their own size, weight, spacing, and color.
<article class="max-w-prose p-6">
<p class="text-xs font-semibold uppercase tracking-widest text-indigo-600">
Guide
</p>
<h1 class="mt-2 text-balance text-3xl font-bold tracking-tight text-slate-900 md:text-5xl">
Typography that scales with the screen
</h1>
<p class="mt-4 text-pretty text-base/7 text-slate-600">
Body copy uses a comfortable line height and a muted color so the
heading carries the hierarchy.
</p>
<p class="mt-4 line-clamp-2 text-sm text-slate-500">
A preview paragraph that is cut off after two lines, however long the
underlying text happens to be in the content you load into this page.
</p>
</article>The md:text-5xl prefix applies the larger size from the medium breakpoint upward. Responsive prefixes are covered in a later chapter.
Exercise
A card title is wrapped in a flex row next to an icon. The class truncate is on the title, but long titles still push the row wider than the card. Name the likely cause and give the fix.
Show answer
Flex items default to min-width: auto, so the title will not shrink below its content width and the ellipsis never triggers. Add min-w-0 to the title element (and keep shrink-0 on the icon) so the title can shrink and truncate can apply.
Exercise
You write <h2 class="text-xl leading-6 text-slate-800">. What line height does the heading get, and how could you express the same size and line height in one class?
Show answer
The line height is 1.5rem (leading-6), because a leading-* utility overrides the line height that text-xl sets, whatever the class order. In one class, use the line-height modifier: text-xl/6.
Exercise
In Tailwind v4, which two lines in an @theme block create a new text-tiny utility with a 1rem line height?
Show answer
--text-tiny: 0.625rem; and --text-tiny--line-height: 1rem;