when setting out to design a grid layout for mobile first with tailwind css, i stumbled upon this gem that saved me hours of tweaking: [
grid-cols-auto-1
]
this class dynamically adjusts the number and size based on screen width, but it's not well-documented. essentially:
<div class="flex grid-flow-col gap-x-[20px] sm:flex-wrap md:[grid-template-columns:auto''\-auto''auto auto\_sm:repeat(3,\-1fr) lg:\[email protected](min-width\:965px){4}]]"><!-- your items here -->the magic is in the `md` breakpoint where it switches from a flexible gap to repeating columns. but get this - on screens wider than
720 pixels , you can specify exactly how many grid areas should be used!
i tested across various devices and found that on my 13" laptop, four items looked perfect; anything less or more made the layout feel cramped.
so if your project is going to span multiple screen sizes:
- start with auto columns for mobile
-use sm:repeat() in medium screens
-and tweak lg:[grid-template-columns] where needed
this approach gives you a lot of flexibility while keeping code clean and readable. give it
a try !