This example addresses the "Gotcha" about forgetting or omitting the base case for the smallest screens.
Below,
grid-cols-1
(or any base
grid-cols-*
) is omitted. Tailwind's mobile-first nature means it defaults to a single column flow for child `div` elements within a `grid` container if no other column definition matches. While it works, it's less explicit.
(Code:
<div class="grid sm:grid-cols-2 md:grid-cols-4">...</div>
)
- On XS screens, items stack, but
grid-cols-1
is not explicitly stated.
Below,
grid-cols-1
is explicitly defined for the smallest screens. This makes the intended layout behavior at all screen sizes perfectly clear.
(Code:
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-4">...</div>
)
- Clear declaration for all screen sizes.
While Tailwind CSS is mobile-first, and omitting a base
grid-cols-*
utility might result in a single-column layout on small screens (due to default block behavior of child elements or other implicit grid flow rules), explicitly setting the base case (e.g.,
grid-cols-1
) is a best practice. It improves code readability and ensures your intended layout is unambiguous for the smallest viewports.