Tailwind 4 css architecture

From CraftOS-dev/craftbot-agent-bundles · View interactive version

Tailwind 4 css architecture is an AI agent skill from the GitHub repository CraftOS-dev/craftbot-agent-bundles. Source: https://tailwindcss.com/blog/tailwindcss-v4 · https://tailwindcss.com/docs

How to use this skill

Skill file

<!--

Source: https://tailwindcss.com/blog/tailwindcss-v4 · https://tailwindcss.com/docs

Authored: June 2026 for the frontend-engineer agent bundle.

-->

Tailwind CSS 4 — CSS architecture

Tailwind CSS 4 (stable Jan 2025) replaced the JS config with **CSS-first

@theme, swapped PostCSS for the Rust-based Lightning CSS** engine

(~10x faster), and gained automatic content detection. This skill also covers

when to prefer Panda, vanilla-extract, or UnoCSS instead.

When to use

system

"CSS-in-TS", "Panda", "vanilla-extract", "UnoCSS"

Decision tree

| Need | Pick |

|---|---|

| Utility-first, fast iteration, ecosystem | Tailwind 4 |

| Type-safe design tokens + zero-runtime + recipes (Chakra-like) | Panda CSS |

| Strictly typed CSS-in-TS, zero-runtime | vanilla-extract |

| Tailwind feel + smaller bundle + faster | UnoCSS |

| Component-scoped without a framework | CSS Modules |

Setup — Tailwind 4 (Vite plugin path)

pnpm add tailwindcss @tailwindcss/vite
// vite.config.ts
import tailwindcss from "@tailwindcss/vite";

export default defineConfig({ plugins: [tailwindcss()] });
/* src/styles/app.css */
@import "tailwindcss";

@theme {
  --color-brand-50: #f0f9ff;
  --color-brand-500: #3b82f6;
  --color-brand-900: #1e3a8a;

  --font-sans: "Inter", "ui-sans-serif", system-ui;
  --font-mono: "JetBrains Mono", "ui-monospace";

  --spacing-128: 32rem;
  --radius-card: 0.75rem;
}
import "./styles/app.css";

Setup — Tailwind 4 (PostCSS path)

pnpm add tailwindcss @tailwindcss/postcss postcss
// postcss.config.mjs
export default { plugins: { "@tailwindcss/postcss": {} } };

Setup — Upgrade from Tailwind 3

pnpm dlx @tailwindcss/upgrade@latest
# - removes tailwind.config.js
# - migrates content paths
# - rewrites class names (gap-x-N → gap-N where applicable)

Verify: pnpm list tailwindcss → 4.x.

Common recipes — Tailwind 4

Recipe 1 — Custom theme tokens

@import "tailwindcss";

@theme {
  --color-primary: oklch(0.72 0.18 250);
  --color-secondary: oklch(0.62 0.12 30);
  --breakpoint-3xl: 120rem;
}

Use as utilities: bg-primary text-secondary. Use the OKLCH color space —

better gamut, perceptually uniform.

Recipe 2 — Dark mode (CSS-based, no config)

@import "tailwindcss";

@custom-variant dark (&:where(.dark, .dark *));
<html className={isDark ? "dark" : ""}>
  <body className="bg-white text-slate-900 dark:bg-slate-950 dark:text-slate-100">
    ...
  </body>
</html>

Recipe 3 — Container queries (built in to v4)

<div class="@container">
  <div class="grid grid-cols-1 @md:grid-cols-2 @xl:grid-cols-3">
    ...
  </div>
</div>

No plugin needed in v4.

Recipe 4 — Group / peer / has / not utilities

<label className="group flex items-center gap-2">
  <input type="checkbox" className="peer" />
  <span className="peer-checked:text-green-500 group-hover:underline">
    Subscribe
  </span>
</label>

<div className="not-data-[active]:opacity-50">...</div>
<div className="has-[:focus]:ring-2">...</div>

Recipe 5 — Arbitrary values + named layers

<div className="grid-cols-[200px_minmax(900px,1fr)_100px] [grid-template-areas:'sidebar_main_aux']">
  ...
</div>
@layer components {
  .btn {
    @apply rounded px-4 py-2 font-medium;
  }
  .btn-primary {
    @apply btn bg-primary text-white hover:bg-primary/90;
  }
}

Recipe 6 — Typography plugin for prose

pnpm add @tailwindcss/typography
@import "tailwindcss";
@plugin "@tailwindcss/typography";
<article className="prose prose-slate dark:prose-invert max-w-prose">
  <h1>Title</h1>
  <p>Body text styled automatically...</p>
