Skip to main content

Tailwind Merge Python

PyPI PyPI - Downloads

A Python utility for merging Tailwind CSS class lists intelligently, resolving conflicts based on Tailwind's principles. Inspired by the popular tailwind-merge JavaScript package.

This utility ensures that when you combine multiple strings of Tailwind classes (e.g., from different component states or logic branches), the resulting string is clean, minimal, and applies the intended styles by removing redundant or conflicting classes based on their function.

Installation

Using pip:

pip install tailwind-merge 

Usage

from tailwind_merge import TailwindMerge 

# Initialize 
twm = TailwindMerge()

result = twm.merge(
    "p-4 w-6 text-blue-500", # Initial classes
    "w-8 text-red-500"       # Overrides for width and text color
)
# "p-4 text-red-500 w-8" 

result = twm.merge("pl-4", "pr-6 pl-2") 
# "pl-2 pr-6"

result = twm.merge("p-2 hover:p-4", "p-3") 
# "hover:p-4 p-3"

result = twm.merge("hover:p-2", "focus:p-1 hover:p-4") 
# "hover:p-4 focus:p-1"

result = twm.merge("p-1", "p-[2px]")
# "p-[2px]"

result = twm.merge(
    "flex items-center justify-center", # Base layout
    "justify-between",                  # Override justify
    "text-red-500",                     # Add text color
    "hover:text-blue-500 text-lg"       # Add hover color and text size
)
# "flex items-center justify-between text-red-500 hover:text-blue-500 text-lg"

Custom Rules

twm.add_rule('custom-icon-size', ['icon-sm', 'icon-md', 'icon-lg'])
twm.merge("icon-sm icon-lg")
# "icon-lg"

Features

  • Conflict Resolution: Correctly identifies and resolves conflicting Tailwind classes based on their utility function, keeping the last applied class within a specific conflict group.
  • Modifier Support: Handles Tailwind modifiers (hover:, focus:, md:, dark:, etc.). Conflicts are resolved independently for base styles and each unique modifier combination (e.g., hover:text-red-500 conflicts with hover:text-green-500 but not with focus:p-4 or p-4).
  • Arbitrary Value Support: Recognizes and correctly groups classes with arbitrary values (e.g., p-[3px], w-[calc(100%-theme(spacing.4))], text-[#FF0000]).
  • Prefix Matching: Uses longest-prefix matching to correctly categorize classes when prefixes might overlap (e.g., correctly identifies border-t-2 as belonging to border-width-top before matching the shorter border- prefix).
  • Order Preservation: Aims to preserve the relative order of the final classes as they appeared in the input strings.
  • Custom classes: Allows adding custom conflict rules using the add_rule method for project-specific utilities or third-party libraries.
  • Zero Dependencies: Pure Python implementation with no external library requirements.

Contributing

Contributions are welcome! If you find a bug, have a feature request, or want to improve the class definitions, please feel free to open an issue or submit a Pull Request. Ensure tests pass and consider adding new tests for your changes.


Metadata

Release files for tailwind-merge 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tailwind-merge 0.3.3
File Size Uploaded
tailwind_merge-0.3.3.tar.gz 8.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tailwind-merge 0.3.3
File Interpreter ABI Platform
tailwind_merge-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 18.0 kB

Release files / tailwind_merge-0.3.3.tar.gz

Download URL tailwind_merge-0.3.3.tar.gz
Size 8.8 kB
Tags Source
SHA-256 checksum
How to use checksums
be19cde831cedafed24078f28f7014224398449fab436a651fd39e96a723a9c8
BLAKE2b-256 checksum
How to use checksums
ad58241aa4c1e4459170a4e0e938520181f3ddbcc8275c7b11f6d95b0b6baaca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.18 Linux/6.11.0-1018-azure

Release files / tailwind_merge-0.3.3-py3-none-any.whl

Download URL tailwind_merge-0.3.3-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e913e108040ae4b5bb906b90556c115a644f8ba6c0b78d8fcc86800125eb0ed
BLAKE2b-256 checksum
How to use checksums
cf560e580fb3459254cd02bd734029392743345d3c72306a0de8037f78091731
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.18 Linux/6.11.0-1018-azure

Release history Release notifications | RSS feed

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page