·5 min read·Blog

How to Center a Div in CSS (Every Method, 2026)

Centering used to be genuinely hard. In 2026 it's easy — but there are still five different ways to do it, each right for a different context. Here's which to use when.

Method 1: Flexbox (use this for most cases)

The modern default. Apply flex to the parent, then center both axes:

.parent {
  display: flex;
  justify-content: center;  /* horizontal */
  align-items: center;      /* vertical */
}

/* The child needs no special CSS */
.child {
  /* nothing required */
}

Requires the parent to have a defined height for vertical centering. If the parent wraps to content height, there's no extra space to center in vertically — add min-height: 100vh or a fixed height.

Method 2: CSS Grid

.parent {
  display: grid;
  place-items: center;   /* shorthand for align-items + justify-items */
}

/* For a single child, this is the most concise method */

place-items: centeris the most concise centering solution in CSS. One line. Works for both axes simultaneously. Use Grid when you're already using Grid for layout; use Flexbox otherwise.

Method 3: Absolute positioning + transform

.parent {
  position: relative;
}

.child {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

Works regardless of the child's dimensions — the transform shifts the element back by half its own width and height. Use this when:

  • The child must overlap other content (positioned on top of something)
  • You need to center something inside a parent that can't be flex/grid (rare)
  • You're centering a loading spinner or modal overlay on the viewport

Method 4: margin: auto (horizontal only)

.child {
  width: 600px;        /* must have explicit width */
  margin-left: auto;
  margin-right: auto;
  /* shorthand: margin: 0 auto; */
}

The classic technique — still correct for horizontally centering a block element with a known width. Common for page content containers:

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1rem;
}

Does not center vertically. Use this for page layout wrappers, article containers, and any block that should be horizontally centered with defined max-width.

Method 5: text-align: center (inline elements)

.parent {
  text-align: center;
}

/* Centers inline/inline-block children: text, images, buttons */

Only works for inline and inline-block elements (text, <img>, <button>, inline-block <div>). It does not center block-level divs — those need margin auto or flex/grid.

Centering full-screen (modal overlays)

.overlay {
  position: fixed;
  inset: 0;                  /* top: 0; right: 0; bottom: 0; left: 0; */
  display: flex;
  justify-content: center;
  align-items: center;
  background: rgba(0, 0, 0, 0.5);
}

.modal {
  /* no special centering needed — flex parent handles it */
  background: white;
  padding: 2rem;
  border-radius: 8px;
  max-width: 500px;
  width: 90%;
}

inset: 0 is the modern shorthand for setting all four sides to zero — making the overlay fill the entire viewport. Combined with flex centering on the parent, the modal is centered regardless of its size.

Centering vertically within the viewport

/* Simple full-page centering */
body {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 0;
}

/* Or with grid: */
body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  margin: 0;
}

The decision guide

SituationMethod
Center inside a container (general case)display: flex + justify-content/align-items: center
Single item in a grid celldisplay: grid + place-items: center
Page content wrapper (horizontal only)max-width + margin: 0 auto
Overlay / modal on top of contentposition: fixed + inset: 0 + flex center
Center text or inline elementstext-align: center on parent
Center element of unknown size on parentposition: absolute + transform: translate(-50%, -50%)

Related tools


Written by Achraf A., founder of TheFreeAITools.

A

Achraf A.

Full-Stack Developer · Morocco 🇲🇦

Building browser-based tools at The Free AI Tools since 2024. Every tool runs 100% in your browser — no uploads, no accounts.

Browse by category

Not sure which tool you need? Start with a category.

Everything you can do — for free

No software to buy. No account to create. Just open a tool and get it done.

Work with images

Compress photos before sending them by email, resize pictures for social media, remove backgrounds, or pick the perfect color for a design project — all without installing any app.

Edit and format text

Count words and characters in an essay, compare two documents side by side, convert text to different formats, or generate placeholder text for a presentation.

Stay safe online

Create a strong unique password in one click, check how secure a password is, encode or decode data, and generate secure tokens — your data never leaves your device.

Calculate anything

BMI, loan repayments, unit conversions, date differences, and dozens of other everyday calculations — no spreadsheet or formula knowledge required.

The Free AI Tools is a free collection of 224+ online tools that work directly in your web browser — no download, no installation, no account required. Whether you need to compress an image for email, count words in an essay, generate a strong password, create a QR code for your business, or format JSON for development — you will find a simple, free tool here.

Every tool is privacy-first: your files, text, and data never leave your device. Tools cover image editing, text processing, developer utilities, security & encoding, SEO & web, design & CSS, and more.

☕ Support Us