Skip to main content
BERTA MATULIAUSKIENE@Berta.codes
  • AI ADOPTION
  • PERSONAL BRAND
  • BLOG
  • ABOUT
  • RESOURCES
    • SETUP
    • AI PROMPTS
    • APPS
  • AI ADOPTION
  • PERSONAL BRAND
  • BLOG
  • ABOUT
  • RESOURCES
    • SETUP
    • AI PROMPTS
    • APPS
Appearance
Find Me Online

Find me online

Explore

  • About Berta
  • Articles
  • Newsletter

Resources

  • AI prompt library
  • My apps

Consulting

  • Practical AI adoption
  • Personal brand review
  • hello@bertacodes.com

STAY CURIOUS

PrivacyRSS feed

© 2026 BERTA CODES

Back to blog
Tailwind CSSCSSBest Practices

Tailwind CSS Class Organization and Linting in 2025

A practical guide to Tailwind CSS class ordering, organization patterns, and linting tools.

March 12, 2025
Updated October 23, 2026
Berta Matuliauskiene
6 min read
Tailwind CSS Class Organization and Linting in 2025

As projects grow, long Tailwind class lists can become difficult to read. Consistent formatting helps, but it is only one part of keeping components maintainable. This guide covers class ordering, editor diagnostics, and ways to reuse styles without hiding how a component works.

These examples target Tailwind CSS 3.x and Prettier 3. The setup below pins prettier-plugin-tailwindcss to version 0.6.14, whose documentation supports this combination. This is a versioned example for an existing Tailwind 3 project, not a Tailwind 4 setup guide.

Correction — October 23, 2026: An earlier version incorrectly described an official Stylelint plugin and included invalid comments inside class attributes. The setup and examples below have been corrected.

Class Organization Principles#

Recommended Class Order#

Instead of maintaining a custom ordering checklist, let Tailwind's official Prettier plugin handle it. Its order follows Tailwind's generated CSS, with layout-related utilities generally before decorative ones, state variants after plain utilities, and responsive variants toward the end. The exact order is explained in Tailwind's class sorting guide.

Here's a button with its classes kept together:

<button
  type="submit"
  class="m-2 flex w-full max-w-xs items-center justify-center rounded-lg bg-blue-600 px-4 py-2 text-sm font-semibold text-white shadow-md hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500 sm:w-auto md:text-base"
>
  Submit
</button>

Class sorting makes the code easier to scan. It does not decide which of two conflicting utilities you intended to use. Remove unintended conflicts rather than relying on the order of class names in the HTML.

Keep Comments Outside Class Attributes#

A class or className value contains class names. CSS comment syntax placed inside that value becomes extra text in the class list; it does not create a comment.

Put explanations beside the element instead:

<!-- One column by default, then two or three as space allows. -->
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2 md:grid-cols-3">
  <!-- Content -->
</div>

In JSX, use a JSX comment outside the attribute when an explanation is useful. Describe why a layout behaves a certain way rather than labelling every utility.

Automated Class Sorting with Prettier#

Tailwind Labs maintains prettier-plugin-tailwindcss. It handles formatting and class sorting; it is not a general-purpose linter or a guarantee that your styles are correct.

For an existing Tailwind 3 project, install this documented version with Prettier 3:

npm install --save-dev prettier@3 prettier-plugin-tailwindcss@0.6.14

Add the plugin to your existing .prettierrc, keeping your other formatting settings:

{
  "plugins": ["prettier-plugin-tailwindcss"],
  "tailwindConfig": "./tailwind.config.js"
}

Adjust tailwindConfig to your actual configuration file; the path is relative to the Prettier configuration. The plugin can find tailwind.config.js beside that file automatically. The explicit option is useful when your configuration lives elsewhere.

This plugin version requires Prettier 3 and is ESM-only. Use the plugin name as shown above, not require('prettier-plugin-tailwindcss'). If you use other Prettier plugins, check compatibility and put the Tailwind plugin last. These options and requirements are documented in the version 0.6.14 README.

Run formatting on a file you want to update, then check it:

npx prettier --write src/components/ProductCard.jsx
npx prettier --check src/components/ProductCard.jsx

Replace the example path with a file in your project. Review the diff and commit the dependency lockfile alongside the configuration so teammates use the same installed versions. A --check command can also run in CI to catch formatting changes without rewriting files.

Linting and Editor Feedback#

Formatting and linting solve different problems. Prettier gives class lists a consistent order. Diagnostics can flag suspicious combinations or unsupported directives, which still need a developer's judgment.

The official Tailwind CSS IntelliSense extension provides completions, CSS previews, and diagnostics in VS Code. For example, its cssConflict rule warns when classes on the same element set the same CSS properties. Its editor warnings are separate from a command-line CI lint check.

If your team needs additional command-line Tailwind rules, choose a linter that explicitly supports your Tailwind and ESLint versions and follow that release's configuration guide. Do not assume a community plugin is an official Tailwind tool, or copy an older ESLint configuration into a project using a different config format. Keep one tool responsible for class ordering so formatters and lint fixes do not compete.

