Loading Results...
Loading Results...
NK Desk, 2025-07-29
Tailwind CSS v4 brings significant improvements to performance, consistency, and design capabilities. While these upgrades are powerful, migrating from v3 to v4 requires some deliberate adjustments. This guide will walk you through the major changes, compatibility concerns, and steps required to migrate smoothly from Tailwind CSS v3 to v4.
Tailwind CSS v4 is a complete rewrite with the goal of being significantly faster and more consistent. Some of the main benefits include:
- **Built-in support for CSS variables using OKLCH color space**
- **Smaller final builds due to smarter content scanning**
- **Improved IntelliSense and TypeScript support**
- **Support for logical properties (e.g., `start`, `end` instead of `left`, `right`)**
- **Better defaults for modern UI development**
There are several breaking changes and improvements in v4 that you need to understand before upgrading:
Classes like `bg-primary` or `text-secondary` no longer work out of the box. Instead, you need to explicitly define your color palette in `tailwind.config.js`.
v4 now uses the OKLCH color space for better contrast and consistency. You can define your colors as:
`--primary: oklch(0.65 0.25 30);` and use it in Tailwind as `oklch(var(--primary) / <alpha-value>)`.
Tailwind now uses an improved content scanning system to remove unused classes more accurately, resulting in smaller builds.
`tailwind.config.js` should now define colors, borderRadius, and fontFamily extensions using CSS variables for consistency with libraries like Shadcn.
Run the following command to upgrade to Tailwind v4:
`pnpm add tailwindcss@latest`
or using npm:
`npm install tailwindcss@latest`
Make sure to update your config file with a new color strategy. Here is a sample setup:
```js
theme: {
extend: {
colors: {
primary: 'oklch(var(--primary) / <alpha-value>)',
background: 'oklch(var(--background) / <alpha-value>)',
},
}
}
```
Classes like `bg-primary`, `text-accent`, etc., need to be manually defined or replaced with new equivalents. Update all references accordingly.
If you're using `:root` variables (like with Shadcn), make sure your `globals.css` is updated and matches the variable names used in your config.
Once everything is updated, build and test your project thoroughly to make sure all styles are rendering correctly and no class is broken.
- Use Tailwind's upgrade guide and changelog for reference.
- Convert one component at a time for large codebases.
- Use tools like Tailwind IntelliSense to detect missing or invalid classes.
- Leverage the new logical properties for better RTL support.
Migrating from Tailwind CSS v3 to v4 may seem daunting, but with a structured approach, you can take advantage of the latest performance, design, and theming capabilities. Make sure to test thoroughly and gradually update your components to ensure stability.