Write <h1>Title</h1> in a Tailwind project. It shows at the size of plain text, not big and not bold. Tailwind removes the browser's built-in styles on purpose. So you decide how text looks yourself, with classes.
Typography classes set how text looks: the font size, how heavy it is, and whether it's aligned left, centre or right. These classes are like the toolbar in a word processor, with a font size picker, a B button for bold, and a centre-align button. In Tailwind, each choice on that toolbar is one class: text-2xl for size, font-bold for bold, text-center for centring.
A title and a description
Here are the two elements from the exercise:
<h1 id="title" class="text-2xl font-bold">Title</h1>
<p id="desc" class="text-sm text-center">Short description</p>
| Class | The CSS Tailwind writes | The result |
|---|---|---|
text-2xl |
font-size: var(--text-2xl) and its line-height |
24px, 32px between lines |
font-bold |
font-weight: var(--font-weight-bold) |
700 |
text-sm |
font-size: var(--text-sm) and its line-height |
14px, 20px between lines |
text-center |
text-align: center |
centred |
- The title becomes 24px and bold. The description is 14px, with its text in the middle.
- Why does
<h1>still needfont-bold? Tailwind adds preflight, base CSS that removes the browser's default styles. You set its size and weight yourself with classes.
Font size
| Class | font-size | px | line-height |
|---|---|---|---|
text-xs |
0.75rem |
12px | 16px |
text-sm |
0.875rem |
14px | 20px |
text-base |
1rem |
16px | 24px |
text-lg |
1.125rem |
18px | 28px |
text-xl |
1.25rem |
20px | 28px |
text-2xl |
1.5rem |
24px | 32px |
text-3xl |
1.875rem |
30px | 36px |
text-4xl |
2.25rem |
36px | 40px |
text-base is the base size: 1rem = 16px. The scale continues up to text-9xl. Each size class also sets a line-height (the space between lines) that suits that size.
Font weight
| Class | font-weight |
|---|---|
font-thin |
100 |
font-light |
300 |
font-normal |
400 |
font-medium |
500 |
font-semibold |
600 |
font-bold |
700 |
font-extrabold |
800 |
font-black |
900 |
font-bold = font-weight: 700, the weight headings usually use. Middle values, such as 500 and 600, only look different when the font has that weight. When it doesn't, the browser uses the nearest weight it has.
Text alignment
text-left, text-center, text-right and text-justify make text-align with the value named after the dash: text-center = text-align: center. Alignment works on the text inside a block element, such as <p>, <h1> or <div>.
The text- prefix has three jobs
Tailwind uses the word after text- to decide which job the class does:
- size:
text-sm,text-base,text-2xlmakefont-size - alignment:
text-left,text-center,text-rightmaketext-align - colour:
text-blue-600,text-whitemakecolor
So text-2xl and text-center set two different things, and they're safe together: class="text-2xl text-center text-blue-600". Weight is different again: its prefix is font-, because the property is font-weight.
💡 Line spacing, letter spacing, and other styles
| Class | CSS | Used for |
|---|---|---|
leading-tight |
line-height: 1.25 |
a big, tight heading |
leading-relaxed |
line-height: 1.625 |
a long paragraph that needs room |
tracking-tight |
letter-spacing: -0.025em |
big headings |
tracking-wide |
letter-spacing: 0.025em |
small labels |
uppercase |
text-transform: uppercase |
capital-letter labels |
underline |
text-decoration-line: underline |
links |
italic |
font-style: italic |
quotes |
Size and line-height can be written together with a slash: text-sm/6 = 14px with a 24px line-height (6 steps × 4px). For long articles there's an official plugin, @tailwindcss/typography, with a prose class. Plugins (add-ons for Tailwind) are covered later, in the plugin lesson.
⚠️ Wrong ideas about the text classes
- Expecting a bare
<h1>to be big. Preflight removes the default size and weight of headings. Writetext-2xl font-boldyourself. text-bold. That class doesn't exist. Bold isfont-bold, because the property isfont-weight.text-centeron a<span>. A<span>is an inline element only as wide as its text, so there's no room to move the text to the middle. Put the class on the block element, for example<p id="desc">.- Reading
text-2xlas twice the size.2xlis the name of a step, not a multiplication:text-2xl= 24px,text-3xl= 30px.
TL;DR: Font size uses text-xs up to text-9xl: text-base = 16px (1rem), text-sm = 14px, text-2xl = 24px. Each size class also sets a line-height (the space between lines). Weight uses font-*: font-normal 400, font-semibold 600, font-bold 700. Alignment uses text-left, text-center, text-right, and only shows on block elements. The text- prefix is used for size, alignment and colour, and Tailwind uses the word after it to tell which one. Preflight (Tailwind's base CSS) makes a bare <h1> the size of ordinary text, so a heading always needs a size class and font-bold. Line spacing uses leading-*, letter spacing tracking-*.
⚡ A title and a description
Write:
<h1 id="title" class="text-2xl font-bold">Title</h1>
<p id="desc" class="text-sm text-center">Short description</p>
Guess first: in a Tailwind project, does an <h1> with no classes show big and bold?
No. Preflight, the base CSS that Tailwind adds, removes the browser's default styles. So an <h1> with no classes is the size of ordinary text.
Meaning: text-2xl makes the letters 24px, font-bold makes them bold (weight 700), text-sm makes the letters 14px, and text-center centres the text.
⚡ Bold uses font-
Guess first: is there a text-bold class?
No. Bold is font-bold, because the property is font-weight.
Meaning: The text- prefix does three jobs: size (text-2xl), alignment (text-center) and colour (text-blue-600). Tailwind uses the word after it to tell which one.
⚡ Alignment needs a block element
Meaning: text-center on a <span> shows no effect, because a <span> is only as wide as its text. Put it on a block element such as <p>, <h1> or <div>.