Lesson 34 of 52Tailwind CSS
Tailwind Typography and Color
Font sizes and line heights, weight, tracking, wrapping and truncation, custom fonts, the colour palette, opacity, gradients, contrast and the typography plugin.
Most of what a reader experiences on a page is text: how big it is, how much room each line gets, how dark it is against the background. Tailwind gives you a carefully tuned type scale and a large colour palette, and if you use them with a little care, pages look calm and readable without a designer in the room. This lesson covers the type utilities in depth, adds a custom font, then turns to colour: the palette, transparency, borders, rings, underlines, gradients and the contrast rules that keep text readable for everyone. It finishes with the typography plugin, which styles whole blog posts in one class.
The type scale
text-xs through text-9xl set font-size, and each one also sets a matching line-height. That pairing is the quiet reason Tailwind text looks good by default: small text gets roomy lines, huge headings get tight ones.
| Class | Font size | Line height |
|---|---|---|
text-xs | 12px | 16px |
text-sm | 14px | 20px |
text-base | 16px | 24px |
text-lg | 18px | 28px |
text-xl | 20px | 28px |
text-2xl | 24px | 32px |
text-4xl | 36px | 40px |
text-6xl | 60px | 1 (same as the size) |
Line height falls as size rises. Body text needs space between lines so the eye can find the next one; a 60px heading with the same ratio would look like separate words floating apart.
Preview
Change text-base to text-base/7. The number after the slash sets the line height directly (7 spacing units, 28px), which is handy for long-form text. You can also set line height on its own with leading-*: leading-tight (1.25), leading-normal (1.5), leading-relaxed (1.625), or spacing-based values like leading-6.
Weight, tracking and wrapping
Weight runs from font-thin (100) through font-normal (400), font-medium (500), font-semibold (600) and font-bold (700) to font-black (900). Only weights the font actually ships will render differently; system fonts usually have several.
Tracking is letter spacing. tracking-tight (-0.025em) tightens big headings, which tend to look loose. tracking-wide and tracking-wider open up small uppercase labels, which look cramped otherwise. That pairing, tight for large and wide for small caps, is worth remembering.
Wrapping controls where lines break:
text-balance(text-wrap: balance) evens out line lengths in short text like headings, so you do not get one lonely word on the last line.text-pretty(text-wrap: pretty) avoids a single short word at the end of paragraphs. Browser support forprettyis newer than forbalance; check Can I Use. Both fall back to normal wrapping, so they are safe to add.truncatekeeps text on one line and ends it with an ellipsis (it setsoverflow: hidden,text-overflow: ellipsisandwhite-space: nowrap).line-clamp-3shows at most three lines, then an ellipsis. Perfect for card excerpts.
Preview
Remove text-balance and watch the heading's last line. Change line-clamp-3 to line-clamp-2, then remove it entirely. Swap tracking-wider for tracking-normal on the label and notice how cramped it gets.
Custom fonts
Tailwind gives you three families: font-sans (the system UI font), font-serif and font-mono. To add your own, define a theme variable whose name starts with --font-:
@import "tailwindcss";
@theme {
--font-display: "Fraunces", Georgia, serif;
}That one line creates a font-display class. You still have to load the font: with a <link> to a font service, with @font-face pointing at files in your project, or, in Next.js, with next/font. Always end the list with a generic family like serif so text appears even if the font fails. The preview has no network, so this example uses Georgia, which is installed almost everywhere:
Preview
Change --font-display to "Courier New", monospace and the heading changes everywhere font-display is used. That is the point of a token. The design systems lesson goes further with @theme.
The colour palette
Tailwind ships over twenty named colours. A few are greys with different temperatures (slate is blue-ish, zinc and neutral are flat, stone is warm), and the rest run around the colour wheel: red, orange, amber, yellow, lime, green, emerald, teal, cyan, sky, blue, indigo, violet, purple, fuchsia, pink, rose. Each has eleven shades: 50, 100 through 900 in hundreds, and 950. In version 4 they are defined in the OKLCH colour space, which keeps steps between shades even to the eye.
A reliable habit for a light page: 50 to 100 for tinted backgrounds, 200 to 300 for borders, 500 to 600 for buttons and accents, 700 to 900 for text.
Any colour works with any colour utility. Put the property prefix in front:
An optional variant comes first. hover: means only while hovered.
The opacity modifier only affects that one colour, which is different from the opacity-50 class that fades the whole element including its text. A classic use is a dark overlay on a picture so white text stays readable: bg-black/50 on a layer above the image.
Preview
Change bg-black/40 to bg-black/0 and to bg-black/70. Try decoration-wavy on the link. A ring is a solid box shadow drawn outside the border; it does not take up space, so adding it never shifts the layout. That is why rings are popular for focus states.
What is the opacity of the background colour (as a percentage) on this element? Type a number.
<div class="bg-slate-900/80 text-white">
Gradients
Version 4 names gradients after the CSS function they produce. bg-linear-to-r is a linear gradient to the right, bg-linear-45 uses an angle, and there are bg-radial and bg-conic too. Colour stops come from from-*, optional via-*, and to-*, and each can take a position like from-10%. (Version 3 called the first one bg-gradient-to-r, which you will still see in older code.)
Preview
Try bg-linear-to-br, or delete the via-violet-600 stop. For gradient text, the background is clipped to the letter shapes with bg-clip-text and the text colour is made text-transparent so the gradient shows through. Keep gradient text for big headings only; it gets hard to read when small.
Contrast: picking pairs people can read
The WCAG accessibility guidelines ask for a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text (about 24px, or about 19px bold). Colour that looks fine on your bright monitor can be unreadable on a phone in sunlight, or for someone with low vision.
Rules of thumb for Tailwind's palette, to confirm with a checker:
- On white, grey text from
500(narrowly) and darker passes;400fails for body text. Use600or700for comfortable reading. - White text on
blue-600,indigo-600orslate-800passes. White onamber-400,yellow-400oremerald-500fails: bright yellows and greens need dark text instead. - On dark backgrounds, flip it:
slate-300orslate-400text onslate-900.
DevTools shows the contrast ratio when you click a colour swatch in the Styles panel, and the Accessibility panel flags failing text. Never use colour as the only signal, either: an error in red should also say "Error" or show an icon.
Match each class to what it changes.
bg-amber-400. What is the best response?The typography plugin for long content
Utilities are perfect for components you build. They are awkward for content you do not write as HTML, like a blog post rendered from Markdown or text from a CMS: you cannot put classes on every p and h2. And Preflight has stripped them all bare.
The official typography plugin solves this. Install it and register it in your CSS:
npm install -D @tailwindcss/typography@import "tailwindcss";
@plugin "@tailwindcss/typography";Then wrap the content in one class:
<article class="prose prose-slate lg:prose-lg dark:prose-invert max-w-none">
<!-- HTML from your Markdown goes here -->
</article>prosestyles headings, paragraphs, lists, links, quotes, tables and code with tuned spacing.prose-slate(orzinc,stone,neutral,gray) picks the grey family.prose-lgand friends scale the whole thing.dark:prose-invertswitches to light text for dark mode.max-w-noneremoves the default maximum width (about 65 characters) if your layout already limits it.not-proseon an inner element opts it out, for example an embedded component.- Element modifiers adjust one tag:
prose-a:text-indigo-700 prose-img:rounded-xl.
The previews here cannot load plugins, so try it in a real project.
- type scale
- Tailwind's set of font sizes from
text-xstotext-9xl, each paired with a line height. - tracking
- Letter spacing.
tracking-tightfor big headings,tracking-widefor small uppercase labels. - line-clamp
- Limits text to a number of lines with an ellipsis, as in
line-clamp-3. - OKLCH
- A colour space Tailwind v4 uses for its palette, giving visually even steps between shades.
- contrast ratio
- How different text and background are in lightness. WCAG asks for 4.5:1 for normal text and 3:1 for large text.
- ring
- A solid box shadow outside an element's border that does not affect layout, often used for focus.
- prose
- The typography plugin's class that styles a block of plain HTML, such as rendered Markdown.