CSS Portal

CSS Flexbox Generator

CSS Flexbox is one of the most powerful layout tools on the web, but its properties can be tricky to reason about without seeing them in action. This generator lets you tweak every container and item property in real time, with plain-English explanations alongside each control, live warnings when a property won't have any effect, and clean copy-paste CSS output.

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!
CSS Flexbox Generator
Live Preview
What's happening

flex-direction
row→ left to right (default)
row-reverse→ right to left
column↓ top to bottom
column-reverse↑ bottom to top
justify-content (main axis)
flex-startpack items to the start
flex-endpack items to the end
centercentre the group
space-betweengaps between, none at edges
space-aroundequal space around each item
space-evenlyperfectly equal gaps everywhere
align-items (cross axis, per row)
stretchitems fill the cross size (default)
flex-startalign to cross-start edge
flex-endalign to cross-end edge
centercentre on cross axis
baselinealign by text baseline
align-content (cross axis, all rows - wrap only)
stretchrows expand to fill space
flex-startrows packed to start
flex-endrows packed to end
centerrows centred
space-betweengaps between rows
space-aroundspace around each row
flex-grow / flex-shrink / flex-basis
flex-grow: 0don't grow (default)
flex-grow: 1take up available space
flex-shrink: 0never shrink below basis
flex-shrink: 1shrink proportionally (default)
flex-basis: autouse item's own size (default)
flex-basis: 0start from zero, grow from there
flex: 1shorthand for grow:1 shrink:1 basis:0
align-self / order
align-self: autoinherit container's align-items
align-self: centeroverride alignment for this item
order: 0default document order
order: -1move before siblings
order: 1move after siblings
If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

About CSS Flexbox Generator

What is Flexbox?

Flexbox (Flexible Box Layout) is a CSS layout model that makes it easy to arrange items in a row or column, distribute space between them, and align them along two axes. It was designed to solve common layout problems that were difficult with floats and positioning - things like vertically centring an element, or making a row of cards equal height.

The two axes

Every flex container has a main axis (the direction items flow, set by flex-direction) and a cross axis (perpendicular to it). justify-content controls alignment along the main axis. align-items controls alignment along the cross axis per row. align-content controls how multiple rows are spaced along the cross axis.

Container vs. item properties

Flexbox properties split into two groups. Container properties (display, flex-direction, flex-wrap, justify-content, align-items, align-content, gap) are set on the parent element. Item properties (flex-grow, flex-shrink, flex-basis, align-self, order) are set on individual children to override or extend the container's rules.

flex-grow, flex-shrink & flex-basis

These three properties control how items respond to available space. flex-basis sets the starting size before space is distributed. flex-grow determines how much of the leftover space an item claims. flex-shrink determines how much an item gives up when space is scarce. The shorthand flex: 1 sets all three to sensible growing defaults (1 1 0).

When to use Flexbox vs Grid

Flexbox is one-dimensional - it works along a single axis at a time, making it ideal for navbars, card rows, button groups, and any linear sequence of items. CSS Grid is two-dimensional - it controls rows and columns simultaneously, making it better for page-level layouts, data tables, and anything with an explicit grid structure. Many real-world layouts combine both.

Frequently Asked Questions

What is CSS Flexbox?

CSS Flexbox (Flexible Box Layout) is a one-dimensional layout model that lets you arrange elements in a row or column, distributing space between them and aligning them efficiently even when their sizes are unknown or dynamic. It is activated by setting display: flex or display: inline-flex on a container element, which immediately makes all of its direct children into flex items. Unlike older layout methods that relied on floats or tables, Flexbox was designed specifically for interface layout and handles alignment, ordering, and spacing in a far more predictable way.

What is the difference between justify-content and align-items?

justify-content controls how flex items are distributed along the main axis - the primary direction of the flex container. If flex-direction is row (the default), this means horizontal alignment. If flex-direction is column, it means vertical. align-items controls how flex items are aligned along the cross axis - perpendicular to the main axis. For a row-direction container, this means vertical alignment. A simple way to remember it: justify-content aligns along the direction items flow; align-items aligns them in the opposite direction.

What is the difference between flex-grow, flex-shrink, and flex-basis?

These three properties control how a flex item sizes itself relative to the available space in the container. flex-basis sets the starting size of the item before any growing or shrinking takes place - think of it as the item's ideal or default size. flex-grow determines how much the item will expand to fill extra space, relative to other flex items. A value of 2 means the item will take twice as much extra space as items with a value of 1. flex-shrink determines how much the item will contract when there is not enough space. A higher value means the item shrinks faster than others. All three can be set together using the shorthand flex property, for example flex: 1 1 auto.

What is the difference between align-items and align-content?

