A Prettier plugin that automatically normalizes Tailwind CSS arbitrary values into standard utility classes, helping maintain consistent and optimized CSS across your project.
Requires Prettier v3.0.0 or higher (Prettier v2 is not supported)
- Converts arbitrary values like
p-[4px]to standard classes likep-1 - Works with HTML, React/JSX, Vue.js, and Angular
- Only transforms class attributes, leaving comments, text, and other attributes untouched
- Supports padding, margin, sizing, typography, borders, shadows, and more
- Supports all Tailwind CSS variants (responsive, state, dark mode, group, peer, etc.)
npm install --save-dev prettier@^3.0.0
npm install --save-dev @youthfulhps/prettier-plugin-tailwindcss-normalizerAdd the plugin to your Prettier configuration:
.prettierrc
{
"plugins": ["@youthfulhps/prettier-plugin-tailwindcss-normalizer"]
}prettier.config.js
module.exports = {
plugins: ["@youthfulhps/prettier-plugin-tailwindcss-normalizer"],
};# Format all files
npx prettier --write .
# Format specific files
npx prettier --write src/**/*.{tsx,jsx,html,vue}To use this plugin alongside other prettier tailwind plugins (like prettier-plugin-tailwindcss), install and configure prettier-plugin-merge:
npm install --save-dev prettier-plugin-merge.prettierrc.js
module.exports = {
plugins: [
"@youthfulhps/prettier-plugin-tailwindcss-normalizer",
"prettier-plugin-tailwindcss",
"prettier-plugin-merge", // Must be last
],
};Before:
<div className="p-[16px] m-[8px] bg-blue-500">
<span className="px-[24px] py-[12px] rounded-[6px]">Button</span>
</div>After:
<div className="p-4 m-2 bg-blue-500">
<span className="px-6 py-3 rounded-md">Button</span>
</div>With Variants:
// Before
<div className="md:p-[16px] hover:m-[8px] dark:bg-[#1f2937]">
Content
</div>
// After
<div className="md:p-4 hover:m-2 dark:bg-[#1f2937]">
Content
</div>- Padding:
p-[4px]→p-1,px-[16px]→px-4 - Margin:
m-[8px]→m-2,my-[12px]→my-3 - Gap:
gap-[8px]→gap-2,gap-x-[16px]→gap-x-4
- Width:
w-[100px]→w-25,w-[200px]→w-50 - Height:
h-[50px]→h-12.5,h-[100px]→h-25
- Font Size:
text-[14px]→text-sm,text-[18px]→text-lg - Letter Spacing:
tracking-[-0.05em]→tracking-tighter
- Border Radius:
rounded-[4px]→rounded,rounded-[6px]→rounded-md - Border Width:
border-[1px]→border,border-[2px]→border-2
- Box Shadow:
shadow-[0_1px_3px_rgba(0,0,0,0.1)]→shadow-sm - Opacity:
opacity-[0.5]→opacity-50
- Rotate:
rotate-[-180deg]→-rotate-180 - Translate:
translate-x-[-100%]→-translate-x-full
By default, Tailwind uses 4px as the base spacing unit (e.g., p-1 = 4px, p-2 = 8px). If you've customized your Tailwind spacing scale, configure the customSpacingUnit option:
.prettierrc.js
module.exports = {
plugins: ["@youthfulhps/prettier-plugin-tailwindcss-normalizer"],
customSpacingUnit: 8, // Change to match your Tailwind spacing scale
};Example with customSpacingUnit: 8:
// Before
<div className="p-[8px] m-[16px] gap-[24px]">Content</div>
// After
<div className="p-1 m-2 gap-3">Content</div>See the examples/custom-spacing directory for a complete working example.
The plugin supports all Tailwind CSS variants:
- Responsive:
sm:,md:,lg:,xl:,2xl: - State:
hover:,focus:,active:,disabled:, etc. - Dark Mode:
dark: - Group/Peer:
group-hover:,peer-checked:, etc. - Pseudo-elements:
before:,after:,placeholder:, etc. - ARIA:
aria-checked:,aria-disabled:, etc. - Data Attributes:
data-[status=active]: - Arbitrary:
[&:nth-child(3)]:,has-[input:focus]:, etc.
The plugin only transforms class-related attributes:
Transformed:
classNameattributes in JSX/TSXclassattributes in HTML:classandv-bind:classin Vue[class]in Angular- String literals in template literals
- Function calls like
clsx(),classnames(),cn()
Untouched:
- Comments and documentation
- Regular text content
- Other HTML attributes
- JavaScript strings and variables
- CSS in
<style>tags
| Format | Extension |
|---|---|
| HTML | .html |
| React JSX | .jsx |
| React TSX | .tsx |
| Vue | .vue |
| Angular | .component.html |
npm testContributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues or have questions:
- Check the Issues page
- Create a new issue with detailed information
- Provide examples of your code and expected behavior
Q: The plugin doesn't work with Prettier v2
A: This plugin requires Prettier v3.0.0 or higher. Please upgrade your Prettier version.
Q: How do I check my Prettier version?
A: Run npx prettier --version to check your current Prettier version.