StackKit

Tailwind CSS tips that save hours of styling

By · · Updated · 3 min read

These tips target Tailwind CSS v4. If you are on v3, configuration lives in tailwind.config.js and the content option replaces the old purge setting.

Getting Started

With Vite, install the Tailwind plugin and add it to vite.config.ts:

npm install tailwindcss @tailwindcss/vite
import { defineConfig } from "vite";
import tailwindcss from "@tailwindcss/vite";

export default defineConfig({ plugins: [tailwindcss()] });

Then add @import "tailwindcss"; to your main CSS file. Frameworks that use PostCSS, including Next.js, use the @tailwindcss/postcss package instead. A tailwind.config.js file is no longer required.

Define Your Design Tokens in CSS

In v4 you configure the theme in your stylesheet with the @theme directive. Each variable becomes available as utility classes.

@import "tailwindcss";

@theme {
  --color-brand: #2563eb;
  --font-display: "Inter", sans-serif;
  --breakpoint-3xl: 120rem;
}

This gives you bg-brand, text-brand, font-display and a 3xl: variant. Because the values are also CSS variables, you can use them in plain CSS with var(--color-brand).

Use @apply Sparingly

@apply copies utilities into your own CSS class, which is useful for styling markup you do not control, such as rendered Markdown:

.prose a {
  @apply text-brand underline underline-offset-2;
}

For your own components, extract a component (a React component or template partial) instead of a CSS class. That keeps the styling next to the markup and avoids growing a second stylesheet.

Never Build Class Names Dynamically

Tailwind finds classes by scanning your source files for complete strings. A name assembled at runtime is never seen, so no CSS is generated for it:

// Broken: "bg-red-500" never appears in the source as a whole string
<div className={`bg-${color}-500`} />

// Works: every class is written out in full
const colors = { red: "bg-red-500", blue: "bg-blue-500" };
<div className={colors[color]} />

Arbitrary Values for One-Offs

When a design needs a value that is not in the scale, use square brackets instead of writing custom CSS:

<div class="w-[137px] grid-cols-[200px_1fr] bg-[#1da1f2] top-[calc(100%-2rem)]">

If you find yourself repeating the same arbitrary value, add it to @theme.

Style Children and Siblings with group and peer

Mark a parent with group to style children when it is hovered or focused, and mark a sibling with peer to react to its state:

<a class="group flex items-center gap-2">
  <span class="group-hover:translate-x-1 transition">→</span>
</a>

<input type="checkbox" class="peer" />
<label class="peer-checked:font-bold">Subscribe</label>

Class-Based Dark Mode

By default dark: follows the operating system setting. To control it with a .dark class, for example from a toggle button, declare a custom variant:

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

Then toggle the class with document.documentElement.classList.toggle("dark").

Add Your Own Utilities

Use @utility to create a utility that works with every variant, such as hover: and md::

@utility tab-4 {
  tab-size: 4;
}

Keep Class Lists Tidy

  • Install prettier-plugin-tailwindcss, which sorts classes into a consistent order automatically.
  • Use tailwind-merge together with clsx when a component accepts a className prop, so that later classes override earlier conflicting ones (for example p-2 followed by p-4).
  • Tailwind v4 detects unused styles on its own, so there is no purge option and no need for PurgeCSS. If classes live in a package outside your project, point Tailwind at it with @source "../node_modules/some-ui-lib";.

Debugging a Class That Does Nothing

If a utility has no effect, check three things. First, the class name must appear in your source as a complete string (see the dynamic class names tip above). Second, the file must be one Tailwind scans: automatic detection skips files listed in .gitignore, so a template inside an ignored folder needs an explicit @source line. Third, restart the dev server if you just changed the CSS entry file, since a stale build is a common cause.

Summary

Move configuration into @theme, write complete class names, reach for components before @apply, and let the Prettier plugin keep class order consistent. Those four habits remove most of the friction people hit when starting with Tailwind.