React / 54
Setting up Tailwind CSS v4
Install one build plugin, add one line of CSS, and define your own colours and fonts with @theme. No config file needed.
How the pieces fit
src/index.css
Your entry CSS
@import "tailwindcss";@theme { --color-brand: … }src/**/*.jsx
Your components
className="bg-brand p-4"build plugin
@tailwindcss/vite
output
One small CSS file
.bg-brand { background-color: var(--color-brand) }Two inputs, one output. Your CSS file says "use Tailwind" (and defines your colours and fonts). Your components use the classes. A build plugin connects them and writes one stylesheet.
With Vite (recommended)
Install
The coretailwindcsspackage and its Vite plugin.Add the plugin
Next to the React plugin invite.config.js.Import Tailwind
One line at the top of your main CSS file.
npm install tailwindcss @tailwindcss/viteimport { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
export default defineConfig({
plugins: [react(), tailwindcss()],
});@import "tailwindcss";Make sure that CSS file is imported once, in your entry file:
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import "./index.css";
import App from "./App";
createRoot(document.getElementById("root")).render(
<StrictMode>
<App />
</StrictMode>
);Run npm run dev and <h1 className="text-3xl font-bold underline"> is styled.
With PostCSS (Parcel, webpack and others)
PostCSS is a tool that passes your CSS through a list of JavaScript plugins, each of which transforms it. Tools like Parcel and webpack run it for you, so Tailwind plugs in as a PostCSS plugin.
{
"plugins": {
"@tailwindcss/postcss": {}
}
}export default {
plugins: {
"@tailwindcss/postcss": {},
},
};Install with npm install tailwindcss @tailwindcss/postcss (add postcss too if your tool doesn't bundle it; Parcel does). The CSS is the same @import "tailwindcss";. You don't need autoprefixer any more: v4 adds vendor prefixes itself.
Your design system with @theme
Tailwind's colours and fonts are just a starting point. Add your own inside @theme, right in your CSS:
@import "tailwindcss";
@theme {
--color-brand: #e4572e;
--color-cream: #fdf6ec;
--font-display: "Poppins", sans-serif;
--breakpoint-3xl: 120rem;
}Each variable's name prefix (its namespace) decides which classes appear:
| You write | You get |
|---|---|
--color-brand | bg-brand, text-brand, border-brand, … |
--font-display | font-display |
--breakpoint-3xl | the 3xl: variant |
--radius-card | rounded-card |
<h1 className="font-display text-brand">Tasty Bites</h1>Adding a new name extends the defaults (bg-orange-500 still works). Reusing a default name overrides it, so --breakpoint-sm: 30rem moves sm:. To remove a whole group, reset it first: --color-*: initial;, then list only your colours.
@theme only names a font. You still have to load it, for example with a Google Fonts <link> in index.html.
Scanning more (or fewer) files
Detection is automatic, but you can steer it with @source, with paths relative to the CSS file:
@import "tailwindcss";
@source "../node_modules/@tastybites/ui"; /* also scan this package */
@source not "./legacy"; /* skip this folder */
@source inline("bg-{red,green}-500"); /* always generate bg-red-500 and bg-green-500 */Plugins
Official plugins like typography (nice styles for blog text) load with @plugin:
@import "tailwindcss";
@plugin "@tailwindcss/typography";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.
