Skip to main content
ponderloop

CSS Flexbox Generator

To center an item both ways, give its container display: flex; justify-content: center; align-items: center; A 80 × 60 px item in a 480 × 240 px container then sits 200px from each side and 90px from the top and the bottom. Set the direction, wrap, alignment, gap and the grow, shrink and basis of each item, watch the layout change, and copy the CSS and an HTML skeleton.

Updated Runs in your browser: nothing you enter is uploadedHow this tool works

Live layout preview

What this layout does

  • A block-level flex container, 480px wide and 240px high, holds 1 item.
  • flex-direction: row. The main axis is horizontal, running left to right, so item 1 is placed at the left and the next items follow towards the right. The cross axis is vertical, running top to bottom.
  • flex-wrap: nowrap. All items stay on one line. They shrink if flex-shrink allows it, otherwise they overflow the container.
  • justify-content: center. Items are packed in the middle of the main axis, with equal free space before and after the group.
  • The items and gaps need 80px of the 480px available, so 400px is free to distribute.
  • align-items: center. Items are centred on the cross axis of their line.
  • align-content: stretch has no effect, because with flex-wrap: nowrap the container has a single line.
  • There is no gap between items.
  • The item has the initial values (flex-grow 0, flex-shrink 1, flex-basis auto, align-self auto, order 0).

Container settings

Presets

display
flex-direction
flex-wrap

Item settings

CSS and HTML

CSS

.container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 480px;
  height: 240px;
}

.item {
  width: 80px;
  height: 60px;
}

HTML skeleton

<div class="container">
  <div class="item">1</div>
</div>

What each flexbox property does

One row per value the generator offers, written from the same lists as the controls. The generator treats flex-start, stretch, nowrap and the other values marked initial as defaults and leaves them out unless “Include default values” is on: the real initial value of justify-content, align-items and align-content is normal, which behaves as flex-start (justify-content) or stretch (align-items and align-content) in a flex container.
PropertySet onValueEffect
displayContainerflexA block-level flex container: it starts on its own line and, with no width set, fills the width of its parent.
displayContainerinline-flexAn inline-level flex container: it sits in the line like a word and, with no width set, is only as wide as its items.
flex-directionContainerrow (initial)The main axis is horizontal, running left to right: item 1 sits at the left and the next items follow towards the right.
flex-directionContainerrow-reverseThe main axis is horizontal, running right to left: item 1 sits at the right and the next items follow towards the left.
flex-directionContainercolumnThe main axis is vertical, running top to bottom: item 1 sits at the top and the next items follow towards the bottom.
flex-directionContainercolumn-reverseThe main axis is vertical, running bottom to top: item 1 sits at the bottom and the next items follow towards the top.
flex-wrapContainernowrap (initial)All items stay on one line. They shrink if flex-shrink allows it, otherwise they overflow the container.
flex-wrapContainerwrapItems that do not fit move to a new line. New lines are stacked from the cross start towards the cross end.
flex-wrapContainerwrap-reverseItems that do not fit move to a new line, but new lines are stacked in the opposite direction, from the cross end towards the cross start.
justify-contentContainerflex-start (initial)Items are packed towards the start of the main axis.
justify-contentContainerflex-endItems are packed towards the end of the main axis.
justify-contentContainercenterItems are packed in the middle of the main axis, with equal free space before and after the group.
justify-contentContainerspace-betweenItems are spread out along the main axis: free space is split into equal gaps between them, and the first and last touch the two ends.
justify-contentContainerspace-aroundItems are spread out along the main axis with equal free space on both sides of each one, so the space between two is twice the space at either end.
justify-contentContainerspace-evenlyItems are spread out along the main axis with equal free space between them and at both ends.
align-itemsContainerstretch (initial)Items without a fixed cross size grow to fill the whole line on the cross axis.
align-itemsContainerflex-startItems sit at the cross start of their line.
align-itemsContainerflex-endItems sit at the cross end of their line.
align-itemsContainercenterItems are centred on the cross axis of their line.
align-itemsContainerbaselineItems are aligned so that the first lines of their text share one baseline.
align-contentContainerflex-startLines are packed towards the start of the cross axis.
align-contentContainerflex-endLines are packed towards the end of the cross axis.
align-contentContainercenterLines are packed in the middle of the cross axis, with equal free space before and after the group.
align-contentContainerspace-betweenLines are spread out along the cross axis: free space is split into equal gaps between them, and the first and last touch the two ends.
align-contentContainerspace-aroundLines are spread out along the cross axis with equal free space on both sides of each one, so the space between two is twice the space at either end.
align-contentContainerspace-evenlyLines are spread out along the cross axis with equal free space between them and at both ends.
align-contentContainerstretch (initial)Lines grow equally to fill the cross size of the container.
gapContainer<length> (initial 0)Space between rows and between columns, never at the outer edges. One value sets both; two values are row-gap then column-gap.
flex-growItem0 (initial)The item keeps its base size and takes none of the free space.
flex-growItem<number> above 0The item takes a share of the free space equal to its factor divided by the sum of the factors on its line.
flex-shrinkItem1 (initial)When the items are too big for the line, this item shrinks along with the others, in proportion to its base size.
flex-shrinkItem0The item never shrinks below its base size and may overflow the container.
flex-shrinkItem<number> above 1The item shrinks faster than items with a smaller factor.
flex-basisItemauto (initial)The base size comes from the item’s width (or height in a column), or from its content when that is auto.
flex-basisItem<length>The base size on the main axis, before free space is shared out. A basis of 0 makes equal flex-grow factors give equal sizes.
flex-basisItem<percentage>The base size as a share of the container’s main size.
align-selfItemauto (initial)The item follows the container’s align-items value.
align-selfItemstretchThis item grows to fill its line on the cross axis if it has no fixed cross size.
align-selfItemflex-startThis item sits at the cross start of its line.
align-selfItemflex-endThis item sits at the cross end of its line.
align-selfItemcenterThis item is centred on the cross axis of its line.
align-selfItembaselineThis item is aligned by the first line of its text with the other baseline-aligned items.
orderItem0 (initial)Items are laid out by ascending order value; items with the same value keep their source order.
orderItem<integer> above or below 0A lower number moves the item towards the start of the main axis, a higher one towards the end. Only the visual order changes, not the order of the HTML.

