Tailwind Merge Python
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-500conflicts withhover:text-green-500but not withfocus:p-4orp-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-2as belonging toborder-width-topbefore matching the shorterborder-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_rulemethod 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)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| tailwind_merge-0.3.3.tar.gz | 8.8 kB | Without provenance | Details for tailwind_merge-0.3.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| tailwind_merge-0.3.3-py3-none-any.whl | Python 3 | none | any | Details for tailwind_merge-0.3.3-py3-none-any.whl |
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
|
Security for tailwind-merge 0.3.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.