Container
The element with display: flex. It creates the flex formatting context.
Flexbox is a one-dimensional layout system. It arranges items along a main axis and can align, wrap and distribute space with very little CSS.
.actions {
display: flex;
flex-wrap: wrap;
gap: 1rem;
}
Layout
When you set display: flex on a container, its direct children become flex items. The container controls the axis, spacing and alignment.
Flexbox is ideal for navigation, button rows, toolbars, cards in a row, media objects and any layout where one dimension matters most.
The mental model is axis first. flex-direction decides the main axis, justify-content works along that main axis and align-items works across it.
The element with display: flex. It creates the flex formatting context.
The direct children that are arranged by flexbox.
The direction items flow: row, row-reverse, column or column-reverse.
The perpendicular direction used by align-items and align-content.
Visual model
Good layout CSS is not a pile of positions. It is a set of relationships: flow, axis, tracks, placement, layering, sticking and rhythm.
Create a flex container.
Choose the main axis.
Set spacing between items.
Allow items to continue on another line.
Examples
.actions {
display: flex;
flex-wrap: wrap;
gap: 1rem;
align-items: center;
}
.button { display: inline-block; margin-right: 17px; }
.button:last-child { margin-right: 0; }
.actions { white-space: nowrap; }
Rules that matter
Layout work is where real content, real devices and real users expose weak assumptions. The goal is a structure that explains itself in code and survives change.
If items mainly flow in a row or column, flexbox is usually a good fit.
Most flex behavior is controlled by the parent.
gap is simpler and works for wrapping rows.
flex-wrap prevents row layouts from breaking on narrow screens.
flex: 1 means grow, shrink and basis decisions at once.
If rows and columns both matter, CSS Grid may be cleaner.
Production thinking
Flexbox makes common interface rows simple and durable. It replaces many old float, inline-block and margin hacks.
Flexbox can visually reorder items. Keep keyboard and reading order sensible.
Use flex for navigation, toolbars, action rows and small component alignment. Do not use it as a universal layout hammer.
Flexbox does not change content meaning, but readable source order still matters for crawlers and users.
Live code lab
The preview runs in an isolated iframe. Links and forms stay inside the practice area, so you can experiment without leaving the lesson.
Mini assignment
Practice assignment
Try it yourself
Self-check
Answer these questions before moving on. If the answer is vague, change the lab layout and inspect the result.
Senior audit upgrade
Most flexbox confusion comes from forgetting that justify-content follows the main axis, while align-items crosses it.
Main axis is horizontal in typical left-to-right writing.
Main axis becomes vertical, so justify-content now works vertically.
If both rows and columns matter equally, CSS Grid may be the cleaner layout tool.