Main axis and cross axis for each flex-direction

justify-content moves items along the main axis and align-items moves them along the cross axis, so the same two properties change direction when flex-direction changes. Left to right text is assumed; in a right to left page the horizontal sides swap.
flex-directionMain axisItems run fromCross axisMain sizeGaps
rowhorizontal, left to rightleft to rightvertical, top to bottomwidth (flex-basis acts on it)column-gap between items, row-gap between lines
row-reversehorizontal, right to leftright to leftvertical, top to bottomwidth (flex-basis acts on it)column-gap between items, row-gap between lines
columnvertical, top to bottomtop to bottomhorizontal, left to rightheight (flex-basis acts on it)row-gap between items, column-gap between lines
column-reversevertical, bottom to topbottom to tophorizontal, left to rightheight (flex-basis acts on it)row-gap between items, column-gap between lines

How justify-content shares the free space: three 60px items in a 400px row

The free space is 220px. The last column is the distance of each item’s left edge from the container’s left edge, computed by the tool’s own distribution function.
justify-contentBefore the first itemBetween itemsAfter the last itemItem left edges
flex-start0px0px220px0px, 60px, 120px
flex-end220px0px0px220px, 280px, 340px
center110px0px110px110px, 170px, 230px
space-between0px110px0px0px, 170px, 340px
space-around36.667px73.333px36.667px36.667px, 170px, 303.333px
space-evenly55px55px55px55px, 170px, 285px

Worked examples

