Skip to the main content

All tutorials

Centring and aligning with Flexbox

  • Level: Beginner
  • About 4 minutes to read (an estimate from the word count: 751 words at 200 words a minute)
  • Last checked:

Centre one thing, space several things, and push one item to the far end, with the three properties that do it.

What you will be able to do

  • Centre one item with justify-content and align-items
  • Explain which property works along the row and which across it
  • Space items with gap instead of margins
  • Push one item to the far end with an auto margin
  • Choose Grid's place-items when it is shorter

One item in the middle

To centre one item in a box, make the box a flex container and centre on both axes. Three properties do the whole job: display: flex, justify-content: center and align-items: center. The box also needs a height that is larger than the item, because otherwise there is no free space to centre in.

.stage {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 9rem;
}
The item is centred across the width and the height of .stage.

The same three lines centre text inside a badge or a button, and they centre a picture inside a frame. The item can be one child element or the text itself. If the box has no extra height, the item is centred but you cannot see it, because there is no space around it.

justify-content and align-items

A flex container has a main axis and a cross axis. By default the main axis runs along the row, left to right in English, and the cross axis runs down. justify-content places the items along the main axis. align-items places them along the cross axis. That is why one property centres across and the other centres up and down.

If you set flex-direction: column, the axes swap. The main axis then runs down, so justify-content works vertically and align-items works horizontally. When a centring rule does the wrong thing, check the direction first.

justify-content takes more values than center. The ones you will use most are flex-start, flex-end, space-between and space-around. The values of align-items that matter most are flex-start, flex-end, center and stretch. Items stretch to the height of the line when you set nothing.

HTML

<div class="flex-one">
  <section class="flex-one__demo" aria-labelledby="flex-one-flex">
    <h2 class="flex-one__label" id="flex-one-flex">Flexbox</h2>
    <div class="flex-one__stage flex-one__stage--flex">
      <p class="flex-one__item">Centred</p>
    </div>
  </section>
  <section class="flex-one__demo" aria-labelledby="flex-one-grid">
    <h2 class="flex-one__label" id="flex-one-grid">Grid</h2>
    <div class="flex-one__stage flex-one__stage--grid">
      <p class="flex-one__item">Centred</p>
    </div>
  </section>
</div>

CSS

.flex-one {
  --fo-bg: #ffffff;
  --fo-fg: #1b1f2a;
  --fo-line: #6b7280;
  --fo-item: #e4e6fb;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 14rem), 1fr));
  gap: 1rem;
  padding: 1rem;
  color: var(--fo-fg);
  background: var(--fo-bg);
}
.flex-one__label { margin: 0 0 0.5rem; font-size: 1rem; }
.flex-one__stage {
  min-height: 9rem;
  border: 2px dashed var(--fo-line);
  border-radius: 8px;
}
.flex-one__stage--flex {
  display: flex;
  justify-content: center;   /* along the main axis (a row by default) */
  align-items: center;       /* across it */
}
.flex-one__stage--grid {
  display: grid;
  place-items: center;       /* shorthand for align-items and justify-items */
}
.flex-one__item { margin: 0; padding: 0.75rem 1.25rem; background: var(--fo-item); border-radius: 6px; }
@media (prefers-color-scheme: dark) {
  .flex-one {
    --fo-bg: #12141c;
    --fo-fg: #eceef5;
    --fo-line: #8b93a7;
    --fo-item: #2a2f45;
  }
}

Space with gap

To put space between items, use gap on the container. It adds space only between items, never before the first one or after the last one. Margins on the items need extra rules to cancel the edge cases, and gap needs none. Two values set the row gap and then the column gap: gap: 0.5rem 0.75rem. The same property works on a grid, so you can learn it once and use it in both layouts.

The far end with an auto margin

Flexbox has no justify-self, so you cannot ask one item to go to the far end. The way to do it is an auto margin. An auto margin takes all the free space on its side. Put margin-inline-start: auto on the item that should sit at the far end, and everything before it stays at the start. To split a row into two groups, put the auto margin on the first item of the second group. The free space then sits between the groups.

The logical property margin-inline-start means the left margin in English and the right margin in a language that is written right to left, so the layout follows the writing direction.

.toolbar { display: flex; gap: 0.5rem; }
.toolbar .far { margin-inline-start: auto; }
The Delete button moves to the far end. The other buttons stay together at the start.

HTML

