TH The Sass Way
Layout: Flexbox & Grid

How to Center a Div in CSS (5 Ways That Actually Work)

How to Center a Div in CSS (5 Ways That Actually Work)
tldrTo center a div horizontally and vertically, set its parent to display: flex, then add justify-content: center and align-items: center. Give the parent a height (like min-height: 100vh) so there is vertical space to center within. For a single child this Flexbox approach works in every modern browser; use grid with place-items: center for containers, or margin: 0 auto for horizontal-only centering.

The Fastest Way to Center a Div

To center a <div> both horizontally and vertically, give its parent three lines:

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

That's it. The child element sits dead center, no fixed widths, no magic numbers, no negative margins. Flexbox is the right answer roughly 90% of the time, and it works in every browser you care about in 2026.

But "center a div" hides several different problems: center only horizontally, center a single item, center a whole grid of items, or center something that has to sit on top of other content. Below are five techniques that each actually work, when to reach for each, and the pitfalls that trip people up.

Quick Comparison: Which Method to Use

Method Centers Best for Browser support
margin: 0 auto Horizontal only Block element with a set width Universal
text-align: center Horizontal (inline content) Text, inline, inline-block Universal
Flexbox Both axes Single item or a row of items Universal (IE11 partial)
CSS Grid + place-items Both axes Whole container, cleanest syntax All modern browsers
Absolute + transform Both axes Overlays, modals, unknown size Universal

If you only remember one thing: Flexbox for a single child, Grid for a container of children, margin: auto when you just need horizontal.

Method 1: margin: 0 auto (Horizontal Only)

The oldest trick still works for the most common case: a block-level element that needs to sit in the middle of the page horizontally.

.box {
  width: 600px;      /* a width is REQUIRED */
  max-width: 100%;   /* so it stays responsive */
  margin: 0 auto;    /* 0 top/bottom, auto left/right */
}

The browser splits the leftover horizontal space equally between the left and right margins. Two things break it:

Understanding why a width is required comes down to the CSS box model — how content, padding, border, and margin combine to size an element.

Method 2: text-align: center (Inline Content)

Reach for this when the thing you're centering is text, an image, or any inline/inline-block element. Set it on the parent, not the child.

.parent {
  text-align: center;
}

This centers inline content inside the parent. A common gotcha: it does not center a block-level child like a <div> with a set width — for that, use margin: auto or Flexbox. But if you set the child to display: inline-block, text-align: center on the parent will center it horizontally.

Flexbox is the modern default for centering because it handles both axes with no measurements and adapts to any content size.

.parent {
  display: flex;
  justify-content: center; /* main axis: horizontal by default */
  align-items: center;     /* cross axis: vertical by default */
  min-height: 100vh;       /* give the parent height to center within */
}

The one detail people miss: the parent needs a height for vertical centering to be visible. If the parent is only as tall as its content, there's no vertical space to distribute. min-height, a fixed height, or an inherited height all work.

To center just horizontally, drop align-items. To center just vertically, drop justify-content. Because justify-content follows the main axis, it flips to vertical if you set flex-direction: column — worth knowing before you swap the two properties in confusion. For the full property-by-property breakdown, see our complete guide to Flexbox.

Centering a row of items

Flexbox also centers multiple children as a group, with even spacing:

.parent {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem; /* space between items */
}

Method 4: CSS Grid with place-items

Grid gives you the shortest centering syntax in all of CSS. place-items is shorthand for align-items and justify-items at once.

.parent {
  display: grid;
  place-items: center;
  min-height: 100vh;
}

Two lines center any single child on both axes. This is the cleanest option when the parent is a dedicated container — a hero section, an empty state, a loading spinner. It's supported in every modern browser. If you're already building your layout with columns and rows, lean on Grid here; our CSS Grid tutorial walks through a full responsive build.

Use place-content: center instead if you want to center the entire grid track group rather than items within their cells.