Each row is built by the generator above (the presets and two more); the last column is the summary the tool writes for it, with sizes in px.
LayoutContainer CSSItem CSSWhat happens
Centereddisplay: flex; justify-content: center; align-items: center;Item: width: 80px; height: 60px;The items and gaps need 80px of the 480px available, so 400px is free to distribute. The item has the initial values (flex-grow 0, flex-shrink 1, flex-basis auto, align-self auto, order 0).
Navbar with space-betweendisplay: flex; justify-content: space-between; align-items: center;Every item: width: 90px; height: 40px;The items and gaps need 360px of the 560px available, so 200px is free to distribute. Every item has the initial values (flex-grow 0, flex-shrink 1, flex-basis auto, align-self auto, order 0).
Sidebar layoutdisplay: flex;Item 1: flex-shrink: 0; flex-basis: 160px; Item 2: flex-grow: 1;Item 1 never shrinks, starts from a main size of 160px. Item 2 grows with factor 1 (it takes a share of the free space).
Equal-width columnsdisplay: flex; gap: 16px;Every item: flex-grow: 1; flex-basis: 0;The items and gaps need 32px of the 560px available, and the 528px of free space goes to the 3 items with flex-grow, in proportion to their grow factors (total 3), so justify-content has nothing left to distribute. Every item grows with factor 1 (it takes a share of the free space), starts from a main size of 0.
Wrapping card griddisplay: flex; flex-wrap: wrap; align-content: flex-start; gap: 16px;Every item: flex-grow: 1; flex-basis: 120px; height: 80px;Every item grows with factor 1 (it takes a share of the free space), starts from a main size of 120px.
Sticky footer columndisplay: flex; flex-direction: column;Item 1: height: 40px; Item 2: flex-grow: 1; Item 3: height: 40px;Item 2 grows with factor 1 (it takes a share of the free space).
Reverse the rowdisplay: flex; flex-direction: row-reverse;Every item: width: 80px; height: 60px;The items and gaps need 240px of the 480px available, so 240px is free to distribute. Every item has the initial values (flex-grow 0, flex-shrink 1, flex-basis auto, align-self auto, order 0).
Centered vertical stackdisplay: flex; flex-direction: column; justify-content: center; align-items: center;Every item: width: 80px; height: 60px;The items and gaps need 180px of the 300px available, so 120px is free to distribute. Every item has the initial values (flex-grow 0, flex-shrink 1, flex-basis auto, align-self auto, order 0).

How to build a CSS flexbox layout

  1. Pick a preset or start from the defaults. A preset such as Navbar with space-between or Wrapping card grid loads a complete layout. Otherwise start with a row of three items and change one setting at a time.
  2. Set the container. Choose display, flex-direction and flex-wrap with the radios, then justify-content, align-items and align-content with the lists. Set the gap in px or rem, and the width and height of the preview container.
  3. Choose how many items, then edit one at a time. Set the number of items (1 to 12), pick an item to edit, and change its flex-grow, flex-shrink, flex-basis, align-self, order and demo size. “Copy this item’s settings to all items” gives every item the same values.
  4. Read the preview and the summary. The numbered boxes show the layout. Under them the summary says in words what the settings do, including how much free space is left on the main axis when every item has a known size.
  5. Copy the CSS and the HTML. Declarations that equal the CSS initial value are left out, so the CSS stays short; tick “Include default values” to write them too, or untick the demo sizes to copy only the flex properties. Copy CSS and Copy HTML give the stylesheet and the markup.

Frequently asked questions

