Flexbox & Grid layout
Build common layouts (nav bars, cards, dashboards) with Flexbox and Grid.
By Invicly Tech, , in Frontend with React & Next.js. About 9 minutes to read.
In short
Flexbox lays items out along one axis, which suits nav bars and toolbars. Grid controls rows and columns together, which suits card grids and page layouts. Most real layouts combine both.
Explained simply
Layout means deciding where boxes go. Modern CSS gives you two main tools. Flexbox lines up items in one direction, a row or a column. Grid places items in rows and columns at the same time, like cells in a spreadsheet.
Use Flexbox when the content decides the layout: a nav bar where links take the space they need, or a row of buttons. Use Grid when the layout decides where content goes: a card gallery with equal columns, or a page with a header, sidebar and main area.
Both work the same way at the start. You set display: flex or display: grid on the parent, called the container. Its direct children become items and follow the container's rules. You rarely need to position each child by hand.
You can mix them freely. A page might use Grid for the overall shape, and Flexbox inside the header to line up the logo and the links. Choosing well comes from asking one question: am I arranging a line, or a two-dimensional pattern?
Press Play on a diagram to walk through it one step at a time.
- One dimension: row or column
- Content decides item sizes
- justify-content and align-items
- Great for nav bars and toolbars
- Two dimensions: rows and columns
- Container defines the tracks
- grid-template-areas for named regions
- Great for cards and page layouts
- 1Look at the design
Find the parent and its direct children
- 2One direction?
A row or a column of items
- 3Yes: Flexbox
Content size drives the layout
- 4No: Grid
Rows and columns must line up
- 5Nest freely
Grid for the page, Flexbox inside parts
- Flex container
- flex-direction
- flex-wrap
- justify-content
- align-items
- gap
- Flex item
- flex (grow shrink basis)
- align-self
- order
- Grid container
- grid-template-columns
- grid-template-areas
- gap
- place-items
- Grid item
- grid-column
- grid-row
- grid-area
In depth
How flex items grow and shrink
Each flex item has three settings, usually written together as flex: grow shrink basis. Basis is the starting size before any space is shared. Grow says how much of the leftover space this item takes. Shrink says how much it gives up when space runs out.
flex: 1 means 1 1 0%: start from zero and share space equally, so all items end up the same width. flex: auto means 1 1 auto: start from the content size, then share the rest, so wider content stays wider. flex: none means do not grow or shrink at all.
Items also have min-width: auto by default, so long words or wide images can stop an item from shrinking. Setting min-width: 0 on the item often fixes overflow.
- flex: 1 for equal columns
- flex: none for fixed-size items like icons
- min-width: 0 fixes overflowing text in flex items
- gap adds space between items without margins
- Means 1 1 0%
- Starts from zero size
- Shares space equally
- Equal-width columns
- Means 1 1 auto
- Starts from content size
- Shares the leftover space
- Wider content stays wider
- Means 0 0 auto
- Never grows
- Never shrinks
- Icons and fixed-size items
Grid tracks: fr, minmax and auto-fill
In Grid you describe the tracks (rows and columns) on the container. grid-template-columns: 200px 1fr gives a fixed 200px sidebar and a main column that takes the rest. The fr unit means a fraction of the free space after fixed tracks are placed.
The most useful pattern is repeat(auto-fill, minmax(220px, 1fr)). It says: fit as many columns as you can, each at least 220px, and stretch them to fill the row. The grid becomes responsive with no media queries. auto-fit is similar, but it collapses empty tracks so a few items stretch across the full row.
Alignment words that work in both
Flexbox and Grid share alignment properties. justify-content moves the whole group along the main axis (in Grid, the row direction). align-items aligns each item on the cross axis. In Grid, justify-items does the same across columns, and place-items: center sets both at once.
This gives the classic one-line center: display: grid; place-items: center. For a nav bar, justify-content: space-between pushes the first and last items to the edges. To push a single flex item to the far end, give it margin-left: auto.
Named areas for page layouts
grid-template-areas lets you draw the layout with words. Each string is a row, each word is a cell, and items claim an area with grid-area. Repeating a name makes the area span several cells, and a dot leaves a cell empty.
The best part is changing layouts. In a media query you redefine only the template, for example putting the sidebar under main on phones, and every item moves with no changes to the HTML.
- "header header"
- "sidebar main"
- "footer footer"
- "header"
- "main"
- "sidebar"
- "footer"
Learn it in 6 short steps
About 1h each. Track them with a timer in your plan.
- 1Build a nav bar with Flexbox~1h
- 2Align and distribute items with Flexbox~1h
- 3Lay out a card grid with Grid~1h
- 4Use grid-template-areas for page layouts~1h
- 5Build a dashboard layout~1h
- 6Choose Flexbox or Grid per layout~1h
Key points
- Use Flexbox when items flow in a single line; use Grid when you need aligned rows and columns.
- gap works in both Flexbox and Grid and replaces margin hacks.
- repeat(auto-fill, minmax(16rem, 1fr)) builds a responsive card grid with no media queries.
- grid-template-areas lets you name regions like header, sidebar and main.
Key terms
Tap a card to flip it. 0/5 known
Examples
.nav {
display: flex;
justify-content: space-between;
align-items: center;
gap: 1rem;
}
.cards {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr));
gap: 1.5rem;
}A Flexbox nav bar and a responsive Grid of cards.
A card grid with no media queries
You have 9 product cards. Step 1: make the list a grid. Step 2: use repeat(auto-fill, minmax(220px, 1fr)) so each column is at least 220px. On a 1000px wide container, 4 columns fit (4 x 220 = 880 plus gaps); on a 500px phone, 2 columns fit; on a 300px screen, 1 column. Step 3: add gap for spacing. The cards reflow on every screen size automatically.
.products {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
gap: 1rem;
}A dashboard with named areas
The dashboard needs a header across the top, a sidebar on the left and main content beside it. Step 1: draw it with grid-template-areas. Step 2: give each child its grid-area. Step 3: on small screens, stack everything in one column by changing only the template.
.dash {
display: grid;
grid-template-columns: 240px 1fr;
grid-template-areas: "head head" "side main";
min-height: 100vh;
}
.dash header { grid-area: head; }
.dash aside { grid-area: side; }
.dash main { grid-area: main; }
@media (max-width: 48rem) {
.dash { grid-template-columns: 1fr;
grid-template-areas: "head" "main" "side"; }
}Quick reference
- Center anything: display: grid; place-items: center;
- Equal flex columns: give each item flex: 1
- Push one flex item to the end: margin-left: auto
- Responsive cards: repeat(auto-fill, minmax(220px, 1fr))
- Use gap for spacing in both Flexbox and Grid
- Long text overflowing a flex item? Add min-width: 0
- Span columns: grid-column: 1 / -1 stretches across the full grid
Common mistakes
- Forcing a two-dimensional layout into nested Flexbox rows.
- Using margins on children for spacing instead of gap.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Build a nav bar: logo on the left, three links on the right, all vertically centered.
- Exercise 2medium
Create a classic "holy grail" layout: header, footer, a 200px left nav, main content and a 200px right sidebar. The footer must stay at the bottom even when main is short.
- Exercise 3hard
A chat row has an avatar (fixed 40px), a message that should take all remaining width, and a timestamp. Long unbroken words overflow the screen. Fix it with Flexbox.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is Grid better than Flexbox?
- Neither is better; they solve different problems. Flexbox is for one-direction lines of items, Grid is for two-dimensional layouts, and most pages use both.
- Should I still use floats for layout?
- No. Floats are now only for wrapping text around an image. Flexbox and Grid replaced them for layout.
- Does the order property affect accessibility?
- Yes. order and grid placement change the visual order but not the reading or tab order, so keyboard users may jump around. Keep the HTML order logical.