@apply No Longer Works in .less Files After Upgrading to Tailwind CSS v4
#20210
Replies: 5 comments
-
|
Consider providing a git repo that reproduces the issue and we can take a look for you. |
Beta Was this translation helpful? Give feedback.
-
|
Tailwind CSS v4 features a completely redesigned compiler engine built in Rust. Unlike v3, it compiles CSS natively and does not rely on PostCSS processing pipelines by default (unless configured via Preprocessors like Less compile CSS before Tailwind runs. Because Less uses the Here is how you can resolve or migrate this in v4: 1. Escape the
|
Beta Was this translation helpful? Give feedback.
-
|
this isn't really a Less problem, Less passes @apply straight through untouched (it treats it as an unknown at-rule and just emits it). i tested it: what actually changed in v4: @apply no longer has access to your theme/utilities unless the file being processed can see them. your main entrypoint has so the migration is mechanical, not a rewrite: add @reference "../app.css"; .btn { @reference imports your theme for resolution only, it doesn't duplicate any css into the output. if you're on the stock theme with no customizations you can even do i verified the whole chain on tailwindcss 4 + less: lessc -> @tailwindcss/postcss, and with @reference present the @apply resolves correctly (.btn comes out with font-weight: 700 and the red-500 color). without it you get exactly the unknown-utility error you're seeing. ordering matters in your build: Less has to run before the Tailwind PostCSS plugin (Tailwind needs to see the compiled css). most setups already do this but worth confirming. so for your large codebase it's basically: prepend one |
Beta Was this translation helpful? Give feedback.
This comment was marked as low quality.
This comment was marked as low quality.
-
|
This is expected behavior in Tailwind CSS v4. Tailwind v4 is designed as a standalone CSS build tool and no longer works with preprocessors like Less or Sass. Here is your migration path: Option 1: Convert .less files to .css (Recommended) Tailwind v4 includes most features that Less provides:
Option 2: Use PostCSS with Less features If you need Less-specific features, use PostCSS plugins: npm install postcss postcss-nesting postcss-custom-propertiesOption 3: Keep Less for non-Tailwind styles Separate your styles:
Option 4: Use Tailwind v3 if migration is too hard If you have a large codebase, you can stay on Tailwind v3: npm install tailwindcss@3Migration example: // Before (Less)
@primary: #3b82f6;
.mixin() {
padding: 1rem;
margin: 1rem;
}
.button {
color: @primary;
.mixin();
@apply text-white;
}/* After (CSS with Tailwind v4) */
:root {
--primary: #3b82f6;
}
.button {
color: var(--primary);
padding: 1rem;
margin: 1rem;
@apply text-white;
}Automated migration: You can use tools like |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Describe the bug
After upgrading from Tailwind CSS v3 to v4, all
@applydirectives inside.lessfiles are broken. The build fails and styles are not applied.Environment
To Reproduce
.lessfiles containing@apply@applynot being recognized in.lessfilesExpected behavior
@applyshould work in.lessfiles as it did in v3, or a clear migration path should be provided.Current behavior
Build fails with errors.
@applydirectives inside.lessfiles are not processed by Tailwind v4.Additional context
I have a large codebase with many
.lessfiles heavily using@apply. Migrating all of them is a significant effort.Question
What is the recommended migration path for projects using
@applyinside.lessfiles?Is there any workaround or compatibility layer planned for v4?
I understand that v4 is designed as a standalone CSS build tool and is not intended to be used alongside preprocessors like Less or Sass. However, a migration guide or an automated upgrade tool that handles
.lessfiles would be greatly appreciated.Beta Was this translation helpful? Give feedback.
All reactions