Upgrading from Tailwind CSS v3 to v4 to v3 to v4: A Comprehensive Guide
Tailwind CSS v4 is a major update to the popular utility-first CSS framework. It introduces a number of new features and improvements, including a new high-performance engine, simplified installation, automatic content detection, and CSS-first configuration. This update significantly enhances performance, streamlines development workflows, and provides developers with more powerful tools for building modern user interfaces.
This guide will walk you through the process of upgrading from Tailwind CSS v3 to v4 to v3 to v4. It will cover the following topics:
Using the upgrade tool
Manually updating your project
Best practices for upgrading
Breaking changes in v4
New features in v4
Troubleshooting
Using the upgrade tool
Manually updating your project
Best practices for upgrading
Breaking changes in v4
New features in v4
Troubleshooting
Using the Upgrade Tool
The easiest way to upgrade a Tailwind v3 project to v4 is to use the official upgrade tool. This tool can automatically handle most of the changes for you, including:
Updating your dependencies
Migrating your configuration file to CSS
Making any necessary changes to your template files
Updating your dependencies
Migrating your configuration file to CSS
Making any necessary changes to your template files
To use the upgrade tool, simply run the following command in your terminal:
────────────────────────────────────────────────────────────────────
bashCopy codenpx @tailwindcss/upgrade@next
The upgrade tool requires Node.js 20 or higher. It's always a good idea to run the upgrade tool in a new branch so you can carefully review the changes and test your project before merging.
Manually Updating Your Project
If the upgrade tool doesn't work for your project, or if you prefer a more hands-on approach, you can manually update your project by following these steps:
Update your dependencies.You'll need to update thetailwindcsspackage to the@tailwindcss/postcsspackage. You can do this by running the following command in your terminal:bashCopy codenpm install -D @tailwindcss/postcss @tailwindcss/cli
Remove postcss-import and autoprefixer.In v3, the Tailwind CSS package was a PostCSS plugin, but in v4 the PostCSS plugin lives in a dedicated@tailwindcss/postcsspackage. Additionally, in v4 imports and vendor prefixing are now handled for you automatically, so you can remove postcss-import and autoprefixer if they are in your project.
Migrate to the plugin (if using Vite).If you're using , it is recommended to migrate from the PostCSS plugin to the new dedicated plugin for improved performance and the best developer experience:javascriptCopy code// vite.config.tsimport{ defineConfig }from"vite";importtailwindcssfrom"@tailwindcss/vite";exportdefaultdefineConfig({plugins: [tailwindcss()],
});This dedicated plugin offers tighter integration with , resulting in faster build times and a smoother development process.
Update any Tailwind CLI commands to use @tailwindcss/ cli.In v4, lives in a dedicated@tailwindcss/clipackage. Update any of your build commands to use the new package instead:bashCopy codenpx tailwindcss -i input.css -o output.css
npx @tailwindcss/cli -i input.css -o output.css
Remove @tailwind directives.In v4 you import Tailwind using a regular CSS@importstatement, not using the@tailwinddirectives you used in v3:cssCopy code@tailwindbase;@tailwindcomponents;@tailwindutilities;@import"tailwindcss";
Update your dependencies.You'll need to update thetailwindcsspackage to the@tailwindcss/postcsspackage. You can do this by running the following command in your terminal:
────────────────────────────────────────────────────────────────────
bashCopy codenpm install -D @tailwindcss/postcss @tailwindcss/cli
Remove postcss-import and autoprefixer.In v3, the Tailwind CSS package was a PostCSS plugin, but in v4 the PostCSS plugin lives in a dedicated@tailwindcss/postcsspackage. Additionally, in v4 imports and vendor prefixing are now handled for you automatically, so you can remove postcss-import and autoprefixer if they are in your project.
Migrate to the plugin (if using Vite).If you're using , it is recommended to migrate from the PostCSS plugin to the new dedicated plugin for improved performance and the best developer experience:
────────────────────────────────────────────────────────────────────
javascriptCopy code// vite.config.ts
import { defineConfig } from "vite";
import tailwindcss from "@tailwindcss/vite";
export default defineConfig({
plugins: [tailwindcss()],
});
This dedicated plugin offers tighter integration with , resulting in faster build times and a smoother development process.
Update any Tailwind CLI commands to use @tailwindcss/ cli.In v4, lives in a dedicated@tailwindcss/clipackage. Update any of your build commands to use the new package instead:
────────────────────────────────────────────────────────────────────
bashCopy codenpx tailwindcss -i input.css -o output.css
npx @tailwindcss/cli -i input.css -o output.css
Remove @tailwind directives.In v4 you import Tailwind using a regular CSS@importstatement, not using the@tailwinddirectives you used in v3:
────────────────────────────────────────────────────────────────────
cssCopy code@tailwind base;
@tailwind components;
@tailwind utilities;
@import "tailwindcss";
For a detailed explanation of the changes and upgrade process, refer to the official Tailwind CSS v4 upgrade guide.
Best Practices for Upgrading
Here are some best practices to ensure a smooth and successful upgrade:
Use the upgrade tool if possible.The upgrade tool can save you a lot of time and effort by automating many of the required changes.
Read the upgrade guide carefully.The official Tailwind CSS v4 upgrade guide provides comprehensive information about the breaking changes and new features in v4. Familiarize yourself with these changes before starting the upgrade process.
Test your project thoroughly.After upgrading, be sure to test your project thoroughly to make sure that everything is working as expected. Pay close attention to any visual regressions or unexpected behavior.
Consider using a CSS-in-JS library.If you're using a CSS-in-JS library, such as Styled Components or Emotion, you can use the@applydirective to apply Tailwind utilities to your styled components. This can help you maintain a consistent design language throughout your project.
Use the upgrade tool if possible.The upgrade tool can save you a lot of time and effort by automating many of the required changes.
Read the upgrade guide carefully.The official Tailwind CSS v4 upgrade guide provides comprehensive information about the breaking changes and new features in v4. Familiarize yourself with these changes before starting the upgrade process.
Test your project thoroughly.After upgrading, be sure to test your project thoroughly to make sure that everything is working as expected. Pay close attention to any visual regressions or unexpected behavior.
Consider using a CSS-in-JS library.If you're using a CSS-in-JS library, such as Styled Components or Emotion, you can use the@applydirective to apply Tailwind utilities to your styled components. This can help you maintain a consistent design language throughout your project.
Breaking Changes in v4
Tailwind CSS v4 introduces several breaking changes aimed at improving performance, consistency, and maintainability. While the upgrade tool handles most of these automatically, it's essential to be aware of them:
Utility Changes
Removed deprecated utilities.Several outdated or undocumented utilities have been removed and replaced with modern alternatives. For example,bg-opacity-*has been replaced with opacity modifiers likebg-black/50.
Renamed utilities.Some utilities have been renamed for better consistency and predictability. For instance,shadow-smis nowshadow-xs, andoutline-noneis nowoutline-0.
Updated shadow, radius, and blur scales.The default scales for shadows, border-radius, and blur have been updated. The "bare" versions still work for backward compatibility, but the-smutilities will look different unless updated to their respective-xsversions.
Removed deprecated utilities.Several outdated or undocumented utilities have been removed and replaced with modern alternatives. For example,bg-opacity-*has been replaced with opacity modifiers likebg-black/50.
Renamed utilities.Some utilities have been renamed for better consistency and predictability. For instance,shadow-smis nowshadow-xs, andoutline-noneis nowoutline-0.
Updated shadow, radius, and blur scales.The default scales for shadows, border-radius, and blur have been updated. The "bare" versions still work for backward compatibility, but the-smutilities will look different unless updated to their respective-xsversions.
Configuration Changes
Removed @tailwind directives.In v4, you import Tailwind using a regular CSS@importstatement instead of the@tailwinddirectives used in v3.
Changed the space-between selector.The selector used by thespace-x-*andspace-y-*utilities has been changed to address performance issues on large pages.
Updated how variants are used with gradients.In v3, overriding part of a gradient with a variant would "reset" the entire gradient. This behavior has been changed in v4 to allow for more granular control over gradient modifications.
Changed how prefixes are used.In v4, you can no longer use a prefix to target arbitrary values. For example,tw-w-1/2is no longer valid; usew-1/2instead.
Changed how variables are used in arbitrary values.You can no longer use variables in arbitrary values likew-[var(--width)]. Instead, use a CSS variable directly, such aswidth: var(--width).
Changed how hover styles are applied on mobile.Hover styles are now applied on mobile devices when you tap and hold an element.
Added the outline-color property to the transition and transition-color utilities.If you're transitioning the outline-color of an element, you'll need to explicitly include it in your transition properties.
Removed the corePlugins option.To disable a core plugin, use the@layerdirective.
Changed how the theme() function is used.Thetheme()function now aligns with how CSS variables are used.
Removed the resolveConfig function.Use the@tailwindcss/resolveConfigpackage to access the resolved Tailwind config.
Changed how @apply is used with , Svelte, or CSS modules.The@applydirective has been updated for consistency with CSS variable usage.
Removed @tailwind directives.In v4, you import Tailwind using a regular CSS@importstatement instead of the@tailwinddirectives used in v3.
Changed the space-between selector.The selector used by thespace-x-*andspace-y-*utilities has been changed to address performance issues on large pages.
Updated how variants are used with gradients.In v3, overriding part of a gradient with a variant would "reset" the entire gradient. This behavior has been changed in v4 to allow for more granular control over gradient modifications.
Changed how prefixes are used.In v4, you can no longer use a prefix to target arbitrary values. For example,tw-w-1/2is no longer valid; usew-1/2instead.
Changed how variables are used in arbitrary values.You can no longer use variables in arbitrary values likew-[var(--width)]. Instead, use a CSS variable directly, such aswidth: var(--width).
Changed how hover styles are applied on mobile.Hover styles are now applied on mobile devices when you tap and hold an element.
Added the outline-color property to the transition and transition-color utilities.If you're transitioning the outline-color of an element, you'll need to explicitly include it in your transition properties.
Removed the corePlugins option.To disable a core plugin, use the@layerdirective.
Changed how the theme() function is used.Thetheme()function now aligns with how CSS variables are used.
Removed the resolveConfig function.Use the@tailwindcss/resolveConfigpackage to access the resolved Tailwind config.
Changed how @apply is used with , Svelte, or CSS modules.The@applydirective has been updated for consistency with CSS variable usage.
Theme Changes
Changed the container configuration.The container utility no longer has options like center and padding. Customize it using the@utilitydirective.
Changed the default border color.
Changed the default ring width and color.
Made changes to preflight.Preflight has been adjusted, including removing the default border-color from form elements and changing the default line-height for buttons.
Changed the default placeholder color.
Buttons now use the default cursor.
Removed dialog margins.
Changed the variant stacking order.
Dark mode behavior changed.Tailwind v4 uses theprefers-color-schememedia feature for dark mode by default. To enable class-based dark mode, use the@custom-variantdirective:cssCopy code@custom-variantdark (&:where(.dark, .dark *));
Changed the container configuration.The container utility no longer has options like center and padding. Customize it using the@utilitydirective.
Changed the default border color.
Changed the default ring width and color.
Made changes to preflight.Preflight has been adjusted, including removing the default border-color from form elements and changing the default line-height for buttons.
Changed the default placeholder color.
Buttons now use the default cursor.
Removed dialog margins.
Changed the variant stacking order.
Dark mode behavior changed.Tailwind v4 uses theprefers-color-schememedia feature for dark mode by default. To enable class-based dark mode, use the@custom-variantdirective:
────────────────────────────────────────────────────────────────────
cssCopy code@custom-variant dark (&:where(.dark, .dark *));
Potential for Larger CSS Output
Due to the inclusion of CSS variables and@propertydefinitions, Tailwind v4 might produce slightly larger CSS output than v3. However, this can be mitigated by using PurgeCSS to remove unused styles.
New Features in v4
Tailwind CSS v4 comes with a host of new features and improvements that enhance performance, streamline workflows, and provide developers with more powerful tools:
Performance Enhancements
New high-performance engine.
Smaller builds with optimized PurgeCSS.
New high-performance engine.
Smaller builds with optimized PurgeCSS.
Modern CSS and Developer Experience
Designed for the modern web.Tailwind CSS v4 leverages cutting-edge CSS features like cascade layers, registered custom properties with@property, andcolor-mix().
Simplified installation.With fewer dependencies and zero configuration, setting up Tailwind CSS v4 is easier than ever.
CSS-first configuration.Configure and extend Tailwind directly in CSS instead of a JavaScript configuration file, simplifying customization and aligning with modern CSS practices.
CSS theme variables.All design tokens are exposed as native CSS variables, making them accessible throughout your project.
Built-in import support.Manage third-party plugins and custom styles more easily with built-in import handling, eliminating the need for additional tools like PostCSS.
Automatic vendor prefixing and syntax transformations.Ensure cross-browser compatibility without manual effort, as vendor prefixes and syntax transformations are handled automatically.
Automatic content detection.Tailwind CSS v4 automatically discovers all your template files, eliminating the need for manual configuration and saving you time during development.
Designed for the modern web.Tailwind CSS v4 leverages cutting-edge CSS features like cascade layers, registered custom properties with@property, andcolor-mix().
Simplified installation.With fewer dependencies and zero configuration, setting up Tailwind CSS v4 is easier than ever.
CSS-first configuration.Configure and extend Tailwind directly in CSS instead of a JavaScript configuration file, simplifying customization and aligning with modern CSS practices.
CSS theme variables.All design tokens are exposed as native CSS variables, making them accessible throughout your project.
Built-in import support.Manage third-party plugins and custom styles more easily with built-in import handling, eliminating the need for additional tools like PostCSS.
Automatic vendor prefixing and syntax transformations.Ensure cross-browser compatibility without manual effort, as vendor prefixes and syntax transformations are handled automatically.
Automatic content detection.Tailwind CSS v4 automatically discovers all your template files, eliminating the need for manual configuration and saving you time during development.
New Utilities and Variants
Dynamic utility values and variants.
Modernized P3 color palette.
Container queries.
New 3D transform utilities.
Expanded gradient APIs.
@starting-style support.
not-variant.*
New inset-shadow-and inset-ring-utilities.**
New field-sizing utilities.
New color-scheme utilities.
New font-stretch utilities.
New inert variant.
New nth-variants.*
New in-variant.*
Support for :popover-open with the open variant.
New descendant variant.
Linear gradient angles.
Gradient interpolation modifiers.
Conic and radial gradients.
Dynamic utility values and variants.
Modernized P3 color palette.
Container queries.
New 3D transform utilities.
Expanded gradient APIs.
@starting-style support.
not-variant.*
New inset-shadow-and inset-ring-utilities.**
New field-sizing utilities.
New color-scheme utilities.
New font-stretch utilities.
New inert variant.
New nth-variants.*
New in-variant.*
Support for :popover-open with the open variant.
New descendant variant.
Linear gradient angles.
Gradient interpolation modifiers.
Conic and radial gradients.
Troubleshooting
While the upgrade process is generally smooth, some users have reported issues, particularly when using UI libraries like Shadcn. These libraries often rely on thetailwind.config.jsfile, which is no longer automatically detected in v4. In some cases, downgrading to Tailwind CSS v3 might be necessary until these libraries are updated to support v4.
Conclusion
Upgrading from Tailwind CSS v3 to v4 to v3 to v4 is a worthwhile endeavor that unlocks a plethora of performance enhancements, modern CSS features, and developer experience improvements. By following the steps outlined in this guide and familiarizing yourself with the breaking changes, you can seamlessly transition to the latest version and leverage its powerful new capabilities to build even better user interfaces.