← Back to HTML & CSS, page 2

html-css-0031 · Easy fresher

What are the different ways to centre an element horizontally and vertically in CSS?

Centring depends on the element. Text uses text-align: center, a block with a width uses margin-inline: auto, and for both axes grid with place-items: center is shortest for one child, while flexbox with justify-content and align-items suits several children. Absolute positioning with inset: 0 and margin: auto, or top and left 50 percent with translate, also works.

Full answer

There is no single way, because the right method depends on the element. These are the usual ones.

  • Text and inline content: text-align: center on the parent centres it horizontally.
  • A block with a set width: margin-inline: auto centres it horizontally.
  • Flexbox: display: flex; justify-content: center; align-items: center on the parent centres children both ways.
  • Grid: display: grid; place-items: center is the shortest form for one child.
  • Absolute positioning: position: absolute; inset: 0; margin: auto works for an element with known size, and top: 50%; left: 50%; transform: translate(-50%, -50%) works for unknown size.

The parent needs a height for vertical centring to be visible.

In Chrome the first four dots have their centre at exactly (100, 50) inside the 200 by 100 frame. In the fifth frame, margin: 40px auto 0 centres the dot horizontally only. Auto margins on a block do not centre vertically in normal flow, but they do inside a flex or grid container.

For new work, choose grid place-items: center for a single child, or flexbox when there are several children to line up.

html
<style>
  .row { display: flex; flex-wrap: wrap; gap: 10px; }
  .frame { position: relative; width: 200px; height: 100px; background: #eee; }
  .dot { width: 40px; height: 20px; background: tomato; }
  .flex { display: flex; justify-content: center; align-items: center; }
  .grid { display: grid; place-items: center; }
  .abs .dot { position: absolute; inset: 0; margin: auto; }
  .tr .dot { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); }
  .auto .dot { margin: 40px auto 0; }
</style>
<div class="row">
  <div class="frame flex" id="f1"><div class="dot"></div></div>
  <div class="frame grid" id="f2"><div class="dot"></div></div>
  <div class="frame abs" id="f3"><div class="dot"></div></div>
  <div class="frame tr" id="f4"><div class="dot"></div></div>
  <div class="frame auto" id="f5"><div class="dot"></div></div>
</div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • text-align: center for inline content; margin-inline: auto for a block with a width.
  • Flex: justify-content and align-items on the parent.
  • Grid: place-items: center on the parent.
  • Absolute: inset: 0 and margin: auto, or top/left 50 percent with translate(-50%, -50%).
  • The parent needs a height to see vertical centring.

Common mistakes

  • using margin: 0 auto on an element with no width. A block with width: auto already fills the line, so there is nothing to centre.
  • vertical centring with height: 100vh without checking mobile browser toolbars. Use min-height and consider dvh units, then check support.

Follow-ups

  • why does vertical-align: middle not centre a div?
  • Why does vertical-align: middle not centre a div?
  • How would you centre an element inside the full viewport?

Tips

  • Memorise two methods: place-items: center on a grid and margin-inline: auto for a block with a width. Those cover most real cases.
  • Give the shortest working answer first (grid, place-items), then list others only if asked.
  • State the one requirement: the parent needs a height.