I completed a Vue.js course last December and the project used TailwindCSS v3. Not even 3 months after, the team released TailwindCSS v4 and a lot of parts required adaptation.
I was new to TailwindCSS upgrades and I tried the official upgrade guide. It didn’t work because the tailwind CSS v3 setup was too custom to be upgraded automatically.
I tried to do it manually, but I couldn’t understand why the utility classes wouldn’t work.
Here is how I eventually achieve the upgrade.
My Initial State
I needed to migrate the project from TailwindCSS v3 to v4.
I couldn’t upgrade through the upgrade tool because of a custom tailwind.config.js
file.
The project also used PostCSS processing:
|
|
The First Steps
I installed @tailwindcss/vite
and the latest tailwindcss
packages as development dependencies.
Following that, I updated the vite configuration to use the @tailwindcss/vite
plugin in the Vite’s plugins
section and remove the css
section altogether.
The rest of the updates occurred either in the main CSS file or the components’ styles.
So, I updated the index.css
file with ’@import “tailwindcss;instead of
@tailwind’ directives, as the manual upgrade suggests.
That meant that I had to replace @layer base
to @layer utilities
.
Also, I checked all the renamed utilities and removed utilities from the list that the TailwindCSS team provided in their upgrade guide.
So far so good, but the most difficult part I had to understand follows next.
What About tailwind.config.js
In version 4, the file becomes less used. It depends on your level of customization.
I won’t say “unused completely” because we still need it to tell where we want the Intellisense activated to add utility classes to the HTML.
What I had left in this file is listed below:
|
|
But how do you convert the theme
object that we had in version 3?
Let’s start with the container
object. If you have:
|
|
It becomes this CSS code that you add to your main CSS file:
|
|
If you use extend
for colors, border radius and so on like this:
|
|
You’ll need to use the @theme
directive:
|
|
What about keyframes
? Very similar method. From this:
|
|
You add the following with the @keyframes
directive:
|
|
Replacing `@apply’ in the Components
Now, we come to the last part.
In a template, I encountered this kind of error when I ran the build command. For example:
|
|
The styles in the component causing the error was this one:
|
|
text-slate-500
is a commonly used utility class predefined in TailwindCSS.
My project used a lot “@apply”… I think it has been heavily used by projects with TailwindCSS. It is useful to avoid repeating the same series of utility classes on several HTML elements. But how did I solve this issue?
You have three options:
-
You simply copy your custom utility classes from your components into the
@layer components’ of your main CSS file. But that defeats the purpose and it won’t work if you use
:deep` option from Vue’s nested components in your styles. -
You use
@utility
directive to define a new utility class in your main CSS file. -
You import your main stylesheet for reference in your components. You need to do that in all the files where you use
@apply
to create a custom CSS class that applies tailwind utility classes. Any tailwind utility class used in the templates of your components doesn’t require adjustments.1 2 3 4 5 6 7
/* Import your main CSS in the component */ @reference "@/assets/index.css"; /* Use tailwind utility classes as before */ .pencil { @apply text-slate-500 cursor-pointer; }
I chose the third option where I used :deep
CSS selectors from Vue and for the rest, I went for the first option.
BE CAREFUL about using the third option everywhere
I’m not sure it’d be wise to use it everywhere. Read more in the documentation about the TailwindCSS directives about this topic.
Conclusion
I’m still getting the hang of TailwindCSS. In a way, I find it interesting. You can quickly transform a layout using flexbox or grid layout system.
Like many out there, I’m wondering about the blotted HTML with those utility classes. Also, what is the logic to add the classes? Is it similar to the CSS files where you put the most generic first, then the typography and, finally, the layout?
Version 4 uses the @layer
feature of vanilla CSS. And from a quick look at this, the cascading applies in a similar way to the usual styles. You can, however, explicitly change the order of cascading:
|
|
More on the topic soon…
Follow me
Thanks for reading this article. Make sure to follow me on X, subscribe to my Substack publication and bookmark my blog to read more in the future.
Photo by Rahul Pandit.