Tailwind CSS
Style components with utility classes and a shared theme.
By Invicly Tech, , in Frontend with React & Next.js. About 10 minutes to read.
In short
Tailwind CSS styles elements with small utility classes like p-4 and text-lg written directly in markup. It generates only the CSS you use, from a shared theme of colors, spacing and breakpoints.
Explained simply
Normally you write CSS in one file and class names in another, then jump between them. Tailwind turns this around. It gives you thousands of tiny ready-made classes, each doing one job: p-4 adds padding, text-lg makes text bigger, rounded-xl rounds the corners. You put them straight on the element.
This sounds messy at first, but it has a big benefit. Everything about how a component looks is in one place, so you can change it without fear of breaking another page. You also stop inventing class names like card-wrapper-inner.
Tailwind only ships the classes you actually use. It reads your source files, finds the class names, and builds one small CSS file. A huge site often ends up with a stylesheet of just a few kilobytes after compression.
The values come from a shared theme: one spacing scale, one color palette, one set of screen sizes. Because everyone picks from the same menu, the whole app looks consistent, and changing a brand color is a one-line edit.
Press Play on a diagram to walk through it one step at a time.
- Step 1Write classes
e.g. flex gap-4 md:grid-cols-3
- Step 2Scan source files
Tailwind finds class names in your code
- Step 3Generate CSS
Only the utilities you used are emitted
- Step 4Ship small CSS
One compact stylesheet for the whole app
- Class names you invent
- Styles live in another file
- Easy to leave dead CSS behind
- Global names can clash
- Scoped class names per file
- Still a separate stylesheet
- No shared scale unless you add one
- Built into Next.js
- Classes from a shared theme
- Styles sit on the markup
- Unused CSS is never generated
- Reuse by making components
In depth
Setting up Tailwind v4 in Next.js
Tailwind v4 is configured in CSS, not in a JavaScript file. If you run create-next-app and answer yes to Tailwind, it is set up for you. To add it by hand, install tailwindcss and @tailwindcss/postcss, add the plugin to postcss.config.mjs, and put one line at the top of app/globals.css: @import "tailwindcss". The root layout imports globals.css, so every page gets the styles.
You no longer list your files in a content array. Tailwind v4 detects source files automatically and skips anything in .gitignore. If classes live somewhere it cannot see, such as a UI package inside node_modules, add an @source line pointing at that folder.
- npm install tailwindcss @tailwindcss/postcss postcss
- postcss.config.mjs: plugins: { "@tailwindcss/postcss": {} }
- globals.css: @import "tailwindcss";
- @source "../node_modules/my-ui"; scans extra folders
- 1globals.css
Starts with @import "tailwindcss"
- 2PostCSS plugin
@tailwindcss/postcss runs during the build
- 3Source scan
Finds class names in app and components
- 4Generated CSS
Only used utilities plus theme variables
- 5Root layout
Imports globals.css for every route
How variants stack
A variant is a prefix that adds a condition. You can chain them, and they read left to right: md:hover:bg-blue-700 means on medium screens and wider, when hovered. Breakpoints are mobile first. A class without a prefix applies everywhere, and md: kicks in from 48rem (768px) up. The defaults are sm 40rem, md 48rem, lg 64rem, xl 80rem and 2xl 96rem.
To target only small screens, use max-md:, which applies below md. There are also state variants (focus-visible:, disabled:, aria-expanded:), parent and sibling variants (group-hover: and peer-invalid:), and container queries: mark a parent with @container and use @md: on children so a card responds to its own width, not the window.
- Unprefixed classes are the mobile style; add md: and lg: for bigger screens
- max-md:hidden hides an element below the md breakpoint
- group on a parent plus group-hover:underline on a child
- peer on an input plus peer-invalid:block on its error message
The theme is just CSS variables
In v4 you extend the theme with an @theme block in your CSS. Each variable you add creates utilities. --color-brand: oklch(0.55 0.2 260) gives you bg-brand, text-brand, border-brand and so on. --font-display creates font-display, and --breakpoint-3xl: 120rem creates a 3xl: variant.
Spacing works from one base value, --spacing, which is 0.25rem by default. p-4 is 4 times that, so 1rem. Every theme value is also a real CSS variable at runtime, so you can use var(--color-brand) in plain CSS or inline styles.
For one-off values use arbitrary values in square brackets, like w-[37px] or grid-cols-[200px_1fr], where an underscore means a space. If you write the same arbitrary value twice, it probably belongs in the theme.
- @theme { --color-brand: oklch(0.55 0.2 260); }
- bg-(--my-var) uses a CSS variable directly
- @utility defines your own utility that works with variants
Keeping class lists maintainable
Long class lists are fine inside a component, but repeated lists are a smell. The main fix is to make a component, not a CSS class: one Button component holds the classes once, and every page uses it.
When classes depend on props, never build names from pieces like "bg-" + color, because the scanner only sees complete strings. Use a lookup object that maps each variant to full class names. Two small libraries help: clsx joins classes conditionally, and tailwind-merge resolves conflicts so a passed-in p-2 replaces the default p-4 instead of fighting it. Many teams wrap them in a helper called cn. The official Prettier plugin, prettier-plugin-tailwindcss, sorts classes into a consistent order.
- Map props to full class strings: { primary: "bg-blue-600", danger: "bg-red-600" }
- cn(...inputs) = twMerge(clsx(inputs)) is a common helper
- @apply exists, but use it sparingly, mostly for third-party markup
Learn it in 4 short steps
About 45m each. Track them with a timer in your plan.
- 1Set up Tailwind in Next.js~45m
- 2Style components with utility classes~45m
- 3Build responsive variants~45m
- 4Extend the theme with custom tokens~45m
Key points
- Utility classes keep styles next to markup, so components are self-contained.
- Responsive prefixes like md: apply styles from that breakpoint up.
- State variants like hover: and focus-visible: style interactions.
- Extend the theme with your own tokens instead of hard-coding arbitrary values.
- Class names must appear as complete strings so the scanner can find them.
Key terms
Tap a card to flip it. 0/4 known
Examples
export function Card({ title }: { title: string }) {
return (
<div className="rounded-xl border p-4 shadow-sm hover:shadow-md md:p-6">
<h2 className="text-lg font-semibold text-gray-900 dark:text-white">
{title}
</h2>
<button className="mt-4 rounded bg-blue-600 px-3 py-2 text-white focus-visible:outline-2">
Open
</button>
</div>
);
}Spacing, hover, responsive and dark variants in markup.
A responsive product grid
You want one column on phones, two on tablets and four on desktops, with the gap growing on larger screens. Step 1: write the mobile layout with no prefix: grid grid-cols-1 gap-4. Step 2: add sm:grid-cols-2 for screens from 40rem. Step 3: add lg:grid-cols-4 lg:gap-6 for 64rem and up. Step 4: give each card a hover and keyboard focus style, so it works for mouse and keyboard users. Resize the browser and watch the columns change at each breakpoint.
export function ProductGrid({ items }: { items: { id: string; name: string }[] }) {
return (
<ul className="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-4 lg:gap-6">
{items.map((p) => (
<li key={p.id}>
<a
href={`/products/${p.id}`}
className="block rounded-lg border p-4 hover:shadow-md focus-visible:outline-2 focus-visible:outline-blue-600"
>
{p.name}
</a>
</li>
))}
</ul>
);
}Adding brand tokens to the theme
Your designer gives you a brand color, a darker hover shade and a display font. Step 1: open app/globals.css under the import line. Step 2: add an @theme block with --color-brand, --color-brand-dark and --font-display. Step 3: use them as normal classes: bg-brand, hover:bg-brand-dark, font-display. Step 4: when the brand color changes, edit one line and every button updates. The same variables are available as var(--color-brand) for any custom CSS.
@import "tailwindcss";
@theme {
--color-brand: oklch(0.55 0.2 260);
--color-brand-dark: oklch(0.45 0.2 260);
--font-display: "Inter", sans-serif;
--breakpoint-3xl: 120rem;
}
/* Usage: <button class="bg-brand hover:bg-brand-dark font-display 3xl:px-8"> */Quick reference
- Tailwind v4 setup: @import "tailwindcss" in your CSS plus the @tailwindcss/postcss plugin
- Mobile first: no prefix applies to all sizes, md: applies from 48rem (768px) up
- max-md: applies only below the md breakpoint
- @theme { --color-x: ... } creates bg-x, text-x, border-x and a CSS variable
- Spacing: p-4 = 4 x --spacing (0.25rem) = 1rem
- Arbitrary values: w-[37px], grid-cols-[200px_1fr] (underscore means space)
- Never build class names from parts; map props to complete class strings
- cn = twMerge(clsx(...)) merges conditional classes and resolves conflicts
Common mistakes
- Building class names dynamically like 'text-' + color, which the scanner cannot see.
- Copying long class lists everywhere instead of extracting a component.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Style a button that is blue, white text, rounded, slightly darker on hover, and shows a clear outline only for keyboard focus.
- Exercise 2medium
Build a page header that stacks the logo and nav vertically on phones and puts them side by side from md up, with the nav hidden below sm.
- Exercise 3hard
Write an Alert component with a tone prop (info, success, danger) that maps to full class strings and lets callers override classes safely.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Does Tailwind make my HTML too big?
- Class lists add some bytes, but HTML compresses very well because the same class names repeat. In practice the small CSS file usually saves more than the extra markup costs.
- Where did tailwind.config.js go in Tailwind v4?
- v4 moves configuration into CSS with @theme, @source and @utility. A JavaScript config still works if you load it with @config, which helps when migrating older projects.
- Can I use Tailwind with CSS Modules or plain CSS?
- Yes. They mix fine, and theme values are real CSS variables you can use anywhere. In a separate CSS Module that uses @apply, add @reference to your main stylesheet so Tailwind knows the theme.
- Is Tailwind good for accessibility?
- Tailwind only handles looks. You still need semantic HTML, labels and keyboard support, but variants like focus-visible: and aria-*: make accessible states easy to style.