How to Automatically sort Tailwind CSS Classes in Any Code Editor or IDE
Tailwind CSS has gained immense popularity among developers for its utility-first approach to styling, offering a vast array of pre-built utility classes. However, maintaining consistency and formatting in your Tailwind CSS classes across projects can be challenging. Fortunately, you can streamline this process by automatically formatting your Tailwind CSS classes on save in any code editor or IDE. In this guide, we'll walk you through the steps to achieve this seamlessly.
Step 1: Install Prettier & Tailwind CSS Plugin
The first step is to install Prettier, a widely-used code formatter, along with the Tailwind CSS plugin. You can do this using your preferred package manager. For example, if you're using npm, run:
npm install --save-dev prettier prettier-plugin-tailwindcss
Or if you prefer yarn:
yarn add --dev prettier prettier-plugin-tailwindcss
Step 2: Configure Prettier
Next, you need to configure Prettier to use the Tailwind CSS plugin. This can be done by adding the configuration directly to your package.json
file. Here's an example configuration:
{
"plugins": [
"prettier-plugin-tailwindcss"
]
}
Feel free to customize these settings according to your preferences.
Step 3: Enable Save Actions
Now, you need to configure your code editor or IDE to run Prettier automatically on save. Most modern editors provide extensions or built-in settings to achieve this. Here's how you can do it in a few popular editors:
Visual Studio Code (VS Code)
Install the "Prettier - Code formatter" extension.
Open VS Code settings (
Ctrl + ,
).Search for
Default Formatter
and select the Prettier-Code formatter option.Search for "Format On Save" and ensure it's enabled.
Sublime Text
Install the "JsPrettier" package using Package Control.
Open Sublime Text preferences (
Ctrl + ,
).Add
"format_on_save": true
to your user settings.
Atom
Install the "prettier-atom" package.
Open Atom settings (
Ctrl + ,
).Check the "Format files on save" option under the "prettier-atom" settings.
JetBrains IDEs (e.g., WebStorm, PhpStorm)
Install the "Prettier" plugin from the JetBrains Marketplace.
Go to
Settings/Preferences -> Languages & Frameworks -> JavaScript -> Prettier
.Check the Automatic Prettier configuration if unchecked.
Check the "On code reformat" and "On save" options.
Step 4: Enjoy Automated Formatting
That's it! With Prettier configured and save actions enabled in your code editor or IDE, your Tailwind CSS classes will be automatically formatted every time you save a file. This ensures consistent formatting and improves the readability of your codebase, making development a smoother experience.
By following these steps, you can seamlessly integrate automated formatting for Tailwind CSS classes into your preferred code editor or IDE, enhancing your development workflow and productivity. Happy coding! ๐
Subscribe to my newsletter
Read articles from RANIT MANIK directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
RANIT MANIK
RANIT MANIK
Frontend Developer | Designer | Power User | Computer Science Engineer