React / 55
Reading an older Tailwind v3 setup
What tailwind.config.js (content, theme, extend, plugins) and .postcssrc did in Tailwind v3, and where each one lives in v4.
The v3 setup at a glance
npm install -D tailwindcss@3 postcss autoprefixer
npx tailwindcss initinit created a tailwind.config.js. The CSS file then pulled Tailwind in with three directives, one per layer (resets, component classes, utilities):
@tailwind base;
@tailwind components;
@tailwind utilities;You may also see @import "tailwindcss/base"; and friends. That's the same thing, written for setups using the postcss-import plugin.
The four keys of tailwind.config.js
/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./index.html", "./src/**/*.{js,jsx}"],
theme: {
extend: {
colors: {
primary: "#3490dc",
secondary: "#ffed4a",
},
},
},
plugins: [require("@tailwindcss/forms")],
};| Key | What it does |
|---|---|
content | Which files to scan for class names. Glob patterns: ** is any folder, {js,jsx} is either extension. A file not matched here gets no styles. |
theme | Your design tokens: colours, fonts, spacing, breakpoints. Values here replace Tailwind's defaults for that group. |
theme.extend | The same, but adds to the defaults instead of replacing them. Lives inside theme. |
plugins | Extra features from packages, like @tailwindcss/forms (nicer form inputs) or @tailwindcss/typography. |
The difference between theme and theme.extend trips people up:
Don't
Replaces every default colour
theme: {
colors: { primary: "#3490dc" },
},Now bg-primary works, but bg-white, text-gray-700 and all the others are gone.
Do
Adds one colour, keeps the rest
theme: {
extend: {
colors: { primary: "#3490dc" },
},
},bg-primary and every default colour work.
What's .postcssrc for?
PostCSS runs your CSS through a list of JavaScript plugins, each one transforming it: Tailwind generates utilities, autoprefixer adds vendor prefixes like -webkit- for older browsers, cssnano minifies. .postcssrc is the file that lists those plugins. It's JSON; postcss.config.js is the same config written in JavaScript.
Parcel reads .postcssrc automatically. A v3 Parcel project usually had:
{
"plugins": {
"tailwindcss": {}
}
}In v4, the plugin name changes to @tailwindcss/postcss, and autoprefixer is no longer needed because Tailwind adds prefixes itself.
Where everything went in v4
- Load Tailwindv3 ·
@tailwind base; @tailwind components; @tailwind utilities;v4 ·@import "tailwindcss"; - Which files to scanv3 ·
content: ["./src/**/*.{html,js,jsx}"]v4 ·automatic (add extras with @source "…") - Add a colourv3 ·
theme: { extend: { colors: { primary: "#3490dc" } } }v4 ·@theme { --color-primary: #3490dc; } - Replace all coloursv3 ·
theme: { colors: { … } }v4 ·@theme { --color-*: initial; --color-…: …; } - Pluginsv3 ·
plugins: [require("@tailwindcss/forms")]v4 ·@plugin "@tailwindcss/forms"; - PostCSS configv3 ·
tailwindcss + autoprefixerv4 ·@tailwindcss/postcss only - Class-based dark modev3 ·
darkMode: "class"v4 ·@custom-variant dark (&:where(.dark, .dark *));
Upgrading an old project
The official tool does most of the work. It needs Node.js 20 or newer; run it on a new git branch and review the changes:
npx @tailwindcss/upgradeNot ready to convert the config? v4 can still load it, but not automatically. Point to it from your CSS:
@import "tailwindcss";
@config "../tailwind.config.js";Gotchas
Recall
Can you answer these?
0/4
Answer out loud (or in your head) before you tap. That's what makes it stick.
Saved on this device only. No login.
