What is CSS flexbox?
Flexbox is a one-dimensional layout model: a flex container lays its children out along a single main axis — a row or a column — and lets them grow, shrink and align along it and along the perpendicular cross axis. It is the right tool for navigation bars, toolbars, card footers, media objects and centering; for two-dimensional page layouts where rows and columns must line up, use CSS Grid instead.
Container properties control the group: flex-direction sets the main axis, flex-wrap allows items to flow onto new lines, justify-content distributes free space along the main axis, align-items aligns items on the cross axis, align-content spaces wrapped lines, and gap adds gutters between items without margins.
Item properties control individuals: flex-grow decides how much of the leftover space an item takes, flex-shrink how much it gives up when space runs out, and flex-basis its starting size before either happens. The flex shorthand sets all three — flex: 1 means 1 1 0%, which makes items share space equally regardless of their content. align-self overrides the cross-axis alignment for one item and order changes its visual position.
How to use the CSS Flexbox Generator
- Choose a direction, wrapping, main-axis (justify-content) and cross-axis (align-items) alignment, and set the row and column gaps.
- Set the number of items and pick a preview size mode to see how alignment reacts to different item sizes.
- Click an item in the preview to edit its flex-grow, flex-shrink, flex-basis, align-self and order. Customised items get a dashed border.
- Start from a preset such as Navbar, Equal columns or Sidebar + main if you prefer.
- Copy the container CSS, per-item CSS and HTML markup.
Example
A header with a logo on the left and navigation pushed to the right:
.header {
display: flex;
align-items: center;
gap: 16px;
}
.header nav {
margin-left: auto;
}Flexbox tips and gotchas
- Flex items have
min-width: autoby default, so long words or wide content can stop them from shrinking. Addmin-width: 0to the item to allow it. justify-contenthas no effect when an item hasflex-grow— there is no free space left to distribute.- An auto margin (
margin-left: auto) absorbs free space and is the simplest way to push one item to the far end. orderand*-reversedirections only change the visual order. Screen readers and keyboard focus still follow the HTML order, so avoid reordering meaningful content.align-contentonly works when items wrap onto more than one line.- Use
gapinstead of margins between items. It is supported for flexbox in every current browser and never adds space at the edges. - Make gaps and sizes fluid with the CSS Clamp Generator, and convert pixel values with PX to REM. For two-dimensional layouts switch to the CSS Grid Generator.
Frequently Asked Questions
What is the difference between flexbox and grid?
Flexbox lays items out in one dimension and lets content size drive the layout; grid lays them out in rows and columns at the same time, with the layout defined on the container. Use flexbox for components and alignment, grid for page-level and card layouts where columns must line up.
How do I center a div with flexbox?
On the parent set display: flex; justify-content: center; align-items: center;. Give the parent a height (for example min-height: 100vh) if you want vertical centering in the viewport. The Center anything preset does exactly this.
What does flex: 1 mean?
flex: 1 expands to flex-grow: 1; flex-shrink: 1; flex-basis: 0%. Every item starts from zero width and shares the space equally, which is why it is the standard way to make equal-width columns.
Why are my flex items not wrapping?
The default is flex-wrap: nowrap, which keeps every item on one line and shrinks them instead. Set flex-wrap: wrap and give items a flex-basis or width so they have a size to wrap at.
What is the difference between justify-content and align-items?
justify-content works along the main axis (horizontal for rows, vertical for columns). align-items works along the cross axis. Changing flex-direction to column swaps which one controls horizontal alignment.