Sub topic 4
Typography utilities
Typography utilities control font size, weight, line height, spacing between letters, alignment, and text color. This chapter assumes you know the setup from the earlier chapters: Tailwind is installed, your build scans your templates, and you are comfortable writing utility classes in markup. It also assumes you know basic CSS text properties such as font-size, font-weight, and line-height.
Font size and its built-in line height
Font size utilities use the text- prefix with a scale name: text-xs, text-sm, text-base, text-lg, text-xl, text-2xl, and so on up to text-9xl.
Each size sets two properties at once: font-size and a matching line-height. For example, text-xl sets the size to 1.25rem and pairs it with a line height of the right proportion. You get readable text without adding a second class.
When you need a different line height, add a leading- utility. You can also set both in one class with a slash: text-sm/6 means small text with a line height of 6 on the spacing scale.
<p class="text-lg leading-relaxed">Looser line height for long paragraphs.</p>
<p class="text-sm/6">Small text with a line height of 1.5rem.</p>Weight, family, and style
Font weight uses font- with a weight name: font-light, font-normal, font-medium, font-semibold, and font-bold. Font family uses font-sans, font-serif, and font-mono. Both share the font- prefix, and Tailwind tells them apart by the value.
Other common style utilities:
italicandnot-italicswitch italic text on and off.uppercase,lowercase, andcapitalizechange letter case without editing the text.underline,line-through, andno-underlinecontrol text decoration.truncatecuts off overflowing text and adds an ellipsis on one line.
Spacing, alignment, and color
Letter spacing uses tracking-tight, tracking-normal, and tracking-wide. Alignment uses text-left, text-center, text-right, and text-justify.
Text color also uses the text- prefix, followed by a color and shade, such as text-slate-600. Tailwind decides whether text-lg is a size or text-slate-600 is a color from the value after the prefix.
Here is a short article header that combines these utilities:
<header class="max-w-prose">
<p class="text-sm font-semibold uppercase tracking-wide text-indigo-600">
Guide
</p>
<h1 class="mt-2 text-3xl font-bold text-slate-900">
Typography in Tailwind
</h1>
<p class="mt-4 text-lg leading-relaxed text-slate-600">
Size, weight, spacing, and color are each one small class.
</p>
</header>The max-w-prose class limits the line length to about 65 characters, which keeps paragraphs comfortable to read.
Common errors
Watch for these mistakes:
- Setting size and line height separately by habit.
text-xl leading-7repeats whattext-xlalready does. Addleading-only when you want a different value. - Expecting
text-classes to conflict.text-lgandtext-red-600do not conflict because they set different properties. Two sizes, such astext-lg text-xl, do conflict, and the one defined later in the generated CSS wins, not the one written later in the class attribute. - Using weights your font does not include.
font-lighthas no visible effect if the loaded font file has only regular and bold. Check which weights you load. - Setting text on the wrong element. Text utilities inherit. Put
text-slate-700on a parent to style all its children, and override only where needed.
Exercise
A paragraph has the classes text-xl leading-tight text-slate-600. Which utility sets the font size, which sets the line height, and which sets the color? What happens to the line height that text-xl provides?
Show answer
text-xl sets the font size, leading-tight sets the line height, and text-slate-600 sets the color. The leading-tight utility overrides the line height that text-xl would otherwise supply.
Exercise
Write the classes for a heading that is 30px-scale large (3xl), bold, in dark slate (slate-900), with tight letter spacing and all uppercase.
Show answer
text-3xl font-bold text-slate-900 tracking-tight uppercase. The order of the classes does not matter.