Sub topic 16
Migrating from tailwind.config.js
This chapter assumes you know @theme, theme namespaces, and how Tailwind v4 is imported in a CSS file. You will move an existing JavaScript config into CSS, and learn when to keep the old file temporarily.
Why the config moves into CSS
In Tailwind v3, design tokens lived in tailwind.config.js under theme and theme.extend. In v4, tokens are CSS variables declared in @theme. The variable name decides which utilities exist, so each config key corresponds to a namespace.
| v3 config key | v4 theme namespace |
|---|---|
| `colors` | `--color-*` |
| `fontFamily` | `--font-*` |
| `screens` | `--breakpoint-*` |
| `transitionTimingFunction` | `--ease-*` |
| `borderRadius` | `--radius-*` |
| `spacing` | `--spacing` (one base value) |
| `animation` | `--animate-*` |
Translating a config by hand
Here is a typical v3 config.
// tailwind.config.js
module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {
colors: {
avocado: { 100: "oklch(0.99 0 0)", 500: "oklch(0.84 0.18 117.33)" },
},
fontFamily: { display: ["Satoshi", "sans-serif"] },
screens: { "3xl": "120rem" },
transitionTimingFunction: { fluid: "cubic-bezier(0.3, 0, 0, 1)" },
},
},
};The v4 equivalent is below. Nested keys become hyphenated names, and the extend wrapper disappears. Anything you declare in @theme is added to the defaults.
@import "tailwindcss";
@theme {
--color-avocado-100: oklch(0.99 0 0);
--color-avocado-500: oklch(0.84 0.18 117.33);
--font-display: "Satoshi", "sans-serif";
--breakpoint-3xl: 120rem;
--ease-fluid: cubic-bezier(0.3, 0, 0, 1);
}The content array has no counterpart. Tailwind v4 detects template files automatically. If you need to add a source it would not find, use @source in your CSS.
A config that set theme.colors directly, without extend, replaced the default palette. In v4 you do that explicitly with --color-*: initial; before your own colors.
Keeping the old file with @config
You can load a legacy file from CSS while you migrate:
@import "tailwindcss";
@config "../../tailwind.config.js";Tailwind then reads the JavaScript theme values as well. This lets you move tokens a few at a time. Remove the line once the file is empty.
Plugins listed in the plugins array move to the @plugin directive:
@plugin "@tailwindcss/typography";Common errors
- Keeping
extendthinking. Writing a token in@themenever replaces the defaults unless you reset the namespace withinitial. - Wrong namespace. A
--colors-brandvariable (plural) creates nothing. The namespace is--color-. - Porting every spacing step. v4 derives the scale from
--spacing. Only declare the base value, plus any unusual one-off steps. - Reading tokens in JavaScript. Code that imported your config to read colors no longer has that object. Read the CSS variable with
getComputedStyle(document.documentElement).getPropertyValue("--color-avocado-500")instead.
Migration steps
- Add
@import "tailwindcss";if it is not already there. - Add
@configpointing at the old file and confirm the build still works. - Move each token group into
@theme, using the namespace table above. - Replace
pluginsentries with@plugin. - Delete a config key once its tokens are in CSS and the page looks the same.
- Remove
@configand the JavaScript file when nothing remains.
Exercise
A v3 config has theme: { extend: { screens: { tablet: "40rem" }, colors: { brand: "#3f3cbb" } } }. Write the v4 @theme block, and name the utilities and variants it creates.
Show answer
@theme { --breakpoint-tablet: 40rem; --color-brand: #3f3cbb; }. The --breakpoint- namespace creates the tablet:* variant (for example tablet:grid-cols-3). The --color- namespace creates color utilities such as bg-brand, text-brand, and border-brand. Because both are added inside @theme and not reset, the default breakpoints and colors remain available.