skip to content

When upgrading a Tailwind CSS v3 project with a `tailwind.config.js` to v4, what happens to the config's theme, and when do you keep the file through `@config`?

level: seniorimportance: nice to knowfreq 6%

answer

  1. the JavaScript file is no longer found
  2. an official upgrade command
  3. extend keys become theme variables
  4. replaced keys get an initial reset
  5. @config fallback; safelist and corePlugins gone

basics

~20 s

Tailwind CSS v4 no longer detects tailwind.config.js. The upgrade tool converts a simple config to @theme variables; a config with presets, unknown keys or plugins it cannot read is kept and loaded with @config, minus options like safelist and corePlugins.

solid answer

~40 s

Run `npx @tailwindcss/upgrade` (it needs Node.js 20 or higher) on a new branch and review the diff. When it can, it rewrites the config into CSS: `@tailwind` directives become `@import "tailwindcss"`, `theme.extend` values become theme variables in the matching namespace (`colors` to `--color-*`, `screens` to `--breakpoint-*`, `fontFamily` to `--font-*`), a theme key set outside `extend` gets a reset such as `--color-*: initial` before its values, and `darkMode` becomes `@custom-variant dark`. When the file uses presets, keys it does not recognise, plugins it cannot identify or non-data values outside the theme, the tool leaves it and adds `@config "./tailwind.config.js"`, because v4 never finds a JavaScript config on its own. Even then, `corePlugins`, `safelist` and `separator` are not supported. Afterwards, replace `theme()` calls with `var(--…)` and move remaining values to `@theme` over time.

code

css · 12 lines
css
/* Before (v3) */
/* @tailwind base; @tailwind components; @tailwind utilities; */

/* After the upgrade tool, for theme.extend.colors.sage and screens.3xl */
@import "tailwindcss";

@theme {
  --color-sage-600: oklch(0.56 0.07 150);
  --breakpoint-3xl: 120rem;
}

@custom-variant dark (&:where(.dark, .dark *));

go deeper

for a junior

Recall that v4 configures the theme in CSS with @theme, that tailwind.config.js is no longer found automatically, and that npx @tailwindcss/upgrade does most of the work.

for a middle

Explain how v3 keys map to namespaces, why a non-extend key becomes a namespace reset, and what @config does for a file the tool could not convert.

for a senior

Plan the migration of a real config: what blocks automatic conversion, which options are gone even through @config, and how to move namespaces into @theme incrementally.

for a principal

Sequence the upgrade across several apps sharing one config, deciding when the legacy file can be retired and how teams stop adding to it.

## What changed in v4 In Tailwind CSS v3 the theme lived in `tailwind.config.js`, which the build found automatically. Tailwind CSS v4 is configured in CSS: tokens are theme variables inside `@theme`, and a JavaScript config is a legacy you opt into. The upgrade guide is explicit: JavaScript config files "are no longer detected automatically", and you load one with the `@config` directive. ## Step one: the upgrade tool ```bash npx @tailwindcss/upgrade ``` - It requires **Node.js 20 or higher**. - Tailwind recommends running it **on a new branch**, then reviewing the diff and testing in the browser. - It updates dependencies, migrates the config to CSS where it can, and rewrites template classes affected by v4's renames. ## How the theme is translated For a yoga-studio app whose config extends colours and adds a breakpoint, the tool maps v3 theme keys onto v4 namespaces: | v3 config key | v4 theme variable | |---|---| | `colors.sage.600` | `--color-sage-600` | | `screens['3xl']` | `--breakpoint-3xl` | | `fontFamily.display` | `--font-display` | | `borderRadius.card` | `--radius-card` | | `boxShadow.soft` | `--shadow-soft` | The difference between `extend` and a top-level key survives the migration: - Values under `theme.extend` become plain theme variables, adding to the defaults. - A key set directly under `theme`, which replaced the whole scale in v3, is written with a namespace reset first, for example `--color-*: initial;` followed by your colours. Other parts of the config move too: `darkMode: 'class'` or `'selector'` becomes a `@custom-variant dark (…)` line, the `content` globs become source paths, and simple plugins become `@plugin` lines. ## When the file stays, loaded by `@config` The tool migrates a config only when it can represent it faithfully. It keeps the file and adds `@config` instead when, among other cases: 1. A top-level option other than `theme` and `plugins` holds something that is not plain data, such as a function. 2. The config uses `presets`. 3. It contains top-level keys or theme keys the migrator does not recognise. 4. Its plugins cannot be identified statically. The result looks like this: ```css @import "tailwindcss"; @config "../../tailwind.config.js"; ``` `@config` and `@theme` can be used together. Tailwind's documentation describes this as the path for moving a config to CSS incrementally: values defined in CSS are merged where possible and otherwise take precedence over the config, presets and plugins. ## What `@config` does not bring back Some v3 options have no v4 equivalent inside the config at all: - **`corePlugins`**, which disabled built-in utilities, is not supported. - **`separator`**, the variant separator, is not supported. - **`safelist`** is not supported; v4 uses a different mechanism for forcing classes into the build. The upgrade tool reports `corePlugins` and removes it from the migrated configuration. ## Scale renames that touch the theme v4 also renamed several theme-driven scales so that every step has an explicit name. The upgrade tool rewrites the templates, but anyone migrating by hand must know them: | v3 class | v4 class | |---|---| | `shadow-sm` | `shadow-xs` | | `shadow` | `shadow-sm` | | `rounded-sm` | `rounded-xs` | | `rounded` | `rounded-sm` | | `blur` | `blur-sm` | Two defaults changed as well: `border-*` utilities now use `currentColor` instead of the configured `gray-200`, and `ring` is now 1px where v3 drew 3px (`ring-3` restores it). A migrated theme that looks slightly off usually traces back to one of these. ## The JavaScript side of the theme Two v3 habits also change: - The CSS `theme()` function is deprecated; prefer the generated variables, `var(--color-sage-600)` instead of `theme(colors.sage.600)`. Where a variable cannot be used, such as in a media query, use the variable name: `theme(--breakpoint-xl)`. - v3's `resolveConfig` export is gone; JavaScript reads the CSS variables, for example with `getComputedStyle(document.documentElement)`. ## A migration plan that holds up 1. Run the tool on a branch and read every generated `@theme` line against the old config. 2. If the config stayed behind `@config`, move one namespace at a time into `@theme` and delete it from the file. 3. Search templates for classes the old config generated through functions; check they still compile. 4. Remove the file and the `@config` line once nothing is left in it.

  • After the upgrade, a project keeps `@config` and also declares `--color-sage-600` in `@theme` with a different value. Which wins?
    Tailwind's documentation says values defined in CSS are merged where possible and otherwise take precedence over those from configs, presets and plugins. The `@theme` value wins, which is what lets you move a config to CSS one namespace at a time.
  • The v3 config had `theme: { colors: { … } }` without `extend`. What should the migrated CSS contain, and why?
    A reset first, `--color-*: initial;`, followed by the listed colours. In v3 a top-level theme key replaced the default scale, so default colours such as `bg-red-500` did not exist; without the reset, v4 would add the palette to the defaults instead of replacing them.

  • Tailwind CSS v4 still picks up tailwind.config.js from the project root.
  • theme.extend and a top-level theme key migrate to the same CSS.
  • Loading the old file with @config restores safelist and corePlugins.
  • The upgrade tool always converts the config fully to CSS.
  • resolveConfig still exposes the theme to JavaScript in v4.

Assumes Tailwind CSS v4 (4.3.3 checked); v4 stopped detecting tailwind.config.js automatically and moved theme configuration to @theme in CSS.