Sub topic 16
Migrating from tailwind.config.js
This chapter shows how to move a Tailwind CSS v3 project, configured in tailwind.config.js, to the CSS-first setup of v4. It assumes you have read the earlier chapters on @theme, namespaces, extending and resetting the theme, and @theme inline. You should also know what a theme.extend block does in a v3 config.
What changes in v4
In v3, a JavaScript file held your content paths, theme, plugins and a few behavior switches. In v4, most of that moves into the CSS file that imports Tailwind. Theme values become theme variables, and the other settings get their own directives.
| v3 config key | v4 CSS equivalent |
|---|---|
| `theme.extend.colors` | `--color-*` variables in `@theme` |
| `theme.extend.fontFamily` | `--font-*` variables |
| `theme.extend.screens` | `--breakpoint-*` variables |
| `theme.extend.borderRadius` | `--radius-*` variables |
| `theme.extend.boxShadow` | `--shadow-*` variables |
| `theme.extend.transitionTimingFunction` | `--ease-*` variables |
| `theme.extend.keyframes` and `animation` | `--animate-*` variable plus `@keyframes` in `@theme` |
| `theme.spacing` | `--spacing` base value |
| `theme.colors` (without `extend`) | `--color-*: initial;` then your colors |
| `content` | Automatic detection, plus `@source` when needed |
| `plugins` | `@plugin` |
| `darkMode: "class"` | `@custom-variant dark` |
The mapping follows the namespace rule you already know. A key in the v3 theme maps to a variable namespace, and the nested key becomes the end of the variable name. colors.avocado.500 becomes --color-avocado-500.
A v3 config to convert
Here is a typical v3 configuration.
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./src/**/*.{html,js}"],
darkMode: "class",
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)",
},
keyframes: {
"fade-in": {
from: { opacity: "0" },
to: { opacity: "1" },
},
},
animation: {
"fade-in": "fade-in 0.3s ease-out",
},
},
},
plugins: [require("@tailwindcss/typography")],
};This is the v4 equivalent, written in your main CSS file.
@import "tailwindcss";
@plugin "@tailwindcss/typography";
@custom-variant dark (&:where(.dark, .dark *));
@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);
--animate-fade-in: fade-in 0.3s ease-out;
@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
}Three things to notice:
- Everything in v3's
theme.extendis now a plain@themeblock. Because@themeextends the default theme by default, you do not need anextendequivalent. - The
contentarray is gone. Tailwind v4 scans your project files automatically and skips files that your.gitignoreexcludes and binary files. darkMode: "class"has no direct setting. You declare thedarkvariant yourself with@custom-variant, so you choose the selector.
Migrating step by step
The migration is easiest when you take it in a fixed order.
- Commit your current state so you can compare the result.
- Run the official upgrade tool. It needs a recent Node.js version (20 or later) and rewrites many things for you.
- Review what it changed, especially the theme.
- Move any remaining config into CSS by hand.
- Remove the JavaScript config once nothing depends on it.
npx @tailwindcss/upgradeThe tool updates dependencies, converts the config to CSS where it can, and renames utilities that changed in v4. Read the diff before accepting it. It cannot translate arbitrary JavaScript, such as a config that computes values with functions or imports other modules.
Resetting instead of extending
A v3 config that sets theme.colors directly, without extend, replaces the default palette. In v4 you reset the namespace with initial and then define your own values.
@import "tailwindcss";
@theme {
--color-*: initial;
--color-white: #fff;
--color-brand: #3f3cbb;
}If the old config replaced the entire theme, use --*: initial; and define every namespace you still need. Treat this as a deliberate choice. After the reset, utilities that depended on defaults, such as rounded-lg or shadow-md, stop working until you define their variables.
Keeping the JavaScript config with @config
If you cannot convert everything at once, v4 can still load a JavaScript config. It does not look for tailwind.config.js on its own, so you point to it from CSS.
@import "tailwindcss";
@config "../../tailwind.config.js";The path is relative to the CSS file that contains the directive. This works well as a temporary bridge in a large codebase. You can move theme values into @theme one namespace at a time and delete them from the JavaScript file as you go.
Replacing theme() and resolveConfig
Code that read config values at build time or at runtime needs attention too.
- In CSS, the
theme()function still works but is deprecated. Use the CSS variable instead:var(--color-avocado-500)replacestheme(colors.avocado.500). - In JavaScript,
resolveConfigis no longer available. Read the variable from the page withgetComputedStyle(document.documentElement).getPropertyValue("--color-avocado-500"), or usevar(--color-avocado-500)directly in CSS and inline styles.
Variables that no utility uses are not emitted to the final CSS by default. If JavaScript reads one at runtime, declare it with @theme static so it always appears in the output.
@theme static {
--color-avocado-500: oklch(0.84 0.18 117.33);
}Common errors
- A theme value silently does nothing. The most common cause is a wrong namespace.
--colour-brandor--colors-brandcreates no utilities, because only--color-*is recognized. Check the table above. - Dark mode stops working. v4 uses the
prefers-color-schememedia query by default. If your v3 config useddarkMode: "class", add the@custom-variant darkline. - The config seems ignored. v4 does not load
tailwind.config.jsautomatically. Add@config, or finish the conversion. - Spacing looks wrong after conversion. A v3
theme.spacingobject does not map to a single variable. Set--spacingfor the base unit, and add named values such as--spacing-gutteronly when you need them. - A utility lost its default. If you reset a namespace with
initialduring the move, the defaults in that namespace are gone. Add the ones you still use.
Exercise
Convert this v3 snippet to v4 CSS. Which directives do you need, and what happens to the content key?
module.exports = {
content: ["./src/*/.html"],
darkMode: "class",
theme: {
extend: {
colors: { brand: { 500: "#3f3cbb" } },
screens: { "3xl": "120rem" },
},
},
};
Show answer
Use an @theme block with --color-brand-500: #3f3cbb; and --breakpoint-3xl: 120rem;, and add @custom-variant dark (&:where(.dark, .dark *)); to keep class-based dark mode. All of it goes after @import "tailwindcss";. The content key is dropped, because v4 detects source files automatically. If some files fall outside what it scans, add them with @source.
Exercise
Your project still has a large tailwind.config.js with a safelist entry, and you want to migrate gradually. Which directive keeps the file in use, and does the safelist entry keep working?
Show answer
Use @config "./tailwind.config.js"; (path relative to the CSS file) after the Tailwind import. The safelist entry does not keep working, because v4 does not support safelist. Replace it with @source inline() or make sure the class names appear in files Tailwind scans.