Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

prettier-plugin-tailwindcss

tailwindlabs7.4mMIT0.6.11TypeScript support: included

A Prettier plugin for sorting Tailwind CSS classes.

readme

prettier-plugin-tailwindcss

A Prettier v3+ plugin for Tailwind CSS v3.0+ that automatically sorts classes based on our recommended class order.

Installation

To get started, install prettier-plugin-tailwindcss as a dev-dependency:

npm install -D prettier prettier-plugin-tailwindcss

Then add the plugin to your Prettier configuration:

// .prettierrc
{
  "plugins": ["prettier-plugin-tailwindcss"]
}

Upgrading to v0.5.x

As of v0.5.x, this plugin now requires Prettier v3 and is ESM-only. This means it cannot be loaded via require(). For more information see our upgrade guide.

Options

Specifying your Tailwind stylesheet path

When using Tailwind CSS v4 you must specify your CSS file entry point, which includes your theme, custom utilities, and other Tailwind configuration options. To do this, use the tailwindStylesheet option in your Prettier configuration.

Note that paths are resolved relative to the Prettier configuration file.

// .prettierrc
{
  "tailwindStylesheet": "./resources/css/app.css"
}

Specifying your Tailwind JavaScript config path

To ensure that the class sorting takes into consideration any of your project's Tailwind customizations, it needs access to your Tailwind configuration file (tailwind.config.js).

By default the plugin will look for this file in the same directory as your Prettier configuration file. However, if your Tailwind configuration is somewhere else, you can specify this using the tailwindConfig option in your Prettier configuration.

Note that paths are resolved relative to the Prettier configuration file.

// .prettierrc
{
  "tailwindConfig": "./styles/tailwind.config.js"
}

If a local configuration file cannot be found the plugin will fallback to the default Tailwind configuration.

Sorting non-standard attributes

By default this plugin sorts classes in the class attribute, any framework-specific equivalents like className, :class, [ngClass], and any Tailwind @apply directives.

You can sort additional attributes using the tailwindAttributes option, which takes an array of attribute names:

// .prettierrc
{
  "tailwindAttributes": ["myClassList"]
}

With this configuration, any classes found in the myClassList attribute will be sorted:

function MyButton({ children }) {
  return (
    <button myClassList="rounded bg-blue-500 px-4 py-2 text-base text-white">
      {children}
    </button>
  );
}

Sorting classes in function calls

In addition to sorting classes in attributes, you can also sort classes in strings provided to function calls. This is useful when working with libraries like clsx or cva.

You can sort classes in function calls using the tailwindFunctions option, which takes a list of function names:

// .prettierrc
{
  "tailwindFunctions": ["clsx"]
}

With this configuration, any classes in clsx() function calls will be sorted:

import clsx from 'clsx'

function MyButton({ isHovering, children }) {
  let classes = clsx(
    'rounded bg-blue-500 px-4 py-2 text-base text-white',
    {
      'bg-blue-700 text-gray-100': isHovering,
    },
  )

  return (
    <button className={classes}>
      {children}
    </button>
  )
}

Sorting classes in template literals

This plugin also enables sorting of classes in tagged template literals.

You can sort classes in template literals using the tailwindFunctions option, which takes a list of function names:

// .prettierrc
{
  "tailwindFunctions": ["tw"],
}

With this configuration, any classes in template literals tagged with tw will automatically be sorted:

import { View, Text } from 'react-native'
import tw from 'twrnc'

function MyScreen() {
  return (
    <View style={tw`bg-white p-4 dark:bg-black`}>
      <Text style={tw`text-md text-black dark:text-white`}>Hello World</Text>
    </View>
  )
}

This feature can be used with third-party libraries like twrnc or you can create your own tagged template by defining this "identity" function:

const tw = (strings, ...values) => String.raw({ raw: strings }, ...values)

Once added, tag your strings with the function and the plugin will sort them:

const mySortedClasses = tw`bg-white p-4 dark:bg-black`

Preserving whitespace

This plugin automatically removes unnecessary whitespace between classes to ensure consistent formatting. If you prefer to preserve whitespace, you can use the tailwindPreserveWhitespace option:

// .prettierrc
{
  "tailwindPreserveWhitespace": true,
}

With this configuration, any whitespace surrounding classes will be preserved:

import clsx from 'clsx'

function MyButton({ isHovering, children }) {
  return (
    <button className=" rounded  bg-blue-500 px-4  py-2     text-base text-white ">
      {children}
    </button>
  )
}

Preserving duplicate classes

This plugin automatically removes duplicate classes from your class lists. However, this can cause issues in some templating languages, like Fluid or Blade, where we can't distinguish between classes and the templating syntax.

