Rules for tailwind v4
This rule provides guidance for transitioning from Tailwind CSS v3 to v4. It should be applied when:
/* v3 (old) */
@tailwind base;
@tailwind components;
@tailwind utilities;
/* v4 (new) */
@import "tailwindcss";
/* v3 (old) */
.my-class {
background-color: theme(colors.red.500);
}
/* v4 (new) */
.my-class {
background-color: var(--color-red-500);
}
<!-- v3 (old) - right to left -->
<ul class="py-4 *:first:pt-0 *:last:pb-0">
<!-- v4 (new) - left to right -->
<ul class="py-4 first:*:pt-0 last:*:pb-0">
<!-- v3 (old) -->
<div class="bg-[--brand-color]"></div>
<!-- v4 (new) -->
<div class="bg-(--brand-color)"></div>
/* v3 (old) */
@layer utilities {
.tab-4 {
tab-size: 4;
}
}
/* v4 (new) */
@utility tab-4 {
tab-size: 4;
}
For Vue, Svelte, or CSS modules files:
<!-- v4 approach for component styles -->
<style>
/* Import definitions without duplicating CSS */
@reference "../../app.css";
h1 {
@apply text-2xl font-bold text-red-500;
}
/* Or better, use CSS variables directly */
p {
color: var(--text-red-500);
font-size: var(--font-size-lg);
}
</style>
Use the official upgrade tool for automated migration:
npx @tailwindcss/upgrade
@tailwindcss/postcss
@tailwindcss/vite
@tailwindcss/cli
theme()
function except for media queries@reference
for component styles@utility
syntax@Source: Tailwind CSS Upgrade Guide