Scripts2026年5月25日·1 分钟阅读

tailwind-merge — Merge Tailwind Classes Without Style Conflicts

A utility function that intelligently merges Tailwind CSS classes, automatically resolving conflicts so the last class wins without producing broken styles.

Agent 就绪

复制一段可直接交给 Agent 执行的安装路径

这个资产可安装;Agent 先选择当前运行时、检查安装计划,再运行匹配命令。

Native · 98/100策略:允许
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
tailwind-merge Overview
直接安装命令
npx -y tokrepo@latest install e1deff28-5857-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run 确认安装计划,再运行此命令。

Introduction

tailwind-merge is a utility function that merges multiple Tailwind CSS class strings while intelligently resolving conflicts. When two classes target the same CSS property, the last one wins. This is essential for component libraries where parent components need to override child styles without leftover conflicting classes.

What tailwind-merge Does

  • Merges multiple class strings and removes conflicting Tailwind utilities
  • Understands Tailwind class groups so px-2 is correctly overridden by p-3
  • Handles arbitrary values, modifiers, and responsive prefixes
  • Supports Tailwind v3 and v4 class syntax out of the box
  • Provides configuration API for custom utility classes and plugins

Architecture Overview

tailwind-merge parses each class string into tokens, identifies the class group each token belongs to (e.g., padding, margin, background-color), and keeps only the last token for each group. A LRU cache stores recent merge results for performance. The class group definitions are generated from Tailwind CSS configuration, and custom groups can be registered for plugins or custom utilities.

Self-Hosting & Configuration

  • Install via npm: npm install tailwind-merge
  • Import and call: twMerge('class-a', 'class-b')
  • Configure for custom Tailwind plugins using extendTailwindMerge
  • Pair with clsx for conditional + conflict-free class merging
  • Cache size defaults to 500 entries; adjustable via config

Key Features

  • Correctly handles all Tailwind utility class conflicts including spacing, colors, and typography
  • Supports arbitrary values like bg-[#123456] and responsive/state modifiers
  • LRU cache ensures fast repeated merges with minimal memory usage
  • Extensible for custom Tailwind plugins and utility definitions
  • Tree-shakeable ESM module with full TypeScript support

Comparison with Similar Tools

  • clsx / classnames — concatenates classes but does not resolve conflicts; complementary, not a replacement
  • cn() helper (shadcn/ui) — combines clsx and tailwind-merge in a single utility function
  • twJoin — included in tailwind-merge for simple joining without conflict resolution
  • Tailwind CSS @apply — compile-time class application; does not help with runtime dynamic classes
  • cva (class-variance-authority) — manages component variants; often used alongside tailwind-merge

FAQ

Q: When do I need tailwind-merge vs. plain clsx? A: Use tailwind-merge when components accept className props that may conflict with internal styles. Use clsx alone when you only need conditional class construction without conflict resolution.

Q: Does it work with Tailwind v4? A: Yes. tailwind-merge supports Tailwind v4 syntax including the new color and spacing utilities.

Q: What is the cn() pattern from shadcn/ui? A: It combines clsx and twMerge: const cn = (...inputs) => twMerge(clsx(inputs)). This lets you conditionally build class strings and resolve Tailwind conflicts in one call.

Q: Can I use it with CSS Modules or other styling solutions? A: tailwind-merge is designed specifically for Tailwind CSS class strings. It will not provide value for non-Tailwind class names.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产