Flex Direction

The flex-direction property defines the main axis direction. Items are laid out along this axis.

flex-direction: row
1
2
3

Default. Items are placed left to right in a horizontal row.

flex-direction: row-reverse
1
2
3

Items are placed right to left in reverse order.

flex-direction: column
1
2
3

Items are placed top to bottom in a vertical column.

flex-direction: column-reverse
1
2
3

Items are placed bottom to top in reverse order.

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

Justify Content

The justify-content property aligns items along the main axis (horizontal for row, vertical for column).

justify-content: flex-start
1
2
3

Items packed at the start of the main axis.

justify-content: center
1
2
3

Items centered along the main axis.

justify-content: flex-end
1
2
3

Items packed at the end of the main axis.

justify-content: space-between
1
2
3

Equal space between items, no space at edges.

justify-content: space-around
1
2
3

Equal space around items (half-size at edges).

justify-content: space-evenly
1
2
3

Equal space between and around all items.

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

Align Items

The align-items property aligns items along the cross axis (vertical for row, horizontal for column).

align-items: stretch
1
2
3

Default. Items stretch to fill the container height.

align-items: flex-start
1
2
3

Items aligned to the top of the container.

align-items: center
1
2
3

Items centered vertically in the container.

align-items: flex-end
1
2
3

Items aligned to the bottom of the container.

align-items: baseline
Big
Small
Med

Items aligned by their text baseline.

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

Flex Wrap

The flex-wrap property controls whether items wrap to new lines when they don't fit in the container.

flex-wrap: nowrap
Item 1
Item 2
Item 3
Item 4

Default. Items shrink to fit on one line.

flex-wrap: wrap
Item 1
Item 2
Item 3
Item 4

Items wrap to new lines when needed.

flex-wrap: wrap-reverse
Item 1
Item 2
Item 3
Item 4

Items wrap in reverse, new lines appear above.

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

Gap

The gap property adds space between flex items without affecting the outer edges. You can also use row-gap and column-gap.

gap: 0.5rem
1
2
3

Small gap between all items.

gap: 1rem
1
2
3

Medium gap between all items.

gap: 2rem
1
2
3

Large gap between all items.

.gap-sm { gap: 0.5rem; }
.gap-md { gap: 1rem; }
.gap-lg { gap: 1.5rem; }
.gap-xl { gap: 2rem; }
.gap-row-col { row-gap: 1rem; column-gap: 2rem; }

Flex grow, shrink, basis — and flex: 1 in CSS

Half the Stack Overflow answers just say “use flex: 1” Here is what that actually expands to: flex-grow, flex-shrink, and flex-basis in one line. Tweak those pieces when equal columns are not quite what you need.

flex-grow: 1 (all items)
1
1
1

All items grow equally to fill available space.

flex-grow: 2 (middle item)
1
2
1

Middle item grows twice as much as others.

flex-shrink: 0
Fixed
Grows

Item won't shrink below its content/basis size.

flex-basis: 200px
100px
200px

Set the initial size before growing/shrinking.

.grow { flex-grow: 1; }
.grow-double { flex-grow: 2; }
.no-shrink { flex-shrink: 0; }
.basis { flex-basis: 200px; }
/* Shorthand: flex: grow shrink basis */
.flex-1 { flex: 1 1 0%; }
.flex-auto { flex: 1 1 auto; }
.flex-none { flex: 0 0 auto; }

Common Flexbox Patterns

Practical flexbox layouts you can use in real projects.

Perfect Centering
Centered!

Center any element both horizontally and vertically.

Navigation Bar
Logo

Logo on left, links on right with space-between.

Equal Width Cards
Card
Card
Card

Cards with flex: 1 share space equally.

Footer Columns

Responsive footer that wraps on smaller screens.

/* Perfect centering */
.center { display: flex; justify-content: center; align-items: center; }

/* Navigation */
.nav { display: flex; justify-content: space-between; align-items: center; }

/* Equal cards */
.cards { display: flex; gap: 1rem; }
.card { flex: 1; }

/* Footer columns */
.footer { display: flex; flex-wrap: wrap; gap: 2rem; }
.footer-col { flex: 1; min-width: 150px; }