Sub topic 14
Extending the spacing and type scale in your theme
This chapter assumes you have read the earlier chapters on the spacing scale, sizing, and typography utilities. It also assumes you have a working Tailwind project, on either v3 or v4. Both versions are covered, because the configuration differs.
By now you can choose between a scale value like p-4 and a one-off value like p-[18px]. This chapter covers the third option: adding your own named values to the theme, so the one-off value becomes a real utility.
What "the theme" means
Tailwind's defaults live in a theme. Each utility family reads its values from a part of that theme. p-4, mt-8, gap-6, w-10, and h-10 read from the spacing scale. text-lg reads from the font size scale. font-sans reads from the font family scale.
When you add a key to one of these scales, every utility family that reads from it gets a new class. That is why one gutter value gives you p-gutter, mt-gutter, gap-gutter, and w-gutter.
Extending versus replacing in Tailwind v3
In v3 you configure the theme in tailwind.config.js. Where you put a key decides what happens to the defaults.
- Keys under
theme.extendare added to the defaults. - Keys directly under
themereplace the whole default scale for that section.
// tailwind.config.js (Tailwind v3)
module.exports = {
content: ['./src/**/*.{html,js}'],
theme: {
extend: {
spacing: {
gutter: '1.5rem',
section: '6rem',
},
},
},
}With this config, p-4 still works, and p-gutter and py-section also work. If you had written spacing directly under theme, p-4 would stop producing CSS. Tailwind would not warn you, because the class would simply match nothing.
Replacing is useful when a design system allows only a few values and you want mistakes to be impossible. For most projects, extending is the safer choice.
Extending the spacing scale in Tailwind v4
Tailwind v4 moves configuration into CSS. You declare theme variables inside an @theme block. A variable named --spacing-* adds a spacing value.
/* app.css (Tailwind v4) */
@import "tailwindcss";
@theme {
--spacing-gutter: 1.5rem;
--spacing-section: 6rem;
}The result is the same: p-gutter, gap-gutter, py-section, and so on. Negative margins work in both versions, so -mt-gutter produces a negative margin of 1.5rem.
Changing the base unit in v4
Tailwind v4 computes numeric spacing from one base variable, --spacing, which defaults to 0.25rem. Classes like p-4 multiply that base by 4. This is why v4 accepts values such as p-13 or w-15 that the v3 scale did not contain.
@import "tailwindcss";
@theme {
--spacing: 0.3rem;
}This rescales every numeric spacing utility at once, so p-4 becomes 1.2rem. Use it only when the whole design uses a different grid, because it changes every existing class.
Naming spacing values
Name values for their role, not their size. A name like gutter can change from 1.5rem to 2rem in one place. A name like space-24px becomes wrong the moment the value changes.
- Good:
gutter,section,card-inset - Poor:
big,24px,medium-large
Avoid names that look like existing scale keys, such as 4 or px. Overriding those silently changes classes already used across your project.
Extending the type scale
Font size is different from spacing because each size usually carries a line height with it. When you add a size, decide on its line height at the same time.
In v3, give an array with the font size first, then an options object:
// tailwind.config.js (Tailwind v3)
module.exports = {
content: ['./src/**/*.{html,js}'],
theme: {
extend: {
fontSize: {
xxs: ['0.625rem', { lineHeight: '0.875rem', letterSpacing: '0.02em' }],
display: ['3.5rem', { lineHeight: '1.1', fontWeight: '700' }],
},
},
},
}You can now write text-xxs and text-display. The text-display class sets the size, line height, and weight together.
In v4, a size variable and a companion --line-height variable do the same job:
@import "tailwindcss";
@theme {
--text-xxs: 0.625rem;
--text-xxs--line-height: 0.875rem;
--text-display: 3.5rem;
--text-display--line-height: 1.1;
}If you leave out the line height, the new size inherits whatever line height the element already has. That is a common source of cramped or loose text, so set one.
Adding font families
A font family key follows the same pattern. In v3, the value is an array of font names, and names with spaces need inner quotes:
// tailwind.config.js (Tailwind v3)
module.exports = {
content: ['./src/**/*.{html,js}'],
theme: {
extend: {
fontFamily: {
display: ['"Playfair Display"', 'Georgia', 'serif'],
},
},
},
}In v4, the same family is a CSS variable:
@import "tailwindcss";
@theme {
--font-display: "Playfair Display", Georgia, serif;
}Either way, you get a font-display class. Defining the family does not load the font. You still need an @font-face rule or a font stylesheet <link> on the page. If the font is not loaded, the browser falls back to the next name in the list, and you will see Georgia or a generic serif.
Using the new values
The new classes behave like built-in ones, including with responsive variants.
<section class="px-gutter py-section md:px-12">
<h1 class="font-display text-display">Quarterly report</h1>
<p class="mt-4 text-xxs text-gray-500">Updated daily</p>
<div class="mt-8 flex gap-gutter">
<a href="#">Summary</a>
<a href="#">Details</a>
</div>
</section>On narrow screens the section uses px-gutter. From the md breakpoint upward, md:px-12 takes over.
Deciding between arbitrary values and theme values
The earlier chapter showed w-[320px] for one-off values. The question is when to promote a value to the theme.
Use these checks:
- The value appears in three or more places: add it to the theme.
- The value has a name in your design, such as "page gutter": add it to the theme.
- The value is a pixel-level fix for one element: use an arbitrary value.
- Changing the value later should change every use: add it to the theme.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `p-4` stops working after a config change | In v3, `spacing` was placed under `theme` instead of `theme.extend` | Move the key under `extend` |
| `p-gutter` produces no CSS in v3 | The config file is not in the project root, or `content` paths do not include the file | Check the `content` globs and where the config lives |
| `p-gutter` produces no CSS in v4 | Variable was written outside `@theme`, or named `--gutter` rather than `--spacing-gutter` | Use `@theme` and the `--spacing-` prefix |
| New text size looks cramped | No line height set for the new size | Add a line height in the array or `--line-height` variable |
| `font-display` shows the wrong font | The font file is not loaded | Add an `@font-face` rule or a font stylesheet |
| `max-w-gutter` does not work | Max width reads a different scale than spacing | Define the value in the max width or container scale instead |
The last row matters. Spacing values feed padding, margin, gap, width, height, and similar utilities. Not every sizing utility reads from the spacing scale, so test each class you expect to exist.
Version differences at a glance
| Task | Tailwind v3 | Tailwind v4 |
|---|---|---|
| Where you configure | `tailwind.config.js` | `@theme` block in CSS |
| Add a spacing value | `theme.extend.spacing` | `--spacing-name` |
| Add a font size | `theme.extend.fontSize` | `--text-name` and `--text-name--line-height` |
| Add a font family | `theme.extend.fontFamily` | `--font-name` |
| Change all numeric spacing | Replace the whole scale | Set `--spacing` |
Exercise
Your design uses a 2.5rem horizontal page margin on every page, and a heading size of 2.25rem with a line height of 1.2. In Tailwind v4, write the @theme block that makes px-page and text-heading available. Then name the classes you would write on a heading that is also in a serif font called font-headline, assuming that family is already defined.
Show answer
@theme {
--spacing-page: 2.5rem;
--text-heading: 2.25rem;
--text-heading--line-height: 1.2;
}
The heading uses the classes font-headline text-heading, and its container uses px-page. The line height comes with text-heading because of the --text-heading--line-height variable.
Exercise
A teammate moves spacing: { gutter: '1.5rem' } out of theme.extend and directly under theme in a v3 config. What happens to p-4, and why?
Show answer
p-4 stops producing CSS. Placing spacing directly under theme replaces the entire default scale with only the gutter key, so numeric values like 4 no longer exist. Keeping the key under theme.extend adds gutter and leaves the defaults in place.