Back to blog

Auto Apply Suggested Tailwind Canonical Classes

3 min read

Cover image for Auto Apply Suggested Tailwind Canonical Classes
Image crafted by robots

If you’re using Tailwind CSS with VS Code, you’ve probably seen suggestions from the Tailwind CSS IntelliSense extension to convert arbitrary selectors into more canonical syntax. In VS Code, this appears as a “suggest canonical classes” hint when non-canonical Tailwind syntax is detected.

Tailwind IntelliSense suggesting canonical class syntax

The extension helpfully suggests converting your arbitrary selectors to more canonical syntax. For example:

className="[&>[role=checkbox]]:translate-y-[2px]"
className="*:[[role=checkbox]]:translate-y-0.5"

This is great for learning Tailwind’s modern syntax, but manually updating these across an entire codebase? That’s tedious work and I’m lazy.

What is the Tailwind Upgrade CLI?

Tailwind CSS has an official upgrade tool that automates this process:

Terminal window
pnpx @tailwindcss/upgrade

This command will:

It does other stuff too like upgrading between Tailwind versions, but we’re benefiting from the canonical class conversion here.

How does it work?

Here’s what it looks like in action. Before running the upgrade:

<div className="[&>[role=checkbox]]:translate-y-[2px]">
{/* content */}
</div>

After running pnpx @tailwindcss/upgrade:

<div className="*:[[role=checkbox]]:translate-y-0.5">
{/* content */}
</div>

As you can see some transformations were applied:

The CLI analyzes your entire project and applies these transformations automatically.

Why use canonical syntax?

Using canonical syntax isn’t just about following conventions:

When to run it

I recommend running the upgrade CLI:

Works with all major package managers

Terminal window
# pnpm
pnpx @tailwindcss/upgrade
# bun
bunx @tailwindcss/upgrade
# npm
npx @tailwindcss/upgrade
# yarn
yarn dlx @tailwindcss/upgrade

Bonus: Pair with Prettier or other formatters

For the ultimate Tailwind class management, combine this with the official Prettier plugin:

Terminal window
pnpm add -D prettier-plugin-tailwindcss

Or with Biome’s use sorted classes rule

biome.json
{
"linter": {
"rules": {
"nursery": {
"useSortedClasses": "error"
}
}
}
}

This gives you automatic class sorting (Prettier plugin or Biome) and canonical syntax updates (upgrade CLI).

Together, they keep your Tailwind code clean and modern with minimal effort.

Another Bonus: Let ESLint plugins take care of this too

eslint-plugin-better-tailwindcss’s upcoming version 4 will offer enforce-canonical-classes which can enforce canonical class suggestions as part of your linting process.

There are also other dedicated plugins that accomplish the same:

And if [Feature request] Canonical class name rule gets implemented, the popular eslint-plugin-tailwindcss could also handle this in the future.

Wrapping up

The @tailwindcss/upgrade CLI is one of those tools I wish I’d known about sooner. It turns a tedious manual process into a single command.

Next time VS Code suggests a canonical class format, remember: you don’t have to update them one by one. Let the upgrade CLI handle it for you.

Let's Discuss

Questions or feedback? Send me an email.

Last updated on

Back to blog