Sub topic 14
Editor support and class sorting
This chapter assumes you have a working Vite and React project with Tailwind CSS v4 installed, as set up in the earlier setup chapter. It also assumes you can edit a JSON config file and run npm scripts. Tailwind works without any of the tooling here. Class lists grow quickly, though, and two tools make them much easier to live with: the Tailwind CSS IntelliSense extension and the official Prettier plugin.
What the tooling does
Each tool covers a different gap:
| Tool | Runs when | What it gives you |
|---|---|---|
| Tailwind CSS IntelliSense (VS Code extension) | While you type | Autocomplete, hover previews, conflict warnings, color swatches |
| `prettier-plugin-tailwindcss` | On save, in CI, or from the command line | One consistent class order across the whole codebase |
| A CSS language association | While you edit CSS | No false errors on `@theme` and `@custom-variant` |
The extension helps you write classes. The plugin keeps them readable and makes diffs smaller. Neither one changes the CSS that Tailwind generates.
Setting up Tailwind CSS IntelliSense
Install the extension named Tailwind CSS IntelliSense from the VS Code marketplace. It is published by Tailwind Labs. Other editors with Language Server Protocol support can use the same language server, @tailwindcss/language-server.
In a v4 project the extension finds your setup by looking for a stylesheet that contains @import "tailwindcss";. It does not need a config file. If it finds no such stylesheet, it stays inactive, so check that your main CSS file has the import.
Once active, it provides:
- Autocomplete. Typing
bg-insideclassNamelists the color utilities. Your own@themevalues appear in the list, sobg-brand-500is suggested after you define--color-brand-500. - Hover previews. Hovering over a class shows the CSS it produces.
- Color decorators. A small swatch appears next to color classes.
- Linting. It warns about classes that set the same CSS property, such as
p-2 p-4, and about classes that do not exist.
Recommended editor settings
Create .vscode/settings.json in your project so every teammate gets the same behavior:
{
"files.associations": {
"*.css": "tailwindcss"
},
"editor.quickSuggestions": {
"strings": "on"
},
"tailwindCSS.classFunctions": ["cn", "clsx"],
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}Each setting does one job:
files.associationstreats.cssfiles as thetailwindcsslanguage. Without it, VS Code's built-in CSS checker reports "Unknown at rule" for@theme,@custom-variantand similar directives.editor.quickSuggestionswithstringsturned on lets autocomplete appear inside quoted strings. This matters because class names insidecn('...')are plain strings. VS Code turns this off by default.tailwindCSS.classFunctionstells the extension that the arguments ofcnandclsxare class lists. The extension already understandsclassName="...". It cannot know that your helpercntakes classes unless you say so.- The last two settings run Prettier whenever you save.
Where IntelliSense stops
The extension reads your source as text, as Tailwind itself does. It cannot autocomplete or validate a class built by string concatenation. The following line gets no help, and as the earlier chapter on class detection explained, it would not generate CSS either:
const bad = <p className={`text-${color}-600`}>Status</p>If autocomplete works in some strings and not others, check that the string sits inside a recognized place. That means a className attribute or a function listed in tailwindCSS.classFunctions.
Class sorting with the Prettier plugin
A hand-written class list tends to drift. One developer writes p-4 rounded-md bg-white, another writes bg-white rounded-md p-4, and a review turns into an argument about order. The plugin ends that by sorting every class list into the same order.
Install and configure
npm install --save-dev prettier prettier-plugin-tailwindcssCreate .prettierrc in the project root:
{
"plugins": ["prettier-plugin-tailwindcss"],
"tailwindStylesheet": "./src/index.css",
"tailwindFunctions": ["cn", "clsx"]
}Replace ./src/index.css with the path to the stylesheet that holds your @import "tailwindcss"; line.
pluginsloads the sorter. If you use other Prettier plugins, list the Tailwind plugin last. It wraps the behavior of the other plugins, and putting it earlier can break their formatting.tailwindStylesheetis required in v4. Tailwind v4 has notailwind.config.jsto point at. The plugin needs your stylesheet so it can load your@themevalues and custom variants. Without it, the plugin sorts using only the default theme. Your own classes, such asbg-brand-500, are then treated as unknown and moved to the front of the list.tailwindFunctionsis the Prettier counterpart of the editor setting above. Without it, onlyclassNamestrings are sorted, and the arguments tocn(...)stay in whatever order you typed them.
Add scripts to package.json so formatting is easy to run and easy to enforce:
{
"scripts": {
"format": "prettier --write src",
"format:check": "prettier --check src"
}
}Run npm run format once to sort the whole codebase. Run npm run format:check in CI to fail a build when a file is not sorted.
What sorting looks like
Take a button with its classes in an arbitrary order:
export function SaveButton() {
return (
<button className="text-white hover:bg-blue-700 px-4 bg-blue-600 py-2 rounded-md disabled:opacity-50">
Save
</button>
)
}After Prettier runs, the same classes appear like this:
export function SaveButton() {
return (
<button className="rounded-md bg-blue-600 px-4 py-2 text-white hover:bg-blue-700 disabled:opacity-50">
Save
</button>
)
}The set of classes is identical, so the rendered page is the same. Only the order changed.
How the order is chosen
The plugin does not sort alphabetically. It uses the order in which Tailwind emits the CSS for those classes, which is the order that controls the cascade. In practice you will see these rules:
- Classes with no Tailwind meaning, such as your own
my-componenthook, go first and keep their relative order. - Plain utilities follow, grouped roughly by what they affect: layout, then sizing and spacing, then borders and backgrounds, then typography.
- Variants such as
hover:,focus-visible:,sm:anddark:come after plain utilities. Within a variant group, the order follows the variant's position in the generated CSS.
This order is deliberate. When two classes target the same property, the one later in the generated stylesheet wins, regardless of its position in your className. Sorting by emitted order means that what you read in the file is close to what the browser applies.
Sorting inside helper functions
With tailwindFunctions set, calls to cn and clsx are sorted string by string. Each argument is sorted on its own, and the arguments are not merged:
import { cn } from './cn'
export function Panel({ active }: { active: boolean }) {
return (
<div
className={cn(
'rounded-lg border p-4 shadow-sm',
active && 'border-blue-600 bg-blue-50'
)}
/>
)
}Each string is sorted internally. The first one, the base styles, stays before the conditional one. This is the behavior you want, because argument order matters to tailwind-merge, where later arguments win conflicts. The plugin never moves a class from one argument to another.
Limits and common problems
- Concatenated strings are not sorted. The plugin works on complete string literals and on template literals in recognized places. A class assembled from fragments is left alone, and it is also invisible to Tailwind's detection.
- Custom attributes need to be declared. To sort classes in a prop such as
containerClassName, add"tailwindAttributes": ["containerClassName"]to.prettierrc. - Duplicates are removed. If
p-4appears twice in one string, the plugin keeps one copy. If you need to keep duplicates, there is atailwindPreserveDuplicatesoption. - Nothing sorts after a wrong stylesheet path. If
tailwindStylesheetpoints to a file that does not exist or does not import Tailwind, check Prettier's output for an error or a warning. A wrong path can leave you with an order that ignores your theme. - The plugin and the editor setting must match. If you add a helper such as
cvafor variants, add it to bothtailwindCSS.classFunctionsandtailwindFunctions. - Monorepos need explicit entry points. In a repository with several apps, the extension may need
tailwindCSS.experimental.configFileto map each stylesheet to the files it applies to. The same applies totailwindStylesheet, which is set per Prettier config, so place a.prettierrcin each package.
Other tools worth knowing
Prettier is the most common choice, but it is not the only one. Biome has a class-sorting lint rule, useSortedClasses, which you can use if your project formats with Biome instead of Prettier. Check its documentation for the current status, because the rule's name and stability have changed between versions. Use one sorter per project. Two tools that order classes differently will keep rewriting each other's output.
Exercise
Your team added prettier-plugin-tailwindcss to a v4 project. After formatting, classes such as bg-brand-500, which you defined in @theme, always move to the very front of the class list, while built-in classes are sorted normally. What is the likely cause, and what is the fix?
Show answer
The plugin cannot see your theme, so it treats bg-brand-500 as an unknown class, and unknown classes are placed first. Tailwind v4 has no tailwind.config.js for the plugin to read. Set "tailwindStylesheet" in .prettierrc to the path of the stylesheet that contains @import "tailwindcss"; and your @theme block, for example "./src/index.css", then run Prettier again.
Exercise
You wrote a helper cn and call it as cn('p-4 flex', isOpen && 'bg-white'). Autocomplete does not work inside those strings, and Prettier does not sort them. Which two settings fix this?
Show answer
Add "cn" to tailwindCSS.classFunctions in the VS Code settings so IntelliSense treats the arguments as class lists. Add "cn" to tailwindFunctions in .prettierrc so the Prettier plugin sorts them. Also confirm that editor.quickSuggestions has strings set to on, because autocomplete in quoted strings is off by default.