</article>

Recipe 7 — Animations + transitions

<div className="transition-all duration-300 ease-out hover:scale-105 hover:shadow-xl" />

<div className="motion-safe:animate-pulse motion-reduce:animate-none" />

Recipe 8 — Conditional className with `clsx` + `tailwind-merge`

pnpm add clsx tailwind-merge
// src/lib/cn.ts
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}
import { cn } from "@/lib/cn";

function Button({ variant, className }: { variant: "primary" | "ghost"; className?: string }) {
  return (
    <button
      className={cn(
        "rounded px-4 py-2 font-medium",
        variant === "primary" && "bg-blue-600 text-white",
        variant === "ghost" && "bg-transparent text-blue-600 hover:bg-blue-50",
        className,
      )}
    />
  );
}

twMerge resolves conflicting Tailwind classes (bg-red-500 bg-blue-500 →

bg-blue-500).

Common recipes — Panda CSS

pnpm add -D @pandacss/dev
pnpm dlx panda init --postcss
// panda.config.ts
import { defineConfig } from "@pandacss/dev";

export default defineConfig({
  preflight: true,
  include: ["./src/**/*.{ts,tsx}"],
  theme: {
    extend: {
      tokens: {
        colors: { brand: { value: "#3b82f6" } },
      },
    },
  },
  outdir: "styled-system",
});
// src/components/Card.tsx
import { css } from "../../styled-system/css";

export function Card() {
  return <div className={css({ bg: "brand", p: "4", rounded: "md", color: "white" })}>...</div>;
}

Build-time CSS-in-TS — zero runtime, fully typed tokens.

Common recipes — vanilla-extract

pnpm add -D @vanilla-extract/css @vanilla-extract/vite-plugin
// vite.config.ts
import { vanillaExtractPlugin } from "@vanilla-extract/vite-plugin";
export default defineConfig({ plugins: [vanillaExtractPlugin()] });
// src/components/Button.css.ts
import { style } from "@vanilla-extract/css";

export const button = style({
  borderRadius: 6,
  padding: "0.5rem 1rem",
  background: "#3b82f6",
  color: "white",
});
import * as styles from "./Button.css";
export const Button = (props) => <button className={styles.button} {...props} />;

Strictly typed, zero-runtime, type-checked at build.

Common recipes — UnoCSS

pnpm add -D unocss
// vite.config.ts
import UnoCSS from "unocss/vite";
export default defineConfig({ plugins: [UnoCSS()] });
// uno.config.ts
import { defineConfig, presetUno } from "unocss";
export default defineConfig({ presets: [presetUno()] });
import "virtual:uno.css";

export const App = () => <div className="p-4 bg-blue-500 text-white">UnoCSS</div>;

Tailwind-compatible syntax + faster + smaller. Good when Tailwind feels heavy.

Examples

Example 1: shadcn/ui-style design tokens in Tailwind 4

@import "tailwindcss";

@theme {
  --color-background: oklch(1 0 0);
  --color-foreground: oklch(0.15 0 0);
  --color-primary: oklch(0.4 0.18 250);
  --color-primary-foreground: oklch(0.98 0 0);
  --color-muted: oklch(0.96 0.005 250);
  --color-muted-foreground: oklch(0.5 0.02 250);
  --color-border: oklch(0.9 0.005 250);
  --radius: 0.5rem;
}

.dark {
  --color-background: oklch(0.15 0 0);
  --color-foreground: oklch(0.98 0 0);
  /* ... */
}

shadcn components reference these tokens (bg-background, border-border).

Example 2: Migrate `tailwind.config.js` to CSS-first

Before (v3):

module.exports = {
  theme: { extend: { colors: { brand: "#3b82f6" } } },
};

After (v4):

@import "tailwindcss";

@theme {
  --color-brand: #3b82f6;
}

Delete tailwind.config.js. Content paths are detected automatically.

Edge cases / gotchas

colors, container queries.

@config "./tailwind.config.js"; only when needed.

the upgrade tool, don't hand-edit.

utilities in JSX with cn() helper.

function or skip it (accept duplicate classes).

but PostCSS path is needed if you compose with other PostCSS plugins

(autoprefixer, postcss-nesting).

specificity wars — pick one primary, document deviations.

(pnpm dlx panda codegen).

no-build-step projects.

in @theme adjusts every responsive variant.

Sources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.