Sub topic 4
Building layouts with flexbox and grid
This chapter assumes you know how CSS flexbox and grid work, and that you have a Tailwind v4 project set up in Vite and React. Tailwind does not add a new layout system. Each utility class maps to one CSS declaration, so flex is display: flex and grid-cols-3 is grid-template-columns: repeat(3, minmax(0, 1fr)).
Choosing flex or grid
Use flexbox when items sit in one row or one column and you want them to size themselves. Use grid when you need rows and columns that line up together.
Flexbox: a navigation bar
Add flex to the container. Direction, alignment and spacing then come from separate utilities.
export function NavBar() {
return (
<header className="flex items-center justify-between border-b border-gray-200 bg-white px-6 py-3">
<span className="text-lg font-semibold text-gray-900">Acme</span>
<nav className="flex gap-4 text-sm text-gray-600">
<a href="/docs">Docs</a>
<a href="/pricing">Pricing</a>
<a href="/about">About</a>
</nav>
</header>
)
}The classes work like this:
items-centersetsalign-items: center, which centers children on the cross axis.justify-betweenpushes the first and last children to opposite ends of the main axis.gap-4adds space between children. Prefer it to margins on each child, because it adds no extra space at the edges.
These are the flex utilities you will use most often:
| Class | CSS effect |
|---|---|
| `flex-col` | Stack children vertically |
| `flex-wrap` | Let children wrap to a new line |
| `flex-1` | Grow and shrink to share free space |
| `shrink-0` | Never shrink below the natural size |
| `justify-center` | Center along the main axis |
Grid: a card layout
For a grid, set the column count with grid-cols-*. Spacing works the same as in flexbox.
export function Dashboard() {
return (
<div className="grid grid-cols-3 gap-4 p-6">
<section className="col-span-2 rounded-lg border border-gray-200 bg-white p-6">
Main chart
</section>
<section className="rounded-lg border border-gray-200 bg-white p-6">
Summary
</section>
<section className="col-span-3 rounded-lg border border-gray-200 bg-white p-6">
Activity table
</section>
</div>
)
}col-span-2 makes an item cover two columns. The next chapter shows how to make the column count change with screen width.
Combining both
Real pages nest the two. A common pattern is a grid for the page frame and flexbox inside each cell:
export function Sidebar() {
return (
<div className="grid min-h-screen grid-cols-[16rem_1fr]">
<aside className="flex flex-col gap-2 bg-gray-900 p-4 text-gray-100">
<a href="/">Home</a>
<a href="/settings">Settings</a>
<button className="mt-auto text-left">Sign out</button>
</aside>
<main className="p-8">Content</main>
</div>
)
}grid-cols-[16rem_1fr] uses an arbitrary value: a fixed 16rem sidebar and a content column that takes the rest. Underscores stand in for spaces. mt-auto on the button pushes it to the bottom of the flex column.
Common mistakes
- Forgetting
flexorgridon the parent. Utilities likeitems-centerandgap-4do nothing without it. - Using
justify-*anditems-*backwards. Withflex-col,justify-*controls the vertical axis anditems-*the horizontal one. - Using margins between siblings when
gap-*would do.
Exercise
A container has the classes items-center gap-4, but its children are stacked vertically with no gap. What is missing, and why?
Show answer
The container needs flex (or grid). items-center and gap-4 only take effect on a flex or grid container. Without display: flex or display: grid, the children stay as normal block elements.