<div class="bar-demo">
  <div class="bar-demo__bar" role="toolbar" aria-label="Document actions">
    <p class="bar-demo__title">Draft notes</p>
    <button type="button" class="bar-demo__button">Save</button>
    <button type="button" class="bar-demo__button">Share</button>
    <button type="button" class="bar-demo__button bar-demo__button--far">Delete</button>
  </div>
</div>

CSS

.bar-demo {
  --bd-bg: #ffffff;
  --bd-fg: #1b1f2a;
  --bd-line: #6b7280;
  --bd-accent: #3730a3;
  --bd-on-accent: #ffffff;
  --bd-bad: #b42318;
  padding: 1rem;
  color: var(--bd-fg);
  background: var(--bd-bg);
}
.bar-demo__bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--bd-line);
  border-radius: 8px;
}
.bar-demo__title { margin: 0; margin-inline-end: 0.5rem; font-weight: 700; }
.bar-demo__button {
  min-height: 44px;
  padding: 0.5rem 1rem;
  font: inherit;
  color: var(--bd-on-accent);
  background: var(--bd-accent);
  border: 1px solid var(--bd-accent);
  border-radius: 6px;
  cursor: pointer;
}
/* An auto margin takes all the free space on its side, so this one item moves to the far end. */
.bar-demo__button--far {
  margin-inline-start: auto;
  background: var(--bd-bad);
  border-color: var(--bd-bad);
}
.bar-demo__button:focus-visible {
  outline: 3px solid var(--bd-fg);
  outline-offset: 2px;
}
@media (prefers-color-scheme: dark) {
  .bar-demo {
    --bd-bg: #12141c;
    --bd-fg: #eceef5;
    --bd-line: #8b93a7;
    --bd-accent: #b4b8ff;
    --bd-on-accent: #12141c;
    --bd-bad: #ff8a80;
  }
}

Wrapping

A flex container keeps all its items on one line unless you allow wrapping. On a narrow screen that makes the items shrink and then overflow. Add flex-wrap: wrap, and an item that does not fit moves to a new line. Combined with gap, you get a row of tags or buttons that wraps cleanly, with even space between the lines and between the items.

HTML

<section class="wrap-gap" aria-labelledby="wrap-gap-title">
  <h2 class="wrap-gap__title" id="wrap-gap-title">Topics</h2>
  <ul class="wrap-gap__list">
    <li class="wrap-gap__chip">Landmarks</li>
    <li class="wrap-gap__chip">Skip links</li>
    <li class="wrap-gap__chip">Focus</li>
    <li class="wrap-gap__chip">Dialogs</li>
    <li class="wrap-gap__chip">Validation</li>
    <li class="wrap-gap__chip">Grid</li>
    <li class="wrap-gap__chip">Flexbox</li>
    <li class="wrap-gap__chip">Custom properties</li>
    <li class="wrap-gap__chip">Transitions</li>
  </ul>
</section>

CSS

.wrap-gap {
  --wg-bg: #ffffff;
  --wg-fg: #1b1f2a;
  --wg-line: #6b7280;
  --wg-chip: #e4e6fb;
  padding: 1rem;
  color: var(--wg-fg);
  background: var(--wg-bg);
}
.wrap-gap__title { margin: 0 0 0.75rem; font-size: 1.25rem; }
.wrap-gap__list {
  display: flex;
  flex-wrap: wrap;            /* without this, the items shrink and then overflow */
  gap: 0.5rem 0.75rem;        /* row gap, then column gap; no margins to cancel at the edges */
  margin: 0;
  padding: 0;
  list-style: none;
}
.wrap-gap__chip {
  padding: 0.375rem 0.875rem;
  background: var(--wg-chip);
  border: 1px solid var(--wg-line);
  border-radius: 999px;
}
@media (prefers-color-scheme: dark) {
  .wrap-gap {
    --wg-bg: #12141c;
    --wg-fg: #eceef5;
    --wg-line: #8b93a7;
    --wg-chip: #2a2f45;
  }
}

When Grid is shorter

When you only want to centre, a grid container is shorter. place-items: center is a shorthand for align-items and justify-items. On a grid container, it centres the item on both axes with one line. The first example above shows the two versions side by side.

If you only need to centre a block across the page, and not up and down, a width and margin-inline: auto is enough. It centres a block in its container along the inline axis and needs no flex container.

Use Flexbox when you lay items out in a line and want space shared among them. Use Grid when you place items in rows and columns, or when you only need to centre.

All examples on this page passed their checks in Chrome 143 on 3 December 2026. They were not tested in other browsers.

References

These snippets in the gallery use the same techniques: