About the Flexbox generator
Flexbox is the CSS layout model for arranging items in a row or column: navigation bars, toolbars, centered content, equal-height columns and wrapping lists of cards.
This generator lets you try every flexbox setting and see the result right away. Change the container's direction, wrapping and alignment, click an item to change how it grows, shrinks or is ordered, and copy clean CSS that includes only the properties you changed.
How to use the Flexbox generator
- Pick a common layout, or set the container's flex-direction, flex-wrap, justify-content, align-items and gap.
- Click an item in the preview to select it, then set its flex-grow, flex-shrink, flex-basis, align-self or order.
- Change the number of items to see how the layout behaves with more or less content.
- Click Copy CSS. Put
.containeron the parent element; the item rules use:nth-child(), so rename them to your own classes if you like.
Examples
Center an item both ways
Input
justify-content: center · align-items: centerOutput
.container {
display: flex;
justify-content: center;
align-items: center;
}Navigation bar with the last item pushed right
Input
align-items: center · gap 16px · margin-left: auto on item 3Output
.container {
display: flex;
align-items: center;
gap: 16px;
}
.container > :nth-child(3) {
margin-left: auto;
}Common errors and how to fix them
align-items: center does nothing
The container is only as tall as its content, so there is no extra space to center in. Give the container a height, such as min-height: 100vh.
Items overflow instead of wrapping
Flex items stay on one line by default. Add flex-wrap: wrap, and check that items don't have a fixed width larger than the container.
Text in a flex item won't shrink or truncate
Flex items have min-width: auto, so they won't shrink below their content. Set min-width: 0 on the item so text-overflow: ellipsis can work.
Good to know
justify-contentaligns items along the main axis (horizontal forrow), andalign-itemsalong the cross axis (vertical forrow). Switching tocolumnswaps the two.flex: 1 1 0(grow 1, basis 0) makes items share the space equally regardless of their content, which is the usual way to make equal columns.align-contentonly matters when items wrap onto more than one line, so it appears only when wrapping is on.- Push the last item to the end adds an auto margin, the standard trick for a navbar with a logo on the left and a button on the right.
orderchanges only the visual order. Screen readers and keyboard focus still follow the HTML order, so keep the two consistent.
Frequently asked questions
How do I center a div with flexbox?
Give the parent display: flex; justify-content: center; align-items: center;. The parent needs a height, such as min-height: 100vh, for vertical centering to be visible. Pick Center everything to see it.
When should I use flexbox instead of grid?
Use flexbox for one-dimensional layouts, a single row or column whose items size themselves by content. Use grid when you need rows and columns aligned together, like a page layout or a card grid.
Why doesn't flex-grow make my items equal?
flex-grow shares out the leftover space, so items with more content stay wider. Set flex-basis: 0 as well (flex: 1 1 0) so all the space is shared equally.
How do I make equal-height columns with flexbox?
Put the columns in a flex row: .columns { display: flex; gap: 1rem; } and .column { flex: 1 1 0; }. Items in a row stretch to the height of the tallest one, because align-items defaults to stretch, so every column ends up the same height and width. If they don't, look for align-items set to another value or a fixed height on a column. To line up buttons at the bottom of cards, make each card a column too (display: flex; flex-direction: column;) and give the button margin-top: auto.