Skip to content

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.

19 min read0/4 questions

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.

ClassFont sizeLine height
text-xs12px16px
text-sm14px20px
text-base16px24px
text-lg18px28px
text-xl20px28px
text-2xl24px32px
text-4xl36px40px
text-6xl60px1 (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.

HTML + Tailwind
<div class="space-y-2 text-slate-900"> <p class="text-xs">text-xs: captions and fine print</p> <p class="text-sm">text-sm: secondary text, labels</p> <p class="text-base">text-base: body copy</p> <p class="text-xl">text-xl: lead paragraph</p> <p class="text-3xl font-semibold">text-3xl: section title</p> <p class="text-5xl font-bold tracking-tight">text-5xl: hero</p></div>

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 for pretty is newer than for balance; check Can I Use. Both fall back to normal wrapping, so they are safe to add.
  • truncate keeps text on one line and ends it with an ellipsis (it sets overflow: hidden, text-overflow: ellipsis and white-space: nowrap).
  • line-clamp-3 shows at most three lines, then an ellipsis. Perfect for card excerpts.
HTML + Tailwind
<article class="max-w-sm rounded-2xl border border-slate-200 p-6"> <p class="text-xs font-semibold uppercase tracking-wider text-emerald-700">Field notes</p> <h2 class="mt-2 text-2xl font-bold tracking-tight text-balance text-slate-900"> What a year of sourdough taught me about patience </h2> <p class="mt-3 line-clamp-3 leading-relaxed text-pretty text-slate-600"> The first loaf was a brick. The second was a slightly flatter brick. Somewhere around the fortieth, the crumb opened up, the crust crackled, and I realised the recipe had never changed. I had. I learned to read the dough instead of the clock, and that skill turned out to matter far beyond the kitchen. </p> <p class="mt-4 truncate text-sm text-slate-500"> By Priya Nair, filed under baking, habits, slow food, weekends and kitchen experiments </p></article>

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.

Quick check
A product card should show the title on one line, cut off with an ellipsis if it is too long. Which class does that?
Need a nudge? A hint rules out a wrong answer.

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:

HTML + Tailwind
<style type="text/tailwindcss"> @theme { --font-display: Georgia, "Times New Roman", serif; }</style><header class="p-4"> <p class="font-mono text-xs uppercase tracking-widest text-slate-500">Issue 12, Autumn</p> <h1 class="mt-2 font-display text-5xl italic text-slate-900">The Slow Kitchen</h1> <p class="mt-3 max-w-md text-slate-600">Recipes that reward waiting, from people who like to wait.</p></header>

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:

Reading a colour class
  1. hover:variant: when it applies
  2. bg-property: background colour
  3. sky-600palette colour and shade
  4. /75opacity modifier: 75%

An optional variant comes first. hover: means only while hovered.

1/4

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.

HTML + Tailwind
<div class="relative h-48 overflow-hidden rounded-xl bg-linear-to-br from-amber-300 via-rose-400 to-violet-500"> <div class="absolute inset-0 bg-black/40"></div> <p class="relative p-6 text-2xl font-bold text-white">Readable on any background</p></div> <p class="mt-5"> <a href="#" class="font-medium text-indigo-700 underline decoration-indigo-300 decoration-2 underline-offset-4 hover:decoration-indigo-700"> A link with a styled underline </a></p><button class="mt-4 rounded-lg border border-slate-300 px-4 py-2 ring-4 ring-sky-500/30"> Ring around a button</button>

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.

Predict the outputHTML

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">
Need a nudge? Hints walk you through it.

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.)

HTML + Tailwind
<section class="rounded-2xl bg-linear-to-r from-indigo-600 via-violet-600 to-fuchsia-600 px-6 py-12 text-center text-white"> <p class="text-xs font-semibold uppercase tracking-widest text-white/80">New this spring</p> <h1 class="mx-auto mt-3 max-w-md text-4xl font-black tracking-tight text-balance"> Meal plans that cook themselves </h1> <p class="mx-auto mt-3 max-w-sm text-indigo-100">Pick five dinners. Get one shopping list.</p> <a href="#" class="mt-6 inline-block rounded-full bg-white px-5 py-2 font-semibold text-indigo-700">Try it free</a></section> <h2 class="mt-8 bg-linear-to-r from-emerald-600 to-sky-600 bg-clip-text text-3xl font-bold text-transparent"> Gradient text</h2>

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; 400 fails for body text. Use 600 or 700 for comfortable reading.
  • White text on blue-600, indigo-600 or slate-800 passes. White on amber-400, yellow-400 or emerald-500 fails: bright yellows and greens need dark text instead.
  • On dark backgrounds, flip it: slate-300 or slate-400 text on slate-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 the pairs0/5 matched

Match each class to what it changes.

Choose an item, then its match on the other side.
Quick check
A designer wants white button text on bg-amber-400. What is the best response?
Need a nudge? A hint rules out a wrong answer.

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>
  • prose styles headings, paragraphs, lists, links, quotes, tables and code with tuned spacing.
  • prose-slate (or zinc, stone, neutral, gray) picks the grey family.
  • prose-lg and friends scale the whole thing.
  • dark:prose-invert switches to light text for dark mode.
  • max-w-none removes the default maximum width (about 65 characters) if your layout already limits it.
  • not-prose on 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.

Key terms
type scale
Tailwind's set of font sizes from text-xs to text-9xl, each paired with a line height.
tracking
Letter spacing. tracking-tight for big headings, tracking-wide for 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.