TExcelTAD
Home › CSS › CSS Align

CSS Align

"How do I centre a div?" is the most famous CSS question. The answer depends on what you're centring (text, a block, an image) and which direction (horizontally, vertically or both). Here are the reliable techniques.

Quick reference

Goal Technique
Centre text / inline content horizontally text-align: center on the parent
Centre a block with a width horizontally margin: 0 auto
Centre anything both ways Parent: display: flex; justify-content: center; align-items: center;
Centre anything both ways (shortest) Parent: display: grid; place-items: center;
Centre a positioned element position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
Centre one line of text vertically line-height equal to the box height
Push to the right margin-left: auto (in flex), or float: right, or text-align: right

Horizontal centring

text-align: center centres inline content (text, images, inline-blocks) inside a block. To centre the block itself, give it a width and auto side margins.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .text { text-align: center; background: #e3f2fd; padding: 8px; }
  .block { width: 200px; margin: 12px auto; background: #ffcc80; padding: 8px; }
  .img-wrap { text-align: center; }
  .dot { display: inline-block; width: 50px; height: 50px; border-radius: 50%; background: #ab47bc; }
</style>
</head>
<body>
  <p class="text">Centred text with text-align</p>
  <div class="block">Centred block with margin: 0 auto</div>
  <div class="img-wrap"><span class="dot"></span></div>
</body>
</html>

Tip: margin: 0 auto needs a width (or max-width) smaller than the container; a full-width block has no free space to share.

Centring both ways with Flexbox and Grid

Modern CSS makes vertical centring easy. Flexbox centres on the main axis with justify-content and the cross axis with align-items. Grid's place-items: center does both in one line.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: flex; gap: 12px; }
  .stage { flex: 1; height: 200px; border: 2px dashed #90a4ae; }
  .flex { display: flex; justify-content: center; align-items: center; }
  .grid { display: grid; place-items: center; }
  .item { padding: 14px 18px; border-radius: 10px; color: white; }
  .a { background: #26a69a; }
  .b { background: #ef5350; }
</style>
</head>
<body>
  <div class="stage flex"><div class="item a">Flexbox</div></div>
  <div class="stage grid"><div class="item b">Grid</div></div>
</body>
</html>

Centring with position and transform

For overlays and modals, position the element at 50% / 50% and then shift it back by half its own size with transform. This works without knowing the element's dimensions.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; margin: 0; }
  .hero { position: relative; height: 240px; background: linear-gradient(135deg, #1a237e, #00bcd4); }
  .badge { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
           background: white; padding: 16px 24px; border-radius: 12px; font-weight: 700; box-shadow: 0 6px 20px rgba(0,0,0,.3); }
</style>
</head>
<body>
  <div class="hero"><div class="badge">Dead centre</div></div>
</body>
</html>

Left and right alignment

To put things on opposite sides, Flexbox's justify-content: space-between or margin-left: auto on one item are the cleanest options.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .bar { display: flex; align-items: center; padding: 10px 14px; background: #263238; color: white; margin-bottom: 10px; border-radius: 8px; }
  .between { justify-content: space-between; }
  .push { margin-left: auto; background: #ffb300; color: #263238; padding: 6px 12px; border-radius: 6px; }
</style>
</head>
<body>
  <div class="bar between"><span>Logo</span><span>Menu</span></div>
  <div class="bar"><span>Home</span>&nbsp;&nbsp;<span>Docs</span><span class="push">Login</span></div>
</body>
</html>

Vertical align for inline content

vertical-align aligns inline or table-cell elements relative to the line: baseline, top, middle, bottom, text-top, sub, super. It does not vertically centre a block inside its parent — that's a common mistake.

Test yourself

Which single declaration on a grid container centres its child both horizontally and vertically?

  1. align: center
  2. place-items: center
  3. text-align: center
  4. vertical-align: middle

Why does margin: 0 auto sometimes fail to centre a div?

  1. The div has no width smaller than its container
  2. Margins can't be auto
  3. It only works on inline elements
  4. It needs position: absolute

In transform: translate(-50%, -50%), what are the percentages relative to?

  1. The parent
  2. The viewport
  3. The element's own size
  4. The font size