Practical Examples#

Card Component with Organized Classes#

The classes below stay on the elements they style. The layout becomes horizontal on larger screens, while the image and spacing adjust with it:

function ProductCard({ product, onAddToCart }) {
  return (
    <div className="mx-auto flex w-full max-w-sm flex-col rounded-xl bg-white p-4 shadow-lg hover:shadow-xl sm:flex-row sm:p-6">
      <img
        src={product.image}
        alt={product.name}
        className="mb-4 h-48 w-full rounded-lg object-cover sm:mb-0 sm:mr-4 sm:h-auto sm:w-1/3"
      />
      <div className="flex grow flex-col">
        <h2 className="text-xl font-bold text-gray-900">{product.name}</h2>
        <p className="mt-2 text-gray-600">{product.description}</p>
        <div className="mt-4 flex items-center justify-between">
          <span className="text-lg font-bold text-blue-600">
            ${product.price}
          </span>
          <button
            type="button"
            onClick={() => onAddToCart(product)}
            className="rounded-lg bg-blue-600 px-4 py-2 font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500"
          >
            Add to Cart
          </button>
        </div>
      </div>
    </div>
  );
}

Navigation Bar Example#

This navigation uses wrapping instead of a hidden mobile menu. All links remain available at small widths, without needing a separate toggle interaction:

function Navbar() {
  return (
    <nav
      aria-label="Main navigation"
      className="flex w-full flex-wrap items-center justify-between gap-4 bg-white px-4 py-3 shadow-md sm:px-6 md:px-8"
    >
      <a href="/" className="flex items-center">
        <img src="/logo.svg" alt="" className="mr-2 h-8 w-8" />
        <span className="text-xl font-bold text-gray-900">Company</span>
      </a>
      <ul className="flex flex-wrap items-center gap-4">
        <li>
          <a
            href="/products"
            className="font-medium text-gray-700 hover:text-blue-600"
          >
            Products
          </a>
        </li>
        <li>
          <a
            href="/about"
            className="font-medium text-gray-700 hover:text-blue-600"
          >
            About
          </a>
        </li>
        <li>
          <a
            href="/contact"
            className="font-medium text-gray-700 hover:text-blue-600"
          >
            Contact
          </a>
        </li>
      </ul>
    </nav>
  );
}

Best Practices for Large Projects#

Use Components for Repeated UI#

If you repeat both markup and classes, a component gives you one place to maintain them. You do not need to extract every element; start where reuse or a clear responsibility makes the code easier to follow.

function Card({ title, children }) {
  return (
    <section className="w-full max-w-md rounded-xl bg-white p-6 shadow-lg">
      <h2 className="mb-4 border-b border-gray-100 pb-4 text-xl font-bold">
        {title}
      </h2>
      {children}
    </section>
  );
}

function ProfileCard({ user }) {
  return (
    <Card title={user.name}>
      <p className="text-gray-700">{user.bio}</p>
    </Card>
  );
}

Use @apply Selectively#

In a Tailwind 3 stylesheet, @apply can collect a small repeated utility pattern into a custom class. It can be useful when you are working with templates and a component would add unnecessary complexity. In React, a reusable component is usually a better starting point. Tailwind's guide to reusing styles explains this tradeoff.

For example, in the main CSS file processed by Tailwind 3:

@tailwind base;
@tailwind components;
@tailwind utilities;

/* A small reusable pattern for template-based markup. */
@layer components {
  .btn-primary {
    @apply rounded-lg bg-blue-600 px-4 py-2 font-medium text-white hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500;
  }
}

Avoid moving every long class list into CSS simply to shorten the markup. A named class still needs to be maintained, and it does not capture a component's structure or behavior.

Conclusion#

Consistent class organization makes everyday changes easier to read and review. Let Prettier handle the ordering, use diagnostics to investigate possible mistakes, and extract components when they make repeated UI easier to maintain. Then check the result in the browser: neatly sorted classes cannot tell you whether the interface works well.

Share this post

Previous Article

Tailwind CSS 3 - A Beginner Guide

Next Article

Server Components vs Client Components i...

You might also like

Tailwind CSS 3 - A Beginner Guide

Tailwind CSS 3 - A Beginner Guide

March 11, 2025

Color psychology

Color psychology

February 9, 2020

How to Start Using AI at Work: Choose One Task You Can Check

How to Start Using AI at Work: Choose One Task You Can Check

October 23, 2026

Table of Contents

  • Class Organization Principles
  • Automated Class Sorting with Prettier
  • Linting and Editor Feedback
  • Practical Examples
  • Best Practices for Large Projects
  • Conclusion

Recent Posts

  • How to Start Using AI at Work: Choose One Task You Can Check
    October 23, 2026
  • A Personal Brand Profile Review: What Should a Stranger Understand?
    October 23, 2026
  • Harness Engineering: Building Our Own Tools While Teaching AI How We Work
    October 21, 2026
  • AI Automation: The Biggest Challenge Is Not How, but What, Where, When, and How Much
    October 17, 2026
  • When Human in the Loop Becomes Theatre
    October 8, 2026