CSS Flexbox Layout Guide

Flexbox is a one-dimensional layout method for arranging items in rows or columns. It offers powerful alignment capabilities and distribution of space between items.

Container Properties

display: flex

The fundamental property that enables flex layout:

Item 1
Item 2
Item 3
.container {
  display: flex;
}

flex-direction

Defines the main axis direction:

Item 1
Item 2
Item 3
.flex-direction-row {
  flex-direction: row; /* Default */
}

.flex-direction-column {
  flex-direction: column;
}

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

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

flex-wrap

Controls whether flex items wrap onto multiple lines:

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
.flex-nowrap {
  flex-wrap: nowrap; /* Default */
}

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

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

justify-content

Aligns flex items along the main axis:

Item 1
Item 2
Item 3
.justify-content-start {
  justify-content: flex-start; /* Default */
}

.justify-content-center {
  justify-content: center;
}

.justify-content-end {
  justify-content: flex-end;
}

.justify-content-between {
  justify-content: space-between;
}

.justify-content-around {
  justify-content: space-around;
}

.justify-content-evenly {
  justify-content: space-evenly;
}

align-items

Aligns flex items along the cross axis:

Item 1
Taller Item
Item 3
.align-items-stretch {
  align-items: stretch; /* Default */
}

.align-items-start {
  align-items: flex-start;
}

.align-items-center {
  align-items: center;
}

.align-items-end {
  align-items: flex-end;
}

.align-items-baseline {
  align-items: baseline;
}

align-content

Aligns flex lines within the container when there's extra space in the cross axis:

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
.align-content-start {
  align-content: flex-start;
  height: 200px;
  flex-wrap: wrap;
}

.align-content-center {
  align-content: center;
  height: 200px;
  flex-wrap: wrap;
}

.align-content-end {
  align-content: flex-end;
  height: 200px;
  flex-wrap: wrap;
}

.align-content-between {
  align-content: space-between;
  height: 200px;
  flex-wrap: wrap;
}

.align-content-around {
  align-content: space-around;
  height: 200px;
  flex-wrap: wrap;
}

Item Properties

flex-grow

Defines the ability for a flex item to grow if necessary:

grow: 0
grow: 1
grow: 2
.flex-grow-0 {
  flex-grow: 0; /* Default */
}

.flex-grow-1 {
  flex-grow: 1;
}

.flex-grow-2 {
  flex-grow: 2; /* Grows twice as much as flex-grow: 1 */
}

flex-shrink

Defines the ability for a flex item to shrink if necessary:

shrink: 0
shrink: 1
shrink: 2
.flex-shrink-0 {
  flex-shrink: 0;
  width: 150px; /* Won't shrink */
}

.flex-shrink-1 {
  flex-shrink: 1; /* Default */
  width: 150px;
}

.flex-shrink-2 {
  flex-shrink: 2; /* Shrinks twice as much */
  width: 150px;
}

flex-basis

Defines the default size of an element before the remaining space is distributed:

auto
100px
200px
.flex-basis-auto {
  flex-basis: auto; /* Default */
}

.flex-basis-100 {
  flex-basis: 100px;
}

.flex-basis-200 {
  flex-basis: 200px;
}

flex (shorthand)

Shorthand for flex-grow, flex-shrink, and flex-basis:

.item {
  flex: 0 1 auto; /* Default (flex-grow flex-shrink flex-basis) */
}

.item-grow {
  flex: 1; /* Same as flex: 1 1 0% */
}

.item-fixed {
  flex: 0 0 200px; /* Fixed width, won't grow or shrink */
}

.item-flexible {
  flex: 1 0 100px; /* Grow, don't shrink, start at 100px */
}

order

Controls the order in which flex items appear:

1 → order: 3
2 → order: 1
3 → order: 2
.order-demo .flex-item:nth-child(1) {
  order: 3; /* Default is 0 */
}

.order-demo .flex-item:nth-child(2) {
  order: 1;
}

.order-demo .flex-item:nth-child(3) {
  order: 2;
}

align-self

Allows the alignment to be overridden for individual flex items:

start
center
end
stretch
.item-start {
  align-self: flex-start;
}

.item-center {
  align-self: center; /* Inherits from container */
}

.item-end {
  align-self: flex-end;
}

.item-stretch {
  align-self: stretch;
  height: auto; /* Allow stretching */
}

Common Flexbox Layouts

Navigation Bar

.navbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #2c3e50;
  padding: 15px;
}

.nav-items {
  display: flex;
  gap: 20px;
}

Card Layout

Image

Card 1

This is card content with flexible sizing.

Image

Card 2

Cards grow and shrink to fit available space.

Image

Card 3

Using flexbox for evenly distributed cards.

.card-container {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

.card {
  flex: 1 1 200px; /* grow shrink basis */
  display: flex;
  flex-direction: column;
}

.card-content {
  flex-grow: 1; /* Takes available space */
}

Holy Grail Layout

Header
Navigation

Main Content

This is a classic web layout with header, footer, and three columns in the middle.

Flexbox makes this much easier than older methods.

Sidebar
.holy-grail {
  display: flex;
  flex-direction: column;
  min-height: 400px;
}

.hg-main {
  display: flex;
  flex: 1;
}

.hg-content {
  flex: 1;
}

/* Responsive */
@media (max-width: 768px) {
  .hg-main {
    flex-direction: column;
  }
}

Browser Support

Flexbox is well-supported in all modern browsers. Internet Explorer 11 has partial support with some bugs.

Tips and Best Practices