HTML Formatterhtmlformatteronline.com

Centering recipes

Six ways to centre something, and which one to use when.

layoutcenteringflexboxgridcss
<div class="demos">

  <figure>
    <figcaption>1. Grid — both axes</figcaption>
    <div class="box grid-both"><span>Centred</span></div>
  </figure>

  <figure>
    <figcaption>2. Flex — both axes</figcaption>
    <div class="box flex-both"><span>Centred</span></div>
  </figure>

  <figure>
    <figcaption>3. Horizontally, known width</figcaption>
    <div class="box margin-auto"><span>Centred</span></div>
  </figure>

  <figure>
    <figcaption>4. Absolute, unknown size</figcaption>
    <div class="box abs"><span>Centred</span></div>
  </figure>

  <figure>
    <figcaption>5. Text and inline content</figcaption>
    <div class="box text"><span>Centred</span></div>
  </figure>

  <figure>
    <figcaption>6. One item pushed to the middle</figcaption>
    <div class="box flex-row"><i>left</i><span>Centred</span><i>right</i></div>
  </figure>

</div>

How it works

"How do I centre a div" is the most-asked question in CSS, and the honest answer is that it depends on which axis, whether you know the size, and whether the element is in flow.

1. place-items: center on a grid is the shortest correct answer for both axes. Two words. It works regardless of the child's size, and it works on any number of children.

2. Flexbox with align-items and justify-content gives the same result. Reach for it when the child also needs to grow, shrink or sit alongside siblings that do — flex controls the main axis in ways grid does not.

3. margin-inline: auto centres a block horizontally, and it needs a width on the child. A block element with no width fills its parent, so there is no leftover margin to distribute. This is the oldest recipe and still the right one for a centred page container.

4. Absolute plus translate(-50%, -50%) is for elements taken out of flow — a modal, a badge on an avatar, a close button. The translate handles the element's own size, which is why it works when you do not know the dimensions.

5. text-align: center centres inline content, not blocks. Matching line-height to the container height centres a single line vertically, which is exactly how button labels are centred. It breaks the moment the text wraps to two lines.

6. Absolute inside a flex row is how you centre one item against the container rather than against its siblings. With justify-content: space-between alone, the middle item is centred in the space left over, so it drifts as the side items change width. This is why a centred logo in a navbar moves when the menu length changes.

Accessibility notes

Centring is purely visual and changes nothing in the accessibility tree, with one caveat: absolute positioning takes an element out of flow visually but not in the DOM, so tab order follows the source. If you absolutely position something into a different visual position, check that tabbing through the page still moves in a sensible direction.

Avoid centring long paragraphs. Centred text has a ragged left edge, so the eye has to find the start of every line — measurably slower to read, and harder for people with dyslexia. Centre headlines and short leads; left-align body copy.

Making it yours

For a full-page centre, put display: grid; place-items: center; min-height: 100dvh on the body. dvh rather than vh so mobile address bars do not push the content off screen.

place-items is shorthand for align-items and justify-items. place-content is the equivalent for the whole track set, which is what you want when centring a grid of several items rather than each item in its own cell.

Related templates

Check your version

Once you have edited this, the HTML validator will catch any tag you left unclosed, and the formatter will tidy the indentation. Both run in your browser.