Beginner12 min read

CSS Flexbox: Modern Layout Made Simple

12 min read
368 words
19 sections16 code blocks

What is Flexbox?

Flexbox (Flexible Box Layout) is a CSS layout system designed to distribute space and align items within a container. It makes building complex layouts much simpler than older methods like floats or inline-block.

Flexbox works in one dimension at a time: either as a row (horizontal) or a column (vertical).

Creating a Flex Container

To use Flexbox, set `display: flex` on the parent element. All direct children automatically become flex items.

CSS
.container {
  display: flex;
}

.item {
  padding: 20px;
  background-color: #3498db;
  color: white;
  margin: 5px;
}
HTML
<div class="container">
  <div class="item">Item 1</div>
  <div class="item">Item 2</div>
  <div class="item">Item 3</div>
</div>

By default, flex items will sit side by side in a row and only take up the space they need.

Flex Direction

The `flex-direction` property controls the main axis, which determines the direction flex items are placed.

CSS
/* Default: items in a row (left to right) */
.row {
  display: flex;
  flex-direction: row;
}

/* Items in a row, reversed (right to left) */
.row-reverse {
  display: flex;
  flex-direction: row-reverse;
}

/* Items stacked vertically (top to bottom) */
.column {
  display: flex;
  flex-direction: column;
}

/* Items stacked vertically, reversed (bottom to top) */
.column-reverse {
  display: flex;
  flex-direction: column-reverse;
}

Justify Content (Main Axis Alignment)

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

CSS
.container {
  display: flex;
  justify-content: flex-start;    /* Default: items packed to start */
}

.center {
  display: flex;
  justify-content: center;        /* Items centered */
}

.end {
  display: flex;
  justify-content: flex-end;      /* Items packed to end */
}

.space-between {
  display: flex;
  justify-content: space-between; /* Equal space BETWEEN items */
}

.space-around {
  display: flex;
  justify-content: space-around;  /* Equal space AROUND items */
}

.space-evenly {
  display: flex;
  justify-content: space-evenly;  /* Equal space between and on edges */
}

Align Items (Cross Axis Alignment)

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

CSS
.container {
  display: flex;
  height: 300px;
  align-items: stretch;     /* Default: items stretch to fill container */
}

.center {
  display: flex;
  align-items: center;      /* Items centered vertically */
}

.start {
  display: flex;
  align-items: flex-start;  /* Items aligned to top */
}

.end {
  display: flex;
  align-items: flex-end;    /* Items aligned to bottom */
}

.baseline {
  display: flex;
  align-items: baseline;    /* Items aligned by text baseline */
}

Perfect Centering

One of the most powerful uses of Flexbox is centering content both horizontally and vertically:

CSS
.center-everything {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
}

This three-line pattern solves a problem that was notoriously difficult with older CSS methods.

Flex Wrap

By default, flex items try to fit on one line. The `flex-wrap` property controls whether items wrap to new lines.

CSS
/* Default: all items on one line */
.no-wrap {
  display: flex;
  flex-wrap: nowrap;
}

/* Items wrap to next line when needed */
.wrap {
  display: flex;
  flex-wrap: wrap;
}

/* Practical example: responsive card grid */
.card-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

.card {
  flex: 1 1 300px;  /* Grow, shrink, min 300px wide */
  padding: 20px;
  background-color: white;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

Gap

The `gap` property adds spacing between flex items without using margins.

CSS
.container {
  display: flex;
  gap: 20px;            /* Equal gap in all directions */
}

.container-2 {
  display: flex;
  gap: 20px 10px;       /* row-gap column-gap */
}

/* Replaces the old margin hack */
.old-way .item {
  margin-right: 20px;
}
.old-way .item:last-child {
  margin-right: 0;
}

/* New way - much cleaner */
.new-way {
  display: flex;
  gap: 20px;
}

Flex Item Properties

Flex Grow

The `flex-grow` property controls how much a flex item should grow relative to other items when there is extra space.

CSS
.item {
  flex-grow: 0;  /* Default: don't grow */
}

.item-grow {
  flex-grow: 1;  /* Take up available space equally */
}

/* Sidebar + main content layout */
.sidebar {
  flex-grow: 0;
  width: 250px;
}

.main {
  flex-grow: 1;  /* Takes remaining space */
}

Flex Shrink

The `flex-shrink` property controls how much an item should shrink when there is not enough space.

CSS
.item {
  flex-shrink: 1;  /* Default: items can shrink */
}

.no-shrink {
  flex-shrink: 0;  /* This item will NOT shrink */
}

Flex Basis

The `flex-basis` property sets the initial size of a flex item before growing or shrinking.

CSS
.item {
  flex-basis: 200px;  /* Start at 200px, then grow or shrink */
}

.item-auto {
  flex-basis: auto;   /* Default: use width/height or content size */
}

Flex Shorthand

The `flex` shorthand combines grow, shrink, and basis.

CSS
.item {
  flex: 0 1 auto;     /* Default: don't grow, can shrink, auto basis */
}

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

.fixed-item {
  flex: 0 0 300px;    /* Don't grow, don't shrink, fixed at 300px */
}

/* Common patterns */
.equal-columns {
  flex: 1;            /* All items share space equally */
}

.double-width {
  flex: 2;            /* Gets twice the space of flex: 1 items */
}

Practical Flexbox Layouts

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

.nav-brand {
  font-size: 1.5rem;
  color: white;
  font-weight: bold;
}

.nav-links {
  display: flex;
  gap: 15px;
  list-style: none;
}

.nav-links a {
  color: #ecf0f1;
  text-decoration: none;
  padding: 8px 12px;
}
CSS
.page-layout {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  flex: 0 0 250px;  /* Fixed 250px sidebar */
  background-color: #f5f5f5;
  padding: 20px;
}

.content {
  flex: 1;           /* Takes remaining space */
  padding: 20px;
}
CSS
.footer {
  display: flex;
  flex-wrap: wrap;
  gap: 30px;
  padding: 40px 20px;
  background-color: #2c3e50;
  color: #ecf0f1;
}

.footer-column {
  flex: 1 1 200px;
}

.footer-column h3 {
  margin-bottom: 15px;
  color: white;
}

Card Grid

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

.card {
  flex: 1 1 calc(33.333% - 24px);
  min-width: 280px;
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

Summary

Flexbox is a powerful and intuitive layout system for one-dimensional layouts. Use `justify-content` to align items on the main axis, `align-items` on the cross axis, and the `flex` shorthand to control how items grow and shrink. With `flex-wrap` and `gap`, you can create responsive grids easily. Flexbox has replaced floats and inline-block for most modern layouts.

In the next article, you will learn about CSS Grid for two-dimensional layouts.