Sub topic 7
Layout with flexbox and grid
This chapter assumes you know basic HTML and the CSS box model. It also assumes you know what flexbox and grid do in plain CSS. Tailwind exposes both as utility classes, so the work is learning the class names and when to choose each layout model.
Flexbox: one row or one column
Add flex to a container to make its children flex items. Three groups of utilities then control the layout:
flex-row(the default) andflex-colset the direction.justify-*aligns items along the main axis. Common values arejustify-start,justify-center,justify-between, andjustify-end.items-*aligns items along the cross axis. Common values areitems-start,items-center, anditems-stretch.
Use gap-* for space between items. It works on both flex and grid, so you don't need margins on each child.
<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. justify-between pushes the logo to one end and the list to the other. items-center centers both vertically. The inner <ul> is its own flex container, and gap-4 spaces the links.
Growing and shrinking
Flex items can resize. These utilities control that:
| Class | Effect |
|---|---|
| `flex-1` | The item grows and shrinks to share free space |
| `grow` | The item grows to fill free space |
| `shrink-0` | The item never shrinks below its natural size |
| `flex-wrap` | Items wrap to a new line instead of overflowing |
A common pattern is a fixed-size sidebar next to a flexible main area: give the sidebar w-64 shrink-0 and the main area flex-1.
Grid: rows and columns together
Use grid when you need to control both axes, or when you want equal columns. Add grid, then set the columns with grid-cols-N, which creates N equal-width columns. Use gap-* for spacing.
<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>This grid has one column by default, two from the sm breakpoint, and three from lg. Breakpoint prefixes are covered in the next chapter. For now, note that the unprefixed class applies to all sizes.
To make an item span several columns, use col-span-N on the child, for example col-span-2. Use col-span-full to span the whole row.
placeholderChoosing between flex and grid
A practical rule: flex lets the content decide sizes, while grid lets the container decide. Navigation bars, button groups, and a row of icon plus label suit flex. Card galleries and page frames suit grid. You can nest them freely, such as a grid of cards where each card is a flex column.
Common errors
- Forgetting the container class.
justify-betweenandgrid-cols-3do nothing unless the parent also hasflexorgrid. - Mixing up the axes. In
flex-col,justify-*works vertically anditems-*works horizontally. The axes swap with the direction. - Using margins for spacing between siblings. Prefer
gap-*on the parent. It avoids extra space after the last item. - Content overflowing a flex row. Add
flex-wrap, or let items shrink by removingshrink-0.
Exercise
You have a toolbar with a title on the left and two buttons on the right, all vertically centered. Which classes go on the toolbar element, and what makes the buttons sit together with space between them?
Show answer
Put flex items-center justify-between on the toolbar. Wrap the two buttons in an inner element with flex gap-2 so they stay together, with justify-between pushing that group to the right edge.
Exercise
A grid has grid grid-cols-3 gap-4. One child must fill an entire row. Which class do you add to that child?
Show answer
col-span-3, or col-span-full, which spans every column regardless of the column count.