If removing duplicate classes is causing issues in your project, you can use the tailwindPreserveDuplicates option to disable this behavior:

// .prettierrc
{
  "tailwindPreserveDuplicates": true,
}

With this configuration, anything we perceive as duplicate classes will be preserved:

<div
  class="
    {f:if(condition: isCompact, then: 'grid-cols-3', else: 'grid-cols-5')}
    {f:if(condition: isDark, then: 'bg-black/50', else: 'bg-white/50')}
    grid gap-4 p-4
  "
>
</div>

Compatibility with other Prettier plugins

This plugin uses Prettier APIs that can only be used by one plugin at a time, making it incompatible with other Prettier plugins implemented the same way. To solve this we've added explicit per-plugin workarounds that enable compatibility with the following Prettier plugins:

  • @ianvs/prettier-plugin-sort-imports
  • @prettier/plugin-pug
  • @shopify/prettier-plugin-liquid
  • @trivago/prettier-plugin-sort-imports
  • prettier-plugin-astro
  • prettier-plugin-css-order
  • prettier-plugin-import-sort
  • prettier-plugin-jsdoc
  • prettier-plugin-multiline-arrays
  • prettier-plugin-organize-attributes
  • prettier-plugin-organize-imports
  • prettier-plugin-style-order
  • prettier-plugin-svelte
  • prettier-plugin-sort-imports

One limitation with this approach is that prettier-plugin-tailwindcss must be loaded last.

// .prettierrc
{
  // ..
  "plugins": [
    "prettier-plugin-svelte",
    "prettier-plugin-organize-imports",
    "prettier-plugin-tailwindcss" // MUST come last
  ]
}

changelog

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.

Unreleased

  • Nothing yet!

