Skip to content
ClapelloNotes by Barath
ReactCh 10 · 3/6
Core4 min readUpdated

React / 54

Setting up Tailwind CSS v4

TL;DR

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"
both go into the build

build plugin

@tailwindcss/vite

or @tailwindcss/postcss for PostCSS-based tools like Parcel or webpack
writes

output

One small CSS file

.bg-brand { background-color: var(--color-brand) }
One CSS import, one build plugin. The plugin reads your CSS (including @theme) and scans your source files, then writes a single stylesheet.

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.

  1. Install

    The core tailwindcss package and its Vite plugin.
  2. Add the plugin

    Next to the React plugin in vite.config.js.
  3. Import Tailwind

    One line at the top of your main CSS file.
npm install tailwindcss @tailwindcss/vite
vite.config.js
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
 
export default defineConfig({
  plugins: [react(), tailwindcss()],
});
src/index.css
@import "tailwindcss";

Make sure that CSS file is imported once, in your entry file:

src/main.jsx
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.

.postcssrc
{
  "plugins": {
    "@tailwindcss/postcss": {}
  }
}
postcss.config.mjs
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:

src/index.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 writeYou get
--color-brandbg-brand, text-brand, border-brand, …
--font-displayfont-display
--breakpoint-3xlthe 3xl: variant
--radius-cardrounded-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:

src/index.css
@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.