Tailwind class sorter
Sort Tailwind classes in the same order as the Prettier plugin, remove duplicates, and drop classes that a later one overrides.
Long Tailwind class lists collect duplicates and classes that cancel each other out. Paste a class list, or a whole HTML or JSX file, and get it back sorted and cleaned up. Below the output, every class is shown by category, and anything removed is crossed out with the reason.
Input
Output
<button className="flex items-center gap-2 rounded-lg bg-sky-700 px-6 py-2 text-sm font-semibold text-white shadow hover:bg-sky-900 sm:px-8 sm:py-3">
Save changes
</button>
<div class="mx-auto mt-4 w-full max-w-md rounded-xl border border-slate-200 bg-white p-4 dark:bg-slate-900 md:p-8">
<h2 className={cn("text-xl font-semibold tracking-tight text-slate-900 dark:text-white", active && "underline")}>Billing</h2>
</div>What changed
flexremoved: duplicatepx-4removed: overridden bypx-6hover:bg-sky-800removed: overridden byhover:bg-sky-900
px-4removed: overridden byp-4p-6removed: overridden byp-4
text-lgremoved: overridden bytext-xl
Sort order
The order follows prettier-plugin-tailwindcss:
- Classes Tailwind doesn't know, like your own
cardclass, go first. - Then base utilities, in the order Tailwind writes their CSS, which runs roughly from layout and positioning, through sizing, borders, colours and spacing, to typography, effects and transitions.
- Then classes with variants. States like
hover:come beforedark:, which comes before breakpoints fromsm:to2xl:.
For example, this list:
text-white px-4 sm:px-8 py-2 sm:py-3 bg-sky-700 hover:bg-sky-800
comes out exactly as the plugin sorts it:
bg-sky-700 px-4 py-2 text-white hover:bg-sky-800 sm:px-8 sm:py-3
The plugin reads your Tailwind config, so custom utilities may land in a slightly different place there.
Conflicts
When two classes set the same thing under the same variants, the later one wins, as with tailwind-merge:
- A broader class removes earlier narrower ones:
px-2 p-4keeps onlyp-4, butp-4 px-2keeps both, becausepx-2still changes something. w-4 h-4 size-6keeps onlysize-6.text-,bg-,border-,shadow-andring-classes are told apart by their value, sotext-sm text-red-500sets two different things and isn't a conflict. Custom names likebg-primaryare treated as colours.- Variants in a different order count as the same:
md:hover:andhover:md:. A class marked important with!doesn't clash with the same class unmarked.
Markup mode
Paste a whole file and only the class lists change: class="…", className="…", className={'…'}, and the first string inside cn(), clsx(), cva() and twMerge(). Template strings containing ${} are skipped, and the rest of the file is left exactly as written.