Flexbox

W3C CSS Snapshot 2026

Flexbox lays out a group of items along one main axis. Set display: flex on the container. Then choose the direction, wrapping and alignment on that container, or change sizing and alignment on an individual item.

On this page

Flex container properties

  • display

    Create the flex container on the parent. Its direct children become flex items.

  • flex-direction

    Choose the direction of the main axis. Direction follows the writing mode.

  • flex-wrap

    Allow the container to start new flex lines when items do not fit.

  • flex-flow

    Set flex-direction and flex-wrap in one declaration. The keywords can appear in either order.

  • justify-content

    Distribute remaining space along the main axis after flexible sizes and auto margins have been resolved.

  • align-items

    Set the default cross-axis alignment for the items in each flex line.

  • align-content

    Align flex lines along the cross axis. Use wrapping and leave cross-axis space to see the result.

  • gap

    Add spacing between items and between flex lines. The shorthand sets row-gap followed by column-gap.

  • row-gap

    Set the row gap. In a row flex container this is the distance between flex lines. In a column flex container it separates items within a line.

  • column-gap

    Set the column gap. In a row flex container this separates items within a line. In a column flex container it separates wrapped lines.

Flex item properties

  • flex-grow

    Set how much an item takes from positive free space. The factor is a ratio, not a width.

  • flex-shrink

    Set how much an item can shrink when the combined starting sizes exceed the space. The browser weights the factor by the flex base size.

  • flex-basis

    Set the starting size along the main axis before the item grows or shrinks.

  • flex

    Set flex-grow, flex-shrink, and flex-basis together on one item.

  • align-self

    Override the container’s cross-axis alignment for one item.

  • order

    Change the visual arrangement of an item. Keep the HTML order meaningful for keyboard and screen reader users.

Choose the direction of the main axis

The main axis is the direction items follow. flex-direction: row follows the horizontal writing direction in this example. column places items vertically. The cross axis goes across the main axis.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Choose the direction of the main axis</title>
  <style>
    .cards { display: flex; flex-direction: row; gap: 12px; }
    .cards > div { background: #d6efea; padding: 8px; }
  </style>
</head>
<body>
<div class="cards"><div>Pen</div><div>Notebook</div><div>Folder</div></div>
</body>
</html>

What to expect Three items appear in a row. Change row to column to stack them.

Align items on each axis

justify-content uses extra space along the main axis. align-items aligns items across it. With a row direction here, those are horizontal and vertical respectively.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Align items on each axis</title>
  <style>
    .cards { display: flex; min-height: 100px; justify-content: space-between; align-items: center; border: 2px solid; }
    .cards > div { background: #d6efea; padding: 8px; }
  </style>
</head>
<body>
<div class="cards"><div>Pen</div><div>Notebook</div><div>Folder</div></div>
</body>
</html>

What to expect Items spread across the row and sit at the vertical center.

Center a flex item on both axes

Set both properties on the parent container. In this row, justify-content: center centers the label horizontally, and align-items: center centers it vertically. The minimum height leaves room above and below the label.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Center a flex item on both axes</title>
  <style>
    .panel {
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 160px;
      border: 2px dashed teal;
    }
    .label { padding: 8px 12px; background: #d6efea; color: #123; }
  </style>
</head>
<body>
<div class="panel"><span class="label">Ready</span></div>
</body>
</html>

What to expect Ready appears in the middle of the panel. Change justify-content to flex-start and run again. The label moves to the left and stays vertically centered.

Wrap items when a row runs out of room

Flexbox uses one line by default. flex-wrap: wrap allows more lines. A flex item can get a starting size with flex-basis; here each uses 100px. gap separates items and the lines when they wrap.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Wrap items when a row runs out of room</title>
  <style>
    .cards { display: flex; flex-wrap: wrap; width: 240px; gap: 10px; }
    .cards > div { flex-basis: 100px; background: #d6efea; }
  </style>
</head>
<body>
<div class="cards"><div>Pen</div><div>Notebook</div><div>Folder</div></div>
</body>
</html>

What to expect Pen and Notebook fit on line one. Folder moves to line two.

Related lessons

Official references