Sub topic 7
Layout with flexbox and grid
Tailwind exposes the CSS flexbox and grid layout modes as utility classes. You put a layout class on a parent element, and its direct children are arranged accordingly. This chapter shows how to do that, how to choose between the two, and which mistakes come up most often.
Prerequisites
This chapter assumes you know the following:
- HTML structure and the CSS box model.
- The ideas behind flexbox (main axis, cross axis) and grid (rows, columns, tracks). You don't need to remember every property name.
- How to apply Tailwind utility classes in markup and how spacing utilities like
p-4andmt-2work.
The parent controls the layout
In both systems, the container sets the layout and the children are the items. A container has to opt in with flex or grid. Without that class, the other utilities do nothing.
flexmakes the element a flex container. Its children become flex items.gridmakes the element a grid container. Its children become grid items.inline-flexandinline-griddo the same, but the container itself flows inline.
Utilities such as justify-between or gap-4 only take effect once the element has a layout class.
Flexbox basics
Flexbox lays items out along one axis. Tailwind names its utilities after the CSS properties they set.
| Goal | Utility | CSS produced |
|---|---|---|
| Row (default) | `flex-row` | `flex-direction: row` |
| Column | `flex-col` | `flex-direction: column` |
| Allow wrapping | `flex-wrap` | `flex-wrap: wrap` |
| Space along the main axis | `justify-start`, `justify-center`, `justify-between`, `justify-end` | `justify-content` |
| Align on the cross axis | `items-start`, `items-center`, `items-end`, `items-stretch` | `align-items` |
| Gap between items | `gap-4`, `gap-x-2`, `gap-y-6` | `gap`, `column-gap`, `row-gap` |
Here is the navigation bar from the topic plan:
<nav class="flex items-center justify-between p-4">
<a href="/" class="text-lg font-bold">Brand</a>
<ul class="flex gap-4">
<li><a href="/docs">Docs</a></li>
<li><a href="/blog">Blog</a></li>
</ul>
</nav>The nav is a flex row. items-center centers the logo and the list vertically. justify-between pushes the logo to the left edge and the list to the right edge. The inner ul is its own flex container, so gap-4 separates the links. Each flex container is independent, so layouts nest freely.
Which axis does each utility use?
justify-* always acts on the main axis, and items-* always acts on the cross axis. With flex-col, the main axis becomes vertical. In a column, justify-center centers items vertically and items-center centers them horizontally. Many layout bugs come from forgetting that the axes swap.
<div class="flex h-64 flex-col items-center justify-center gap-2 bg-slate-100">
<p class="font-semibold">Centered both ways</p>
<p class="text-sm text-slate-600">The column needs a height for justify-center to matter.</p>
</div>justify-center only has visible effect here because h-64 gives the container more room than its content needs. Without a height, the container shrinks to fit its content and there is nothing to center within.
Controlling how items grow and shrink
Flex items can also be told how to use leftover space. These utilities go on the items, not the container.
flex-1lets an item grow and shrink from a zero basis, so severalflex-1siblings share space equally.growlets an item take up extra space.grow-0is the opposite.shrink-0stops an item from being squeezed below its natural size.basis-1/3orbasis-64sets the starting size before growing or shrinking.self-start,self-center, andself-endoverrideitems-*for one item.ml-autopushes an item, and everything after it, to the far end of the row.
A common pattern is a row with an avatar, flexible text, and a button:
<div class="flex items-center gap-3 rounded-lg bg-white p-3 shadow">
<img src="/avatar.png" alt="" class="h-10 w-10 shrink-0 rounded-full">
<p class="min-w-0 flex-1 truncate">A very long display name that would otherwise push the button off the row</p>
<button class="shrink-0 rounded bg-indigo-600 px-3 py-1 text-white">Follow</button>
</div>The avatar and button keep their size because of shrink-0. The text takes the remaining space because of flex-1.
The min-w-0 class matters. A flex item has an automatic minimum width equal to its content, so a long unbroken string stops it from shrinking and truncate never kicks in. Setting min-w-0 removes that minimum.
Grid basics
Grid places items in rows and columns at once. Use it when alignment matters in two directions, such as a gallery, a dashboard, or a page with a sidebar.
| Goal | Utility |
|---|---|
| Create a grid | `grid` |
| N equal columns | `grid-cols-3` |
| N equal rows | `grid-rows-2` |
| Gap between cells | `gap-4`, `gap-x-4`, `gap-y-2` |
| Item spans columns | `col-span-2`, `col-span-full` |
| Item spans rows | `row-span-2` |
| Item starts at a line | `col-start-2` |
grid-cols-3 produces grid-template-columns: repeat(3, minmax(0, 1fr)). Each column gets an equal share of the width, and the minmax(0, 1fr) part lets columns shrink below their content size. That avoids the overflow problem described above for flex items.
A page with a sidebar and a main area, using a four-column grid:
<div class="grid grid-cols-4 gap-6">
<aside class="col-span-1 rounded bg-slate-100 p-4">Sidebar</aside>
<main class="col-span-3 rounded bg-slate-50 p-4">Main content</main>
</div>Spans must fit the track count. If a row can't hold an item's span, the item moves to the next row. That can leave gaps.
Grid with breakpoints
Grid combines naturally with breakpoint prefixes. A later chapter covers the prefixes in detail. The idea here is that unprefixed utilities apply to every width, and prefixed ones apply from that width upward:
<div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3">
<div class="rounded bg-slate-100 p-4">One</div>
<div class="rounded bg-slate-100 p-4">Two</div>
<div class="rounded bg-slate-100 p-4">Three</div>
</div>The layout has one column by default, two from sm, and three from lg. The cards themselves don't change.
Choosing between flexbox and grid
As a rule of thumb:
- Use flex when the content decides the sizes. Navigation bars, button groups, and media rows are typical.
- Use grid when the layout decides the sizes. Card galleries, forms with aligned labels, and page shells are typical.
- Mix them. A grid of cards, where each card is a
flex flex-colstack, is very common.
Gaps, not margins
Prefer gap-* over margins on children. A gap only appears between items, so there is no stray space after the last one. It works the same in flex and grid and adapts when items wrap.
Tailwind also has space-x-* and space-y-*. They add margins to children using a selector, and they can misbehave with wrapping and reordered items. gap is usually the safer choice.
Common errors
- Forgetting
flexorgridon the parent.justify-betweenon a plaindivdoes nothing. - Putting container utilities on the item.
items-centerbelongs on the parent. The per-item version isself-center. - Mixing up the axes in
flex-col. Remember thatjustify-*follows the main axis. - Expecting vertical centering with no height. The container needs extra room, for example
h-64ormin-h-screen. - Text overflow in flex items. Add
min-w-0to the item that holds the long text. - Squashed icons or images. Add
shrink-0to items that must keep their size. - Spans larger than the grid.
col-span-4in a three-column grid creates an implicit extra column.
Exercise
Exercise
A card row uses <div class="flex gap-4"> with three cards. On narrow screens the cards squeeze and their text overflows. Name two changes that keep the flex layout but let the cards wrap onto new lines, and say what justify-center would do if you added it to the same container.
Show answer
Add flex-wrap to the container so items move to a new line instead of shrinking, and give each card a size it can wrap around, such as basis-64 or w-64, with shrink-0 if it must not compress. Adding justify-center would center each line of cards along the main axis (horizontally in a row), leaving equal empty space on both sides of each line.
Exercise
You need a dashboard with a header that spans the full width, a sidebar one column wide, and a main area three columns wide, all in a four-column grid. Which classes go on the container, the header, the sidebar, and the main area?
Show answer
Container: grid grid-cols-4 gap-4. Header: col-span-4. Sidebar: col-span-1. Main area: col-span-3. The header fills the first row, and the sidebar and main area share the second row.