Is there a best order to write Tailwind CSS utility classes? #18874
mdtaufiquekhan
started this conversation in
General
Replies: 1 comment
-
You can use a prettier plugin for Tailwind. There was one a few years back which you can find here: Or directly: I see it has Tailwind V4 support now. I'm not sure if you can define your own sorting order though.. Hope this helped a bit. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I’m pretty new to Tailwind CSS, and I’m trying to understand not just how to use it, but also how to keep my code clean and readable as I go.
I really like Tailwind’s utility-first approach. The idea is simple: instead of writing separate CSS files, you write all your styles directly in your HTML using Tailwind’s utility classes. I also read that Tailwind’s creator, Adam Wathan, doesn’t recommend overusing
@apply
, because Tailwind is designed for “writing styles as you go.”This also means that it’s totally fine to repeat the same classes on some identical elements — it doesn’t follow the traditional Don’t Repeat Yourself (DRY) rule, and that’s by design.
Now, here’s where I’m struggling: when I write a component that is fully styled with Tailwind (including responsive breakpoints and dark mode), I often end up with a lot of classes. For example:
When I’m coding, I don’t really follow a consistent order for these classes. Sometimes I start with typography, sometimes with spacing, sometimes with layout, etc. But when I come back later to make changes, it’s hard to read and find what I need.
I asked an AI tool, and it suggested an order like this:
That seems helpful, but I wanted to ask real developers:
Is there any widely accepted or recommended way to order Tailwind classes, or is it just personal style?
Thanks in advance! I’m still learning, so any tips or suggestions would really help me write better Tailwind code.
Beta Was this translation helpful? Give feedback.
All reactions