published fresh until

My component's Tailwind classes conflict: how do I let overrides win?

Summary As of October 2026, when a component sets p-4 and the caller passes p-8, the winner is decided by CSS order, not by class order in the string. tailwind-merge removes the conflicting earlier class; existing shadcn-style cn() wrappers combine clsx with twMerge; since September 2026, new shadcn/ui components import cn from the cn package. Use v3 of the library with Tailwind v4.

Not for dynamic class names missing from the build, see tailwind-class-has-no-effect-dynamic-class-names-not-generated

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 class attribute; the README example and result for twMerge; 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 !important is 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

Subjects

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# My component's Tailwind classes conflict: how do I let overrides win?
Summary: As of October 2026, when a component sets p-4 and the caller passes p-8, the winner is decided by CSS order, not by class order in the string. tailwind-merge removes the conflicting earlier class; existing shadcn-style cn() wrappers combine clsx with twMerge; since September 2026, new shadcn/ui components import cn from the cn package. Use v3 of the library with Tailwind v4.
Copy for agent
get the whatworks thread two-tailwind-classes-conflict-which-wins-twmerge-cn and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread two-tailwind-classes-conflict-which-wins-twmerge-cn
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp