Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

tailwind-merge-laravel

> 编程语言
Open source

Automatically resolves Tailwind CSS class conflicts in Laravel

342 stars0 likes1 views
WebsiteGitHub

About

Automatically resolves Tailwind CSS class conflicts in Laravel


TailwindMerge for Laravel allows you to merge multiple Tailwind CSS classes and automatically resolves conflicts between classes by removing classes conflicting with a class defined later. This is especially helpful when you want to override Tailwind CSS classes in your Blade components.

A Laravel / PHP port of tailwind-merge by dcastil.

Supports Tailwind v3.0 up to v3.3.

If you find this package helpful, please consider sponsoring the maintainer:

  • Sandro Gehri: github.com/sponsors/gehrisandro

If you are NOT using Laravel, you can use the TailwindMerge for PHP directly.

Table of Contents

  • Get Started
  • Usage
    • Laravel Blade Components
    • Laravel Blade Directive
    • Everywhere else in Laravel
  • Configuration
    • Custom Tailwind Config
  • Contributing

Get Started

Requires Laravel 10

First, install TailwindMerge for Laravel via the Composer package manager:

bash
composer require gehrisandro/tailwind-merge-laravel

Optionally, publish the configuration file:

bash
php artisan vendor:publish --provider="TailwindMerge\Laravel\TailwindMergeServiceProvider"

This will create a config/tailwind-merge.php configuration file in your project, which you can modify to your needs using environment variables. For more information, see the Configuration section:

env
TAILWIND_MERGE_PREFIX=tw-

Finally, you may use TailwindMerge in various places like your Blade components:

php
// circle.blade.php

// your-view.blade.php

// output

TailwindMerge is not only capable of resolving conflicts between basic Tailwind CSS classes, but also handles more complex scenarios:

…

It's possible to pass the classes as a string, an array or a combination of both:

php
TailwindMerge::merge('h-10 h-20'); // h-20
TailwindMerge::merge(['h-10', 'h-20']); // h-20
TailwindMerge::merge(['h-10', 'h-20'], 'h-30'); // h-30
TailwindMerge::merge(['h-10', 'h-20'], 'h-30', ['h-40']); // h-40

Usage

For in depth documentation and general PHP examples, take a look at the gehrisandro/tailwind-merge-php repository.

Use in Laravel Blade Components

Create your Blade components as you normally would, but instead of specifying the class attribute directly, use the mergeClasses method:

php
// circle.blade.php

Now you can use your Blade components and pass additional classes to merge:

php
// your-view.blade.php

This will render the following HTML:

xml

Note: Usage of $attributes->merge(['class' => '...']) is currently not supported due to limitations in Laravel.

Merge classes on multiple elements

By default Laravel allows you to only merge classes in one place. But with TailwindMerge you can merge classes on multiple elements by using twMergeFor():

blade
// button.blade.php
withoutTwMergeClasses()->twMerge('p-2 bg-gray-900 text-white') }}>
    twMergeFor('icon', 'h-4 text-gray-500') }} viewBox="0 0 448 512">
    
    {{ $slot }}

You can now specify additional classes for the button and the svg icon:

blade
// your-view.blade.php

  Click Me

This will render the following HTML:

xml

  

  Click Me

Note: Use withoutTwMergeClasses() on your main attributes bag, otherwise all class:xyz attributes will be rendered in the output.

Use Laravel Blade Directive

The package registers a Blade directive which can be used to merge classes in your Blade views:

php
@twMerge('w-10 h-10 rounded-full bg-red-500 bg-blue-500') // w-10 h-10 rounded-full bg-blue-500

// or multiple arguments
@twMerge('w-10 h-10 rounded-full bg-red-500', 'bg-blue-500') // w-10 h-10 rounded-full bg-blue-500

If you want to rename the blade directive, you can do so in the config/tailwind-merge.php configuration file:

php
// config/tailwind-merge.php
return [
    'blade_directive' => 'customTwMerge',
];

You could even disable the directive completely by setting it to null:

php
// config/tailwind-merge.php
return [
    'blade_directive' => null,
];

Everywhere else in Laravel

If you don't use Laravel Blade, you can still use TailwindMerge by using the Facade or the helper method directly:

Facade

php
use TailwindMerge\Laravel\Facades\TailwindMerge;

TailwindMerge::merge('w-10 h-10 rounded-full bg-red-500 bg-blue-500'); // w-10 h-10 rounded-full bg-blue-500

Helper Method

php
twMerge('w-10 h-10 rounded-full bg-red-500 bg-blue-500'); // w-10 h-10 rounded-full bg-blue-500

More usage examples

Take a look at the TailwindMerge for PHP repository.

Configuration

If you are using Tailwind CSS without any extra config, you can use TailwindMerge right away. And stop reading here.

If you're using a custom Tailwind config, you may need to configure TailwindMerge as well to merge classes properly.

By default TailwindMerge is configured in a way that you can still use it if all the following apply to your Tailwind config:

  • Only using color names which don't clash with other Tailwind class names
  • Only deviating by number values from number-based Tailwind classes
  • Only using font-family classes which don't clash with default font-weight classes
  • Sticking to default Tailwind config for everything else

If some of these points don't apply to you, you need to customize the configuration.

Configure Prefix

You can configure the prefix directly in the config/tailwind-merge.php configuration file or by setting the environment variable:

env
TAILWIND_MERGE_PREFIX=tw-

Modify merge process

If TailwindMerge is not able to merge your changes properly you can modify the merge process by modifying existing class groups or adding new class groups.

For example, if you want to add a custom font size of "very-large":

php
// config/tailwind-merge.php

return [
  'classGroups' => [
      'font-size' => [
          ['text' => ['very-large']]
      ],
  ],
];

For a more detailed explanation of the configuration options, visit the original package documentation.

Contributing

Thank you for considering contributing to TailwindMerge for Laravel! The contribution guide can be found in the CONTRIBUTING.md file.


TailwindMerge for PHP is an open-sourced software licensed under the MIT license.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHP

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言