Skip to main content
drupalreleases

Use direct release data with your AI assistant.

Learn more

Tailwind Merge Classes

Security covered Maintenance fixes only 46 sites

This module provides a Twig extension to automatically merge and optimize Tailwind CSS class lists, resolving conflicts to ensure a clean output. It can output classes as a string or an array, making it easy to use with Drupal's theming system.

Introduction

Tailwind Merge Classes is a Drupal module for users working with Tailwind CSS in a component-based syntax. It adds a Twig filter, |tw_merge, that merges Tailwind CSS classes, resolves conflicts automatically, and keeps the class list clean. The older tw_merge() and tw_merge_as_array() functions are still available for existing templates.

If you are interested in more details, this video explains the original JavaScript utility tailwind-merge that this module's logic is based on.

Using the Filter

  • {{ 'p-4 bg-white p-2'|tw_merge }} outputs class="bg-white"
  • {{ ['p-4', 'bg-white', 'p-2']|tw_merge }} outputs class="bg-white"
  • {{ attributes.addClass(['p-4', 'bg-white', 'p-2']|tw_merge).removeClass('p-2') }} outputs class="bg-white"

Legacy Functions

Sites that already use the tw_merge() and tw_merge_as_array() Twig functions can keep using them. They are still available and were not removed, so existing templates keep working without changes. The recommended approach is the tw_merge filter: use it in new templates, and switch existing ones when convenient.

The functions need you to choose the return type, because a string and an array are used in different places:

  • tw_merge(classes) or tw_merge(classes, as='string'): returns a string, for printing inside class="...". A string can't be used with removeClass(), because Drupal stores it as a single class.
  • tw_merge(classes, as='array'): returns an array, for addClass() and removeClass(), where each class must be a separate item.
  • tw_merge_as_array(classes): the earlier way to get an array, which works the same as as='array'.

The new filter doesn't need this choice. It returns a value that works both when printed and inside addClass() and removeClass(), so you can just use |tw_merge with the filter syntax everywhere.

Each function and its filter equivalent are listed in the legacy guide for your version:

Guides

Version 2.x (Tailwind CSS 4.x)

Version 1.x (Tailwind CSS 3.x)

Class Prefix (Optional)

If your Tailwind setup uses a class prefix, set it at Configuration » Development » Tailwind Merge - Add Prefix (/admin/config/development/tailwind-merge):

  • Version 2.x: enter the value from prefix(tw), for example tw, for classes like tw:flex.
  • Version 1.x: enter the value from tailwind.config.js exactly, for example tw-, for classes like tw-flex.

Version Support

  • Module version 1.x: Compatible only with Tailwind CSS 3.x
  • Module version 2.x: Compatible only with Tailwind CSS 4.x

Third-Party Libraries

This module depends on different PHP packages to handle merging of Tailwind CSS classes for each Tailwind version.

Both PHP packages require PHP 8.1 and above.

Drupal 10–11

Ask your assistant about Tailwind Merge Classes

Check compatibility with your Drupal and PHP version, maintenance and security coverage, from current release data. How it works

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
17
Tracked since
Jan 2025
Latest release
4 weeks ago
Releases (12 mo)
14 ▲ from 3
Maintenance
Active

Release Timeline

Releases

Version Type Core PHP Release date
2.3.0 Stable 10–11 Oct 31, 2026
1.3.0 Stable 10–11 Oct 31, 2026
2.2.3 Stable 10–11 Sep 10, 2026
1.2.2 Stable 10–11 Sep 10, 2026
2.2.2 Stable 10–11 Aug 20, 2026
2.2.1 Stable 10–11 Aug 20, 2026
1.2.1 Stable 10–11 Aug 20, 2026
2.2.0 Stable 10–11 May 24, 2026
1.2.0 Stable 10–11 May 24, 2026
2.1.0 Stable 10–11 Jan 18, 2026