What is CSS Flexbox?

Flexbox (Flexible Box Layout) is a modern CSS layout model for distributing space and aligning items in a container, even when their size is unknown or dynamic. Flexbox is one-dimensional (row or column), making it ideal for navigation bars, cards, toolbars, and responsive layouts.

Flex Container Properties

Flex Item Properties

Axes Visualization

Flexbox main axis and cross axis diagram

Tip: The main axis is controlled by flex-direction. The cross axis is perpendicular to the main axis.

Interactive Examples

Flex Direction (flex-direction)

Controls the direction of the main axis. Try changing the direction to see how items flow.

row
row
row
row-reverse
row-reverse
row-reverse
column
column
column
column-reverse
column-reverse
column-reverse
.flex-row { display: flex; flex-direction: row; }
.flex-row-reverse { display: flex; flex-direction: row-reverse; }
.flex-column { display: flex; flex-direction: column; }
.flex-column-reverse { display: flex; flex-direction: column-reverse; }

Justify Content (justify-content)

Aligns items along the main axis. Useful for horizontal alignment in rows or vertical in columns.

flex-start
flex-start
flex-start
center
center
center
flex-end
flex-end
flex-end
space-between
space-between
space-between
space-around
space-around
space-around
space-evenly
space-evenly
space-evenly
.flex-justify-start { justify-content: flex-start; }
.flex-justify-center { justify-content: center; }
.flex-justify-end { justify-content: flex-end; }
.flex-justify-between { justify-content: space-between; }
.flex-justify-around { justify-content: space-around; }
.flex-justify-evenly { justify-content: space-evenly; }

Align Items (align-items)

Aligns items along the cross axis. Try different values to see how items align vertically (in a row) or horizontally (in a column).

flex-start
flex-start
flex-start
center
center
center
flex-end
flex-end
flex-end
stretch
stretch
stretch
baseline
baseline
baseline
.flex-align-start { align-items: flex-start; }
.flex-align-center { align-items: center; }
.flex-align-end { align-items: flex-end; }
.flex-align-stretch { align-items: stretch; }
.flex-align-baseline { align-items: baseline; }

Flex Wrap (flex-wrap)

Controls whether flex items wrap onto multiple lines. Useful for responsive layouts.

nowrap
nowrap
nowrap
nowrap
nowrap
wrap
wrap
wrap
wrap
wrap
wrap-reverse
wrap-reverse
wrap-reverse
wrap-reverse
wrap-reverse
.flex-wrap-nowrap { flex-wrap: nowrap; }
.flex-wrap-wrap { flex-wrap: wrap; }
.flex-wrap-wrap-reverse { flex-wrap: wrap-reverse; }

Gap (gap)

Sets the spacing between flex items. Preferred over using margins for consistent spacing.

A
B
C
.flex-gap { gap: 24px; }

Flex Grow, Shrink, Basis, Flex (flex-grow, flex-shrink, flex-basis, flex)

Control how flex items grow, shrink, and their initial size. flex is shorthand for all three.

flex: 1
flex: 2
flex: 3
shrink: 1
shrink: 3
shrink: 5
basis: 100px
basis: 200px
basis: 50px
.grow1 { flex: 1; }
.grow2 { flex: 2; }
.grow3 { flex: 3; }
.flex-shrink-demo .flex-item:nth-child(1) { flex-shrink: 1; width: 200px; }
.flex-shrink-demo .flex-item:nth-child(2) { flex-shrink: 3; width: 200px; }
.flex-shrink-demo .flex-item:nth-child(3) { flex-shrink: 5; width: 200px; }
.flex-basis-demo .flex-item:nth-child(1) { flex-basis: 100px; }
.flex-basis-demo .flex-item:nth-child(2) { flex-basis: 200px; }
.flex-basis-demo .flex-item:nth-child(3) { flex-basis: 50px; }

Align Self (align-self)

Allows a single flex item to override the align-items value for itself.

flex-start
center
flex-end
stretch
.flex-align-self .flex-item[style] { align-self: flex-start/center/flex-end/stretch; }
.flex-align-self .flex-item[style*="center"] { align-self: center; }
.flex-align-self .flex-item[style*="flex-end"] { align-self: flex-end; }
.flex-align-self .flex-item[style*="stretch"] { align-self: stretch; }

Order (order)

Controls the order in which flex items appear in the flex container, regardless of their source order.

order: 1
order: 2
order: 3
.order1 { order: 3; }
.order2 { order: 1; }
.order3 { order: 2; }

Align Content (align-content)

Aligns multiple lines of flex items along the cross axis (only works if items wrap onto multiple lines).

A
B
C
D
E
F
A
B
C
D
E
F
A
B
C
D
E
F
A
B
C
D
E
F
A
B
C
D
E
F
A
B
C
D
E
F
.flex-align-content-start { align-content: flex-start; }
.flex-align-content-center { align-content: center; }
.flex-align-content-end { align-content: flex-end; }
.flex-align-content-between { align-content: space-between; }
.flex-align-content-around { align-content: space-around; }
.flex-align-content-stretch { align-content: stretch; }

Flex Flow (flex-flow)

Shorthand for flex-direction and flex-wrap. Example: flex-flow: row wrap;

1
2
3
4
.flex-flow-demo { flex-flow: row wrap; gap: 12px; }

Best Practices & Accessibility

← Back to Home