What this thread does
The problem
A Button has px-4, the caller passes px-8, and the old one still wins because Tailwind's generated CSS puts one utility after the other regardless of how they are ordered in the class string. Agents respond by adding !important or inline styles.
What tailwind-merge does
The library (README example: twMerge('px-2 py-1 bg-red hover:bg-dark-red', 'p-3 bg-[#B91C1C]') returns 'hover:bg-dark-red p-3 bg-[#B91C1C]') drops earlier classes that a later class replaces, and keeps non-conflicting ones such as the hover variant. Its README says the current version supports Tailwind v4.0 to v4.3, and v2.6.0 is the one for Tailwind v3. The repo's issue tracker shows configuration limits for custom text- classes, for example issue 368, closed as resolved on March 9, 2024, so teach it your custom classes via configuration.
The helper in existing projects
Existing shadcn/ui projects may define cn(...inputs) as twMerge(clsx(inputs)); since September 2026, new components import cn from the cn package, while existing helpers keep working. The agent should always build the final className as cn("base classes", variantClasses, className) with the caller's className last, so that caller overrides win.
What to tell the agent
"Use cn() for every component's className. Put the caller's className last. Do not use !important or inline style to override Tailwind classes. If a custom class is merged wrongly, extend twMerge's config." (Mine, untried.) Variants with many options pair well with cva; the merge step stays the same.
Common mistakes
Plain template strings instead of cn, caller classes placed before the base ones, using the Tailwind v3 build of tailwind-merge with v4 (or the reverse), and !important on utilities.
Limits
I did not run these prompts or reproduce the bugs; the CSS behaviour is from the cited docs.
Sources
Primary sources, opened 18 October 2026: Tailwind styling with utility classes and the tailwind-merge repo (README, metadata and issue 368 read through the GitHub API).
- Confirms: when two classes set the same property, the one later in the generated stylesheet wins, not the one later in the
classattribute; the README example and result fortwMerge; support for Tailwind v4.0 to v4.3 and v2.6.0 for Tailwind v3; MIT licence; issue 368, "Wrong merge of classes with text-prefix", is closed. - The linked issue was closed as resolved on March 9, 2024. Tailwind's own page does not mention tailwind-merge; it recommends not putting conflicting classes on one element, exposing props from components, and offers the
!modifier as a last resort, so the thread's advice against!importantis a preference, not Tailwind's rule. - Checked on November 15, 2026: shadcn/ui’s cn change documents the new package import and existing helpers; the cva installation guide shows wrapping cva output with
twMerge.
Started from a post by @dcastil/tailwind-merge README and issues, packaged by the editor. Original post