0.6.11 - 2025-01-23

  • Support TypeScript configs and plugins when using v4 (#342)

0.6.10 - 2025-01-15

  • Add support for @zackad/prettier-plugin-twig (#327)
  • Drop support for @zackad/prettier-plugin-twig-melody (#327)
  • Update Prettier options types (#325)
  • Don't remove whitespace inside template literals in Svelte (#332)

0.6.9 - 2024-11-19

  • Introduce tailwindStylesheet option to replace tailwindEntryPoint (#330)

0.6.8 - 2024-09-24

0.6.7 - 2024-09-24

  • Improved performance with large Svelte, Liquid, and Angular files (#312)
  • Add support for @plugin and @config in v4 (#316)
  • Add support for Tailwind CSS v4.0.0-alpha.25 (#317)

0.6.6 - 2024-08-09

  • Add support for prettier-plugin-multiline-arrays (#299)
  • Add resolution cache for known plugins (#301)
  • Support Tailwind CSS v4.0.0-alpha.19 (#310)

0.6.5 - 2024-06-17

  • Only re-apply string escaping when necessary (#295)

0.6.4 - 2024-06-12

  • Export PluginOptions type (#292)

0.6.3 - 2024-06-11

  • Improve detection of string concatenation (#288)

0.6.2 - 2024-06-07

Changed

  • Only remove duplicate Tailwind classes (#277)
  • Make sure escapes in classes are preserved in string literals (#286)

0.6.1 - 2024-05-31

Added

  • Add new tailwindPreserveDuplicates option to disable removal of duplicate classes (#276)

Fixed

  • Improve handling of whitespace removal when concatenating strings (#276)
  • Fix a bug where Angular expressions may produce invalid code after sorting (#276)
  • Disabled whitespace and duplicate class removal for Liquid and Svelte (#276)

0.6.0 - 2024-05-30

Changed

  • Remove duplicate classes (#272)
  • Remove extra whitespace around classes (#272)

0.5.14 - 2024-04-15

Fixed

  • Fix detection of v4 projects on Windows (#265)

0.5.13 - 2024-03-27

Added

  • Add support for @zackad/prettier-plugin-twig-melody (#255)

0.5.12 - 2024-03-06

Added

  • Add support for prettier-plugin-sort-imports (#241)
  • Add support for Tailwind CSS v4.0 (#249)

0.5.11 - 2024-01-05

Changed

  • Bumped bundled version of Tailwind CSS to v3.4.1 (#240)

0.5.10 - 2023-12-28

Changed

  • Bumped bundled version of Tailwind CSS to v3.4 (#235)

0.5.9 - 2023-12-05

Fixed

  • Fixed location of embedded preflight CSS file (#231)

0.5.8 - 2023-12-05

Added

  • Re-enable support for prettier-plugin-marko (#229)

0.5.7 - 2023-11-08

Fixed

  • Fix sorting inside dynamic custom attributes (#225)

0.5.6 - 2023-10-12

Fixed

  • Fix sorting inside {{ … }} expressions when using @shopify/prettier-plugin-liquid v1.3+ (#222)

0.5.5 - 2023-10-03

Fixed

  • Sort classes inside className in Astro (#215)
  • Support member access on function calls (#218)

0.5.4 - 2023-08-31

Fixed

  • Type tailwindFunctions and tailwindAttributes as optional (#206)
  • Don’t break @apply … #{'!important'} sorting in SCSS (#212)

0.5.3 - 2023-08-15

Fixed

  • Fix CJS __dirname interop on Windows (#204)

0.5.2 - 2023-08-11

Fixed

  • Fix intertop with bundled CJS dependencies (#199)

0.5.1 - 2023-08-10

Fixed

  • Updated Prettier peer dependency (#197)

0.5.0 - 2023-08-10

Added

  • Sort expressions in Astro's class:list attribute (#192)
  • Re-enabled support for plugins when using Prettier v3+ (#195)

0.4.1 - 2023-07-14

Fixed

  • Don't move partial classes inside Twig attributes (#184)

0.4.0 - 2023-07-11

Added

  • Export types for Prettier config (#162)
  • Add Prettier v3 support (#179)

Fixed

  • Don't move partial classes inside Liquid script attributes (#164)
  • Do not split classes by non-ASCII whitespace (#166)
  • Match tagged template literals with tag expressions (#169)

0.3.0 - 2023-05-15

Added

  • Added support for prettier-plugin-marko (#151)
  • Allow sorting of custom attributes, functions, and tagged template literals (#155)

Fixed

  • Speed up formatting (#153)
  • Fix plugin compatibility when loaded with require (#159)

0.2.8 - 2023-04-28

Changed

  • Remove support for @prettier/plugin-php (#152)

0.2.7 - 2023-04-05

Fixed

  • Don't break liquid tags inside attributes when sorting classes (#143)

0.2.6 - 2023-03-29

Added

  • Support ESM and TS config files (#137)

Fixed

  • Load tailwindcss modules from nearest instance only (#139)

0.2.5 - 2023-03-17

Fixed

  • Fix class sorting in capture liquid tag (#131)

0.2.4 - 2023-03-02

Fixed

  • Sort class attribute on components and custom elements in Astro (#129)

0.2.3 - 2023-02-15

Fixed

  • Don't sort classes in Glimmer concat helper (#119)
  • Add support for @ianvs/prettier-plugin-sort-imports (#122)

0.2.2 - 2023-01-24

Fixed

  • Add prettier plugins to peer dependencies (#114)
  • Traverse await expression blocks in Svelte (#118)

0.2.1 - 2022-12-08

Fixed

  • Fix support for latest Shopify Liquid plugin (#109)

0.2.0 - 2022-11-25

Changed

  • Don't bundle prettier-plugin-svelte (#101)

Added

  • Improve compatibility with other Prettier plugins (#101, #102)

0.1.13 - 2022-07-25

Fixed

  • Fix error when using Angular pipes (#86)

0.1.12 - 2022-07-07

Added

  • Add support for Glimmer / Handlebars (#83)

0.1.11 - 2022-05-16

Changed

  • Update prettier-plugin-svelte to v2.7.0 (#77)

Fixed

  • Fix sorting in Svelte :else blocks (#79)

0.1.10 - 2022-04-20

Removed

  • Remove whitespace tidying and duplicate class removal due to issues with whitespace removal (#72)

0.1.9 - 2022-04-19

Added

  • Add license file (#64)
  • Add whitespace tidying and duplicate class removal (#70)

0.1.8 - 2022-02-24

Changed

  • Use Tailwind's getClassOrder API when available (#57)

Fixed

  • Fix Tailwind config file resolution when Prettier config file is not present (#62)

0.1.7 - 2022-02-09

Fixed

  • Fix single quotes being converted to double quotes (#51)

0.1.6 - 2022-02-08

Fixed

  • Fix error when no Prettier options provided (#46)

0.1.5 - 2022-02-04

Added

  • Add support for MDX (#30)

Fixed

  • Fix error when formatting Svelte files that contain let:class attributes (#24)

0.1.4 - 2022-01-25

Fixed

  • Handle empty class attributes (#17)
  • Handle TypeScript syntax in Vue/Angular class attributes (#18)

0.1.3 - 2022-01-24

Fixed

  • Ignore !important when sorting @apply classes (#4)

0.1.2 - 2022-01-24

Fixed

  • Fix error when using nullish coalescing operator in Vue/Angular (#2)