CSS Flexbox: A Practical Guide With Real Examples (2026)

Flexbox is a CSS layout mode that arranges items along one axis, either a row or a column. Set display: flex on a parent, then control spacing with justify-content on the main axis and align-items on the cross axis. That is the whole mental model.
Part of the complete web design guide.
On this page
- The one idea that makes Flexbox click
- Main axis and cross axis
- The properties you’ll actually use
- On the container
- On the items
- Real layouts, not toy examples
- A navbar with the logo left and links right
- A card row that wraps on mobile
- Sticky footer on a short page
- Where people get tripped up
- Browser support in 2026
- When to use it, when to skip it
- FAQ
- Is Flexbox still relevant now that CSS Grid exists?
- Why isn’t my flex item shrinking?
- Should I use flexbox or float for layout in 2026?
Flexbox has been in every browser for a decade now, and it’s still the tool I reach for first when I need to lay something out on one axis. The problem is most Flexbox tutorials front-load a wall of properties before showing you a single useful layout. So let’s flip that. Here’s how it actually works, with code you can paste into a project right now.
A quick note on where I’m coming from: I build sites for a living, mostly for small businesses around here, and I’ve watched a lot of people fight CSS because they never learned the mental model behind it. Flexbox is worth ten minutes of real understanding.

The one idea that makes Flexbox click
Flexbox is one-dimensional. It lays things out along a single axis at a time. That axis is either a row or a column, and everything else in Flexbox is about controlling how items sit along that axis and how they behave across it.
You turn a container into a flex container with one line:
.row {
display: flex;
}
The moment you do that, every direct child becomes a flex item. Their default behavior changes. Block-level children that used to stack vertically now sit side by side, shrink to fit, and stop respecting their width in the way you might expect. That surprise is where most confusion starts.

Main axis and cross axis
This is the part people skip and then wonder why justify-content isn’t doing what they want.
- Main axis: the direction items flow. With
flex-direction: row(the default) it’s horizontal. Withcolumn, vertical. - Cross axis: perpendicular to the main axis. Row means the cross axis is vertical. Column means it’s horizontal.
Two properties do most of the alignment work, and they map to those axes:
justify-contentaligns items along the main axis.align-itemsaligns items along the cross axis.
So centering something both ways is genuinely two lines:
.hero {
display: flex;
justify-content: center; /* horizontal, because row is default */
align-items: center; /* vertical */
min-height: 60vh;
}
If you switch to flex-direction: column, those two flip. Same properties, opposite visual result. That’s the whole trick.
The properties you’ll actually use
On the container
display: flexordisplay: inline-flex.flex-direction:row,row-reverse,column,column-reverse.flex-wrap: lets items wrap to the next line instead of squishing. Default isnowrap, which is why things get tiny on mobile.gap: spacing between items. Use this instead of margins. It’s supported everywhere and it doesn’t add space on the outer edges.justify-content:flex-start,center,space-between,space-around,space-evenly.align-items:stretch(default),flex-start,center,flex-end,baseline.
On the items
flex-grow: how much extra space an item takes if there’s room.flex-shrink: whether it gives up space when there isn’t.flex-basis: the starting size before growing or shrinking.align-self: override the container’salign-itemsfor one item.order: change visual order without touching the HTML.
You’ll see flex: 1 a lot. That’s shorthand for flex-grow: 1; flex-shrink: 1; flex-basis: 0%. It means “take an equal share of the available space.” Handy for equal-width columns.
Real layouts, not toy examples
A navbar with the logo left and links right
.nav {
display: flex;
align-items: center;
justify-content: space-between;
gap: 1.5rem;
padding: 1rem 1.5rem;
}
.nav ul {
display: flex;
gap: 1.5rem;
list-style: none;
margin: 0;
padding: 0;
}
No floats, no clearfix hack, no absolute positioning. space-between pushes the two groups apart and the gap keeps the links from touching.
A card row that wraps on mobile
.cards {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
.card {
flex: 1 1 280px;
}
Read that flex value out loud: grow to fill, shrink if needed, but start at 280px. When the container is wide enough for three 280px cards plus gaps, you get three. Narrow it and they wrap. One rule, responsive behavior, no media queries.
Sticky footer on a short page
body {
display: flex;
flex-direction: column;
min-height: 100vh;
margin: 0;
}
main {
flex: 1;
}
The header and footer take their natural height, and main absorbs whatever’s left. On a page with barely any content, the footer still sits at the bottom of the viewport instead of floating up.
Where people get tripped up
A few things I see constantly when reviewing other people’s CSS:
- Using margins for spacing between items. You end up writing
:last-child { margin-right: 0 }forever. Usegap. - Forgetting
min-width: 0on flex items containing long text. A flex item defaults to a minimum size based on its content, so a long unbroken string or a wide table will refuse to shrink and blow out your layout. Settingmin-width: 0on the item lets it shrink properly. - Reaching for Flexbox for full page grids. If you’re laying out rows and columns at the same time, that’s CSS Grid’s job. Flexbox and Grid aren’t competitors. I use both on nearly every project.
- Assuming
align-items: stretchis broken. It’s the default, and it’s why your items are all the same height. Usually that’s what you want. When it isn’t, set it explicitly.
Browser support in 2026
Flexbox is fully supported in every current browser. If you’re still writing -webkit- prefixes for it, you’re supporting browsers that stopped mattering years ago. The one gap that lingered was gap in flex containers on older Safari, and that’s long since resolved. You can use it without a fallback.
If you’re maintaining a site built around floats and table layouts, Flexbox is usually the cleanest thing to migrate toward, one section at a time. It doesn’t require rewriting your HTML, which is what makes it a low-risk change. That matters if you’re paying someone to touch a site you depend on for leads. A good Long Island web design job should leave you with CSS you can actually read and change later, not a pile of overrides.
When to use it, when to skip it
Use Flexbox for:
- Navigation bars and toolbars
- Button groups and form rows
- Centering anything
- Distributing a handful of items along one line
- Card rows that need to wrap
Skip it for:
- Two-dimensional page layouts (use Grid)
- Simple vertical stacks where
display: blockand margins already work
That’s the honest version. Flexbox is a tool with a narrow, well-defined job, and once the axis model clicks, you stop guessing and start writing layout code in one pass.
FAQ
Is Flexbox still relevant now that CSS Grid exists?
Yes. They solve different problems. Grid handles rows and columns together, Flexbox handles distribution along a single axis. Most real layouts use both, often nested inside each other.
Why isn’t my flex item shrinking?
Almost always the content minimum size. Add min-width: 0 to the item (or min-height: 0 if it’s in a column) and it will shrink as expected.
Should I use flexbox or float for layout in 2026?
Flexbox. Floats were never designed for layout, and the clearfix workarounds are unnecessary now. Reserve floats for what they were meant for, like wrapping text around an image.