align-items aligns flex items within a single row (or column) along the cross axis. It works even when there is only one line of flex items. align-content only has an effect when the flex container has multiple lines of items - that is, when flex-wrap is set to wrap or wrap-reverse and items have wrapped onto more than one row. It controls the spacing and alignment of those rows as a group within the container. If all your flex items fit on a single line, align-content will have no visible effect.

What does flex-direction do?

flex-direction sets the main axis of the flex container - the direction in which flex items are placed. row (the default) places items horizontally left to right. row-reverse places them horizontally right to left. column stacks items vertically top to bottom. column-reverse stacks them vertically bottom to top. Changing flex-direction also swaps which axis justify-content and align-items apply to, since those properties are always relative to the main and cross axes rather than fixed to horizontal or vertical.

What does flex-wrap do and when should I use it?

By default, flex items all try to fit onto a single line, shrinking if necessary. flex-wrap: wrap allows items to flow onto multiple lines when they would otherwise overflow the container. wrap-reverse does the same but stacks the new lines above (for row containers) or to the left (for column containers) rather than below or to the right. You should use flex-wrap: wrap any time you want a responsive layout where items naturally reflow into rows as the container gets narrower - for example, a grid of cards or thumbnails.

How do I centre an element horizontally and vertically with Flexbox?

Apply display: flex, justify-content: center, and align-items: center to the parent container. This centres the child element along both the main axis and the cross axis simultaneously:

.container { display: flex; justify-content: center; align-items: center; }

This works regardless of the size of the child element and is one of the most reliable centring techniques in CSS. If the container does not have a defined height, make sure to set one - otherwise there is no cross-axis space to centre within.

What is the difference between display: flex and display: inline-flex?

Both values turn an element into a flex container and give its children flex behaviour. The difference is how the container itself behaves in relation to surrounding content. display: flex makes the container a block-level element - it takes up the full width available and starts on a new line, like a div. display: inline-flex makes the container inline - it only takes up as much width as its content requires and flows alongside other inline elements, like a span. Use inline-flex when you need the flex container to sit within a line of text or beside other inline content.

What does the order property do in Flexbox?

The order property controls the visual order in which a flex item appears within its container, independent of its position in the HTML source. All flex items default to an order value of 0. Items with a lower value appear first; items with a higher value appear later. Negative values are allowed and will place items before those with the default value of 0. This is useful for changing visual layout at different screen sizes via media queries without altering the HTML, though keep in mind it only affects visual order - screen readers and keyboard navigation still follow the source order.

What is align-self and how does it differ from align-items?

align-items is set on the flex container and applies the same cross-axis alignment to all flex items inside it. align-self is set on an individual flex item and overrides the container's align-items value for that specific item only. For example, if the container has align-items: center, you can give one particular item align-self: flex-end to push it to the end of the cross axis while all other items remain centred.

When should I use Flexbox vs CSS Grid?

Flexbox is a one-dimensional layout system - it is best suited for arranging items in a single row or a single column, with control over how they grow, shrink, and align along that axis. It excels at things like navigation bars, button groups, centring content, and distributing items along one direction. CSS Grid is a two-dimensional layout system - it lets you place items across both rows and columns simultaneously. It is better suited for overall page layouts, card grids, and any design where you need precise control over placement in both directions at once. In practice, the two complement each other well. A common pattern is to use Grid for the overall page structure and Flexbox for the components within it.

Why are my flex items not wrapping?

By default, flex-wrap is set to nowrap, which forces all items onto a single line regardless of their size. To allow items to wrap onto multiple lines, add flex-wrap: wrap to the flex container. If items are still not wrapping after setting this, check whether a flex-shrink value is causing items to compress rather than wrap, or whether the items have an explicit width that is smaller than you expect.

Properties Used

display
Defines how a particular HTML element should be displayed.
flex-direction
Indicates the direction in which the flex elements are located inside the container.
flex-wrap
Determines whether the flex container will be single-line or multi-line. If multi-line is enabled in the container, this property also allows you to control the direction in which the flex elements are placed.
justify-content
Determines how the browser distributes the space between and around the flex elements along the main axis of the container (horizontally), or aligns the entire grid layout along the axis of the grid container row.
align-items
Aligns all the elements inside the flex container along the transverse axis, or aligns all elements of the grid layout along the axis of the column.
align-content
Determines how the browser distributes the space between and around the flex elements along the transverse axis of the container (vertically), or aligns the entire grid layout along the axis of the grid container column.
flex-grow
Indicates how much an element can increase in relation to other flex elements in one container.
flex-shrink
Determines how the element will be compressed relative to other flex elements in the container (with a lack of free space).
flex-basis
Defines the default size for the flex element before allocating space in the container.
align-self
Specifies the alignment of individual row elements inside a flex container, or aligns a grid layout element inside a cell along the column axis of a grid container.
order
Sets the order of the flex element in the container relative to the rest.
If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!