Sub topic 14
Editor support and class sorting
This chapter assumes you have a Vite and React project with Tailwind CSS v4 installed, as in the setup chapter. You should also be comfortable with npm and with your editor's settings. Tailwind class lists get long. Good tooling makes them easier to write, read, and review.
What tooling gives you
Three tools cover most needs:
- The Tailwind CSS IntelliSense extension for VS Code adds autocomplete, hover previews, and linting for class names.
prettier-plugin-tailwindcsssorts classes into a consistent order every time you format.clsxandtailwind-mergehelpers need extra configuration so the tools above also understand classes inside function calls.
Install IntelliSense
In VS Code, install the extension named "Tailwind CSS IntelliSense". It reads your project's Tailwind setup, so it knows about custom theme values you define with @theme. For example, after you add --color-brand-500, typing bg-br suggests bg-brand-500.
The extension provides:
- Autocomplete for utilities and variants.
- A hover card that shows the CSS a class produces.
- Color swatches next to color utilities.
- Warnings for conflicting classes, such as
block flexon the same element.
If suggestions do not appear, check that your main stylesheet containing @import "tailwindcss"; is inside the opened workspace folder. The extension finds your Tailwind setup from that file.
Make IntelliSense work inside helper functions
By default the extension looks at className attributes. Class names passed to cn() or stored in a variants object are plain strings, so it may not suggest anything there. Tell it which functions to watch in your VS Code settings.json:
{
"tailwindCSS.classFunctions": ["cn", "clsx"],
"tailwindCSS.classAttributes": ["class", "className"]
}With this setting, strings inside cn('rounded-md px-4', ...) get autocomplete and linting. Add your own function names to the list if you wrap class logic elsewhere.
Sort classes with Prettier
Class order does not change the CSS result. Tailwind's own stylesheet order decides which rule wins, not the order in your className. Still, random order makes diffs noisy and reviews slower. The official Prettier plugin sorts classes in the same order Tailwind uses internally: layout first, then spacing, typography, colors, and variants last.
Install it as a dev dependency:
npm install -D prettier prettier-plugin-tailwindcssThen register it in a Prettier config file named .prettierrc:
{
"plugins": ["prettier-plugin-tailwindcss"],
"tailwindStylesheet": "./src/index.css",
"tailwindFunctions": ["cn", "clsx"]
}The tailwindStylesheet option points to the file with @import "tailwindcss";. Tailwind v4 has no tailwind.config.js by default, so the plugin needs this path to load your theme. Adjust it to match your project. The tailwindFunctions option makes the plugin sort strings inside cn() and clsx() as well as className attributes.
Before formatting:
<div className="p-6 text-white bg-blue-600 rounded-lg flex items-center">After running Prettier:
<div className="flex items-center rounded-lg bg-blue-600 p-6 text-white">Format on save
Add this to your VS Code settings so sorting happens automatically:
{
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode"
}This needs the Prettier VS Code extension. You can also add a script to package.json, such as "format": "prettier --write src", and run it in CI with prettier --check src to catch unsorted classes.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| No autocomplete at all | Stylesheet is outside the workspace, or the extension is disabled | Open the project root folder and reload the window |
| No autocomplete inside `cn()` | The function is not listed | Add it to `tailwindCSS.classFunctions` |
| Classes inside `cn()` are not sorted | The function is not listed for Prettier | Add it to `tailwindFunctions` |
| Custom theme classes sort first or last | Plugin cannot find your theme | Set `tailwindStylesheet` |
Exercise
Your team uses a cn() helper. Autocomplete works in className="..." but not in cn('px-4 py-2', className), and Prettier leaves the strings inside cn() unsorted. Which two settings do you change?
Show answer
Add "cn" to tailwindCSS.classFunctions in VS Code settings.json so IntelliSense watches the function. Add "cn" to tailwindFunctions in the Prettier config so the plugin sorts strings inside it.