Canonicalize Tailwind
Use when the user wants to clean up, canonicalize, or normalize Tailwind class lists.
Workflow
- Identify Tailwind class strings in the requested files or components.
- Canonicalize them with
npx @tailwindcss/cli canonicalize. - Apply the changed strings back to the source.
- Run the project's formatter or relevant checks when available.
Commands
npx @tailwindcss/cli canonicalizecollapses shorthands (mt-2 mr-2 mb-2 ml-2→m-2), resolves overrides (py-3 p-1 px-3→p-3), canonicalizes arbitrary values to named utilities, and sorts classes; pass--css path/to/input.cssif the project uses a custom CSS entry file.Single class string:
npx @tailwindcss/cli canonicalize "mt-2 mr-2 mb-2 ml-2" # m-2Multiple strings as positional args (each on its own line):
npx @tailwindcss/cli canonicalize "py-3 p-1 px-3" "mt-2 mr-2 mb-2 ml-2" # p-3 # m-2Pipe strings via stdin (one per line):
echo "py-3 p-1 px-3\nmt-2 mr-2 mb-2 ml-2" | npx @tailwindcss/cli canonicalize # p-3 # m-2--format jsonor--format jsonlgives structured output withinput/output/changedfields:npx @tailwindcss/cli canonicalize --format json "py-3 p-1 px-3" # [{ "input": "py-3 p-1 px-3", "output": "p-3", "changed": true }]--streamprocesses stdin line-by-line without buffering:npx @tailwindcss/cli canonicalize --stream
Verify
- Confirm classes still express the same visual intent after canonicalization.