Flex Container

What is Flexbox?

Flexbox lays out items in one direction, a row or a column, and makes it easy to align them, space them out, and let them grow or shrink. Turn an element into a flex container with display: flex; its direct children become flex items.

flex-direction and gap

Example

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
  <style>
    .container { display: flex; gap: 10px; background: #f1f1f1; padding: 10px; margin-bottom: 10px; }
    .col { flex-direction: column; }
    .container > div { background: #04AA6D; color: white; padding: 10px 16px; font: 15px Arial; }
  </style>
</head>
<body>

<div class="container"><div>1</div><div>2</div><div>3</div></div>
<div class="container col"><div>1</div><div>2</div><div>3</div></div>

</body>
</html>

Result

justify-content (Main Axis)

Example

justify-content distributes items along the main axis (horizontal for a row):

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
  <style>
    .container { display: flex; background: #f1f1f1; padding: 6px; margin-bottom: 6px; }
    .container > div { background: #04AA6D; color: white; padding: 6px 12px; margin: 2px; font: 13px Arial; }
    .c1 { justify-content: flex-start; }
    .c2 { justify-content: center; }
    .c3 { justify-content: flex-end; }
    .c4 { justify-content: space-between; }
    .c5 { justify-content: space-around; }
    .c6 { justify-content: space-evenly; }
  </style>
</head>
<body>

<div class="container c1"><div>flex-start</div><div>2</div><div>3</div></div>
<div class="container c2"><div>center</div><div>2</div><div>3</div></div>
<div class="container c3"><div>flex-end</div><div>2</div><div>3</div></div>
<div class="container c4"><div>space-between</div><div>2</div><div>3</div></div>
<div class="container c5"><div>space-around</div><div>2</div><div>3</div></div>
<div class="container c6"><div>space-evenly</div><div>2</div><div>3</div></div>

</body>
</html>

Result

align-items (Cross Axis) and flex-wrap

Example

align-items aligns items on the cross axis; flex-wrap: wrap lets items move to a new line:

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
  <style>
    .container { display: flex; gap: 8px; height: 90px; background: #f1f1f1; margin-bottom: 8px; }
    .container > div { background: #04AA6D; color: white; padding: 8px; font: 13px Arial; }
    .center { align-items: center; }
    .wrap { flex-wrap: wrap; height: auto; padding: 6px; }
    .wrap > div { width: 110px; }
  </style>
</head>
<body>

<div class="container center"><div>centered</div><div>vertically</div></div>
<div class="container wrap"><div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div></div>

</body>
</html>

Result