How do I center something with flexbox?
Put display: flex; justify-content: center; align-items: center; on the parent. justify-content: center centers the items along the main axis (horizontally in a row) and align-items: center centers them on the cross axis (vertically). The parent needs a height larger than its content for the vertical centering to show: in the example it is 240px, so the 60px item sits 90px from the top.
What is the difference between justify-content and align-items?
justify-content places items along the main axis, align-items along the cross axis, and flex-direction decides which is which. In a row the main axis is horizontal; with flex-direction: column it turns vertical, so the same two properties swap their screen directions. The Centered vertical stack example uses display: flex; flex-direction: column; justify-content: center; align-items: center; and centers a column of items both ways.
Why does justify-content do nothing?
It only distributes free space on the main axis, so it has nothing to do when there is none. Either the items already use all the space or more (they need 240px of the 200px available: with flex-shrink 0 the items overflow the container by 40px.), or an item with flex-grow absorbs it: The items and gaps need 240px of the 480px available, and the 240px of free space goes to the 1 item with flex-grow, in proportion to their grow factors (total 1), so justify-content has nothing left to distribute. The other usual cause is a container that is only as wide as its content, such as an inline-flex container with no width.
What is the difference between align-items and align-content?
align-items aligns each item inside its own line on the cross axis; align-content positions the whole set of lines inside the container and applies when flex-wrap lets the items form several lines. With flex-wrap: nowrap the container has one line and align-content has no effect, which the summary under the preview says whenever it is the case. In the Wrapping card grid the 8 items form 2 lines, and align-content: flex-start packs them at the start of the 300px height.
What do flex-grow, flex-shrink and flex-basis do?
flex-basis is the starting size on the main axis, flex-grow says how the free space is shared among the items (0 means none), and flex-shrink says how the items shrink when they do not fit. The initial values are flex-grow: 0, flex-shrink: 1, flex-basis: auto, align-self: auto, order: 0. The flex shorthand packs the three: flex: 1 means flex-grow 1, flex-shrink 1 and a basis of 0. This generator writes the longhands, so each value is visible and none is overridden by accident.
How do I make equal-width columns?
Give every item flex-grow: 1 and flex-basis: 0, as the Equal-width columns preset does. The basis is 0, so the free space is the whole line and equal factors give equal widths whatever the content: in a 560px container with 3 items and a 16px gap, each column is 176px. With flex-basis auto, columns with more content would come out wider.
How does flex-wrap decide when to start a new line?
An item moves to the next line when its base size plus the gap no longer fits. With a 560px container, 120px basis and 16px gap, 4 items fit on a line, so 8 items make 2 lines. With flex-grow: 1 the items of a line then widen to share the leftover space, here 128px each. wrap-reverse stacks the new lines the other way.
Does the order property change the tab order?
No. order only changes where an item is drawn: keyboard focus, screen readers and copy and paste still follow the order of the HTML. Use it for visual rearrangement of the same content, and change the HTML itself when the reading order should change. Items with equal order values keep their source order, and a negative value moves an item before the others.
What does gap do that margins do not?
gap puts space between items and between lines only, never at the outer edges, so you do not need first-child and last-child margin exceptions. It takes one value for both directions or two, row-gap first and then column-gap. The generator writes it in px or rem and writes a single value when both are equal.
Why does my flex item not shrink below its content?
A flex item has an automatic minimum size, so it will not shrink below the size of its content (a long word, an image, a wide table) even when flex-shrink allows it. Setting min-width: 0 on the item (min-height: 0 in a column) lets it shrink further, and overflow: hidden or auto does the same. The preview boxes only hold a number, so this does not show up here; it appears when real content is inside.

How it works

Every setting is validated before any CSS is written. The keyword settings must be one of the values CSS defines for the property; numbers are read as plain digits with an optional decimal point, rounded to three decimals and range checked (items 1 to 12, flex-grow and flex-shrink up to 100, flex-basis up to 5000px or 1000%, order from -100 to 100, gap up to 500px or 50rem, container width 50 to 1200px and height 50 to 800px), so the tool never writes an invalid declaration. Declarations equal to the initial value are left out: the initial value of justify-content, align-items and align-content is normal, which behaves as flex-start, stretch and stretch in a flex container (CSS Box Alignment Level 3), so the omitted result is the same layout. A value shared by every item goes into one .item rule and a value that differs goes into an .item:nth-child rule. The free space in the summary is computed for a one-line container when every item has a definite main size (its flex-basis, or its width in a row or height in a column), with rem counted as 16px; it ignores borders, padding and margins, which the demo boxes do not have. The preview applies the validated declarations to numbered boxes in a container of the size you set and scales the whole picture down when the page is narrower than the container; the layout itself is computed by your browser at full size. Flexbox is described by CSS Flexible Box Layout Module Level 1 and the alignment keywords and gap by CSS Box Alignment Module Level 3.

Sources

More: Color and CSSAll developer toolsAll toolsHome & DIY calculators