Method 5: Absolute Positioning + transform

When the element has to sit on top of other content — a modal, a tooltip, a badge — and you don't know its exact size, absolute positioning with a transform is the reliable choice.

.parent {
  position: relative; /* establishes the positioning context */
}

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

Here's the clever part. top: 50% and left: 50% push the element's top-left corner to the center, which looks off-center. translate(-50%, -50%) then pulls it back by half its own width and height — and because percentages in transform refer to the element itself, you never need to know its dimensions. It self-corrects at any size.

The only requirement: the parent must have position: relative (or any non-static position), or the child will position itself against the viewport instead.

Bonus: A Reusable Sass Centering Mixin

If you center elements all over a project, wrap the Flexbox pattern in a Sass mixin so you write it once and reuse it everywhere. This keeps your stylesheet DRY and lets you tweak the behavior in a single place.

@mixin center($direction: both) {
  display: flex;

  @if $direction == both {
    justify-content: center;
    align-items: center;
  } @else if $direction == horizontal {
    justify-content: center;
  } @else if $direction == vertical {
    align-items: center;
  }
}

// Usage
.hero {
  min-height: 100vh;
  @include center;             // both axes
}

.toolbar {
  @include center(horizontal); // horizontal only
}

The @if / @else if branches let one mixin cover every centering case with an argument. That's the kind of small abstraction that makes a large codebase easier to maintain — the same reasoning behind reusable spacing and typography helpers.

Common Centering Mistakes

Which Method Should You Actually Use?

For 2026, here's the honest default order:

  1. One item, both axes? Flexbox (display: flex + justify-content + align-items) or Grid place-items: center.
  2. A whole container? Grid — place-items: center is the least code.
  3. Just horizontal, block element? margin: 0 auto with a width.
  4. Text or inline? text-align: center on the parent.
  5. Overlay of unknown size? Absolute positioning + translate(-50%, -50%).

The old hacks — table-cell display, negative margins with fixed pixels, ghost-element vertical-align tricks — are no longer necessary. Flexbox and Grid solved centering for good, and both have near-universal support. Pick based on whether you're centering an item (Flexbox) or a container (Grid), and you'll rarely go wrong.

FAQ

How do I center a div horizontally and vertically?

Set the parent to display: flex, then justify-content: center (horizontal) and align-items: center (vertical). Give the parent height via min-height: 100vh or a fixed height, otherwise there is no vertical space to center within. CSS Grid with place-items: center does the same in two lines and is equally well supported in modern browsers.

Why is my div not centering vertically?

The most common cause is that the parent has no height. Flexbox and Grid can only distribute space that exists, so if the parent is only as tall as its content there is nothing to center within. Add min-height: 100vh, a fixed height, or an inherited height to the parent, and vertical centering will appear immediately.

How do I center a div with margin auto?

Use margin: 0 auto and give the element an explicit width or max-width. The browser splits the leftover horizontal space evenly between the left and right margins. It only works horizontally and only when a width is set, because a block element defaults to full width and leaves no space to distribute. It never centers vertically.

What is the best way to center a div in 2026?

Flexbox for a single child (display: flex with justify-content and align-items) and CSS Grid with place-items: center for a whole container. Both center on both axes, need no fixed measurements, and have near-universal browser support. Older table-cell and negative-margin hacks are no longer necessary.

How do I center an absolutely positioned div?

Give the child position: absolute with top: 50% and left: 50%, then transform: translate(-50%, -50%). The offsets place its top-left corner at center, and the transform pulls it back by half its own size, so it stays centered at any dimensions. The parent must have position: relative to act as the positioning context.

What is the difference between justify-content and align-items?

In Flexbox, justify-content aligns items along the main axis and align-items aligns them along the cross axis. By default the main axis is horizontal, so justify-content controls horizontal centering and align-items controls vertical. If you set flex-direction: column, the axes flip and the two properties swap roles.