iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐡
Tailwind CSS: Automatically Sort Class Names on Save in VSCode [Next.js]
Introduction
Next.js + Tailwind CSS is very convenient, isn't it?
However, when using Tailwind CSS, because you use many classes, a problem arises where the order of class names becomes inconsistent.
In this article, I will show you how to use ESLint to enforce an error when the order is not aligned, and how to automatically fix them on save in VSCode.
Prerequisites
- A project created with the following command from Next.js, with Tailwind CSS integrated.
$ npx create-next-app@latest
npm Package to Install
How to do it
Install eslint-plugin-tailwindcss
Installation
$ npm i -D eslint-plugin-tailwindcss
Modify the ESLint configuration file
.eslintrc.json
{
- "extends": "next/core-web-vitals",
+ "extends": "next/core-web-vitals",
+ "rules": { "tailwindcss/classnames-order": "error" },
+ "plugins": ["tailwindcss"]
}
Now VSCode will start nagging you.
It will start complaining about something as trivial as the order of class names 😢

Since it's annoying to just be scolded
Make VSCode automatically reorder Tailwind CSS class names when saving the code.
.vscode/settings.json
+{
+ "editor.codeActionsOnSave": {
+ "source.fixAll.eslint": "explicit"
+ }
+}
Result
What a surprise!!
The class names are now automatically reordered!!! 🥳

Discussion