DEV Community

Cover image for tailwindCSS's classes order pattern "best practice" ๐Ÿƒ
mohammad mohagheghian
mohammad mohagheghian

Posted on • Edited on

tailwindCSS's classes order pattern "best practice" ๐Ÿƒ

Using patterns ๐Ÿ“Š in your codes is always a pretty great thing๐Ÿ‘ because your code looks much better ๐Ÿ‘€, in the future, you won't be confuse ๐Ÿง , and other developers ๐Ÿง‘โ€๐Ÿ’ป that contribute to your project will understand it better, especially in open source projects ๐Ÿ“–.

Some patterns are confusing ๐Ÿง , and hard to remember ๐Ÿ˜ก since one of the goals of the tailwindCSS is not to be complicated ๐Ÿฆพ, I try a lot to make as much as easy, and easy to customize ๐Ÿ–Œ๏ธ.

I also made a GitHub repository for you guys to be able to contribute and make it better. ๐Ÿ˜‰

Basics

Let's take a look at it.

Image description

You may say what is special about this piece of code ๐Ÿค”๐Ÿ’ญ, the answer is that during coding we have no pattern and methodology we just type class names that we thinking of, and this will make our code dirty ๐Ÿ’ฉ.

Logic

the logic behind orders and patterns is we go from outside to inside ๐Ÿ“ฅ. It means first we define positions, after that margin, padding, outline, and border, and then we define element inner styles like display, align-items, background-color, text-color, font properties, etc. ๐ŸšŒ Next we define class names like transitions, animations, and external CSS classes.

Responsive

Some developers prefer to use media queries at the end of the string which is not perfect ๐Ÿ”” because it's easier to have all media queries of one element in one look ๐Ÿ‘๏ธ. like below.

Image description


Orders

The most important thing in this pattern is orders but how do we categorize them in groups, and what are the categories? ๐Ÿ‘†


1. position, inset, top, bottom...

Including position, inset, top, left, bottom, and right.

1๏ธโƒฃ position => absolute, relative, static...
2๏ธโƒฃ inset => inset-2, inset-4...
3๏ธโƒฃ top => top-6, top-10...
4๏ธโƒฃ right => right-6, right-10...
5๏ธโƒฃ bottom => bottom-6, bottom-10...
6๏ธโƒฃ left => left-6, left-10...

<div className="absolute top-10 right-10">
   <h1>Position</h1>
</div>
Enter fullscreen mode Exit fullscreen mode

2. Margin, Padding, outline, and border

the Order of the directions are clockwise ๐Ÿ”ƒ, like: mt-0 => mr-0 => mb-0 => ml-0

1๏ธโƒฃ Margin
2๏ธโƒฃ Padding
3๏ธโƒฃ Outline
4๏ธโƒฃ Border

<div className="my-10 p-24 outline-none border-2">
   <h1>Margin, Padding, Outline, and Border</h1>
</div>
Enter fullscreen mode Exit fullscreen mode

3. Height, width, min and max-width, and min and max-height

1๏ธโƒฃ width => w-10
2๏ธโƒฃ height => h-10
3๏ธโƒฃ min-width => h-5
4๏ธโƒฃ min-height => h-5
5๏ธโƒฃ max-width => h-16
6๏ธโƒฃ max-height => h-16

<div className="w-10 h-4 min-h-2 max-w-36 max-h-10">
   <h1>Width, Height, min-..., and max-...</h1>
</div>
Enter fullscreen mode Exit fullscreen mode

4. Display (grid or flexbox items in addition)

The order in this category does not matter ๐Ÿซ , because it may be different. ๐Ÿ›ซ

<div className="flex items-center justify-content flex-col">
   <h1>Flex, and Grid</h1>
</div>
Enter fullscreen mode Exit fullscreen mode

5. Background-color, and box-shadow

1๏ธโƒฃ background color
2๏ธโƒฃ box shadow
3๏ธโƒฃ drop-shadow

<div className="bg-red-900 shadow-xl drop-shadow-lg">
   <h1>background-color, and box-shadow, and drop-shadow</h1>
</div>
Enter fullscreen mode Exit fullscreen mode

6. Text, and font

First, we define text color, then text font-size, and last but not least font-weight.

1๏ธโƒฃ color => text-red-600, text-white...
2๏ธโƒฃ font-size => text-lg, text-3xl...
3๏ธโƒฃ font-height => leading-9, leading-5...
4๏ธโƒฃ font-weight => font-semibold, font-medium...

<div className="text-red-600 text-sm leading-4 font-semibold">
   <h1>Color, font-size, font-height, and font-weight</h1>
</div>
Enter fullscreen mode Exit fullscreen mode

these most common classes, so for more classes you can add them at the end of the category.


7. Transitions, and animations

1๏ธโƒฃ transition => transition-all duration-750...
2๏ธโƒฃ animation => animate-spin

<div className="transition-all duration-1000 animation-ping">
   <h1>Transition, and Animation</h1>
</div>
Enter fullscreen mode Exit fullscreen mode

8. Filters

Due to a large number of cases, it's not great.

<div className="backdrop-blur-lg">
   <h1>Filters</h1>
</div>
Enter fullscreen mode Exit fullscreen mode

Conclusion

I developing a react project with this method, and after finishing it I will share the link here. So be sure to save it ๐Ÿ˜‰๐Ÿ˜ˆ.

back to top

keep in touch

Keep Coding Y'All ๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป

Top comments (6)

Collapse
 
maciejadamski89 profile image
Maciej Adamski

Just use 'Headwind" - a VSCode extension ;)

Collapse
 
mohammad-mghn profile image
mohammad mohagheghian

Good tip!

Collapse
 
xanwtf profile image
Xan • Edited

Iโ€™ve always followed โ€œthe order of f$?keryโ€ even back without Tailwind. Just so happens thatโ€™s what the Headwind extension uses too. Order your CSS files in the amount of โ€œf@&keryโ€ they can cause. Position, display, flex, width, height, border, font size, background, color. Everything higher up the chain is likely to cause the most issues.

Collapse
 
manuartero profile image
Manuel Artero Anguita ๐ŸŸจ

I thought I use emojis heavely ... till I read your post ๐Ÿ˜…

Good post by the way :)

Collapse
 
mohammad-mghn profile image
mohammad mohagheghian

Yeah, because I found it boring, so added many more emojis

Collapse
 
fruntend profile image
fruntend

ะกongratulations ๐Ÿฅณ! Your article hit the top posts for the week - dev.to/fruntend/top-10-posts-for-f...
Keep it up ๐Ÿ‘