Upgrading to Tailwind CSS 4: A Quick Guide






In this article we'll learn about the best practices for using the Tailwind CSS Typography plugin to create amazing page styling.
Learn how to overlay text on an image using Tailwind CSS utility classes to design elegant imagery.

Learn how to use Tailwind grid to build responsive and flexible layouts for mobile and desktop websites.

Learn how Tailwind CSS supports various types of theming and alternative theming libraries.
Get the latest articles delivered to your inbox. No spam, unsubscribe anytime.
Tailwind CSS has released version 4 and it brings with it many powerful new features. After recently upgrading to the latest version, the process was not as clear cut as I had initially thought, so I wanted to share some of they key challenges I faced through the upgrade process and how to resolve them.
In this post, I'll walk you through some of the essential changes to help upgrade your project with minimal friction, including:
On that note, let's take a look at how to upgrade Tailwind CSS to take advantage of the enhanced features in v4.
At a high level, there will be several breaking changes. The good news is that Tailwind CSS provides an upgrade tool that will do a bulk of the work, including:
It's best to do this on a clean branch, so you can review what's changed and make adjustments.
The tool will do a lot of pattern matching and it might change things inadvertently and so you'll need to check the UI to make sure it still looks as expected.
To run the upgrade process, use the command:
1npx @tailwindcss/upgrade
Once the tool has completed running the upgrade, let's breakdown what's changed.
The PostCSS configuration postcss.config.mjs (or postcss.config.js if using CommonJS) will have changed and be a little more streamlined.
In v3, it would look like this:
1export default {2 plugins: {3 "postcss-import": {},4 tailwindcss: {},5 autoprefixer: {},6 },7};
In v4, it only needs the following.
1export default {2 plugins: {3 '@tailwindcss/postcss': {},4 },5}
The Tailwind upgrade tool will automatically update your main CSS file with the new directives used in v4.
In v3, the CSS directives are used as follows:
1@tailwind base;2@tailwind components;3@tailwind utilities;45/* other Tailwind CSS here*/
In v4, it has changed to:
1@import 'tailwindcss';23/* other Tailwind CSS here*/
This import will automatically set all of the inital base settings under the hood.
In v3, we're probably all familiar with the JavaScript config tailwind.config.js file for defining theme details.
In v4, this file is still supported for backwards compatibility but now you'll need to explicitly use the @config directive to use it in v4.
You will now see the following similar changes.
1@import 'tailwindcss';23/* path to tailwind.config file */4@config '../../../tailwind.config.js';56/* other Tailwind CSS here*/
At first glance it may seem complex but this is a strategic move to make Tailwind v4 a CSS-only config approach.
The good news is that you can still use the JavaScript config and you may find in some cases you'll still need it as we'll see.
We'll take a look at some examples for defining themes in v4 later in this post but next let's look at what's changed in utility classes.
The upgrade tool will find any of the class utilities that have been deprecated, renamed or changed in v4.
Here's a couple examples of classes that have been deprecated or use a different notation.
For a complete list of what's been deprecated click here.
Certain classes have been renamed to avoid confusion or to make things more explicit. Here's some examples.
For a complete list of what's been renamed click here.
In v4, some of the default utility class values have been changed to use currentColor as the default color over a standard default gray-200.
I found these changes required the most refactoring for me since components can use a lot of borders and the tool may or may not highlight these changes.
So just make sure to double check the UI still looks the same and set an intended border color before saving your changes.
Here's a couple of examples to show how it's changed in v4.
After running the update tool on a couple projects, I found this step to be the most critical, since it will most likely have an impact on the UI look and feel.
Now that the breaking changes have been addressed, we can move on to some of the new directives for defining themes and plugins.
Tailind v4 provides a new @theme directive that can be used to define theme details. Defining theme keys is based on CSS variables.
Here's some of the common things you can do with themes in v4.
In v3, you might define custom colors as follows:
1module.exports = {2 theme: {3 extend: {4 colors: {5 primary: 'oklch(0.8158 0.1597 76.85)',6 secondary: 'oklch(0.5318 0.0367 226.95)',7 // etc..8 },9 },10 },11};
To create a very simple theme, just use the @theme directive and use the available namespaces and Tailwind will create utilities for them.
Here's a few of the namespaces available but I'll add a link to the full list below.
For a complete list of theme namespaces look here.
Here's a basic example of using the theme directive.
1@import 'tailwindcss';23@theme {4 --color-primary: oklch(0.8158 0.1597 76.85);5 --color-primary-foreground: oklch(1 0 0);67 --color-secondary: oklch(0.5318 0.0367 226.95);8 --color-secondary-foreground: oklch(1 0 0);910 --font-sans: "Open Sans", sans-serif;11}
The base color palette has been improved to include a wider range of steps from 50 to 950 and also some new colors.
Any colors inside the @theme directive will extend the base color palette as usual.
To apply the theme is the same as it is in v3, just use the utility classes as usual.
1<p class="text-primary-foreground font-sans">Tailwind v4 theme demo</p>2<button class="bg-secondary text-secondary-foreground">Click Here!</button>
Here's a common example of how to toggle between light and dark mode themes but it can be used for any number of themes you might define in your project.
By defining the default theme CSS variables in the root, it can be switched using an attribute or class name selector.
Tailwind provides the @custom-variant directive to apply a custom selector theme variant.
1@import 'tailwindcss';23:root {4 --primary: oklch(0.8158 0.1597 76.85);5 --primary-foreground: oklch(1 0 0);67 --secondary: oklch(0.5318 0.0367 226.95);8 --secondary-foreground: oklch(1 0 0);9}1011[data-theme='dark'] {12 --primary: oklch(0.46 0 0);13 --primary-foreground: oklch(0.91 0 0);1415 --secondary: 217.2 32.6% 17.5%;16 --secondary-foreground: 210 40% 98%;17}1819@theme {20 --color-primary: var(--primary);21 --color-primary-foreground: var(--primary-foreground);2223 --color-secondary: var(--secondary);24 --color-secondary-foreground: var(--secondary-foreground);25}2627@custom-variant dark (&:where([data-theme=dark], [data-theme=dark] *));
To make the theme more dynamic it's best to take advantage of CSS variables to add flexibility.
In order to switch themes, you can set an attribute or a class selector dyanamically in your front-end HTML page to toggle the theme.
1<html>2 <body data-theme="dark">3</html>
Or, using a class selector example.
1<html>2 <body class="dark">3</html>
This technique can be used on the body tag for the whole page content or the variant can be applied to a single section as needed.
Now that we have the basic idea of how to work with themes, let's take a look at how to define and use plugins in v4.
Since most websites using Tailwind are probably using some plugins, I'll show how to carry those over during the upgrade.
Keep in mind, that the plugins can remain in the tailwind.config.js until you're ready to make the switch.
I'll configure the @tailwindcss/typography and @tailwindcss/forms as examples.
In v3, the plugins were installed as follows in the tailwind.config.js file. I'll add some plugin optional values to demonstrate how to use them in v4.
1module.exports = {2 theme: {3 extend: {4 plugins: [5 require('@tailwindcss/typography')({6 className: wysiwyg; // custom class prefix7 }),8 require("@tailwindcss/forms")({9 strategy: 'class', // only generate classes10 }),11 ]12 },13 },14};
In v4, a plugin can be installed as follows using the @plugin directive.
1@import 'tailwindcss';23@plugin "@tailwindcss/typography";4@plugin "@tailwindcss/forms";
Or, if you want to specify configuration options in the plugin, use the following notation.
1@import 'tailwindcss';23@plugin "@tailwindcss/typography" {4 className: wysiwyg;5}6@plugin "@tailwindcss/forms" {7 strategy: 'class';8}
I used the @tailwindcss/typography plugin because it's a good example of where you might still need to use the tailwind.config.js.
To fully customize the @tailwindcss/typography classes, you'll need to modify the default styles in the theme extension, for example.
1module.exports = {2 theme: {3 extend: {4 typography(theme) {5 return {6 DEFAULT: {7 css: {8 fontSize: 'var(--text-lg)',9 maxWidth: 'none',10 code: {11 fontSize: 'var(--text-base)',12 },13 blockquote: {14 fontSize: 'var(--text-base)',15 color: 'var(--text-blue-700)',16 background: 'var(--text-blue-200)',17 padding: '.5rem',18 p: {19 margin: '5px',20 },21 },22 },23 },24 };25 },26 },27 }28};
It's better to reference values using CSS variables to maximize flexibility.
Currently, I have found no other way customize the typography plugin styles using the v4 CSS-first configuration method.
So, without introducing any unneccesary CSS to customize a plugin's style output, it's more manageable to just keep using the tailwind.config.js for now.
Although this article is only covering the basic upgrade process, there's many benefits to using Tailwind v4.
Being able to define themes and configurations independently of a centralized tailwind.config.js file, has many advantages in v4.
tailwind.config.js files too.I hope this article has helped to shed some light on the upgrade process in some way. Tailwind is an awesome CSS alternative and worth staying up on the latest trends.