Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › CSS Snippets › Center a Div

CSS Snippet

How to Center a Div in CSS

The most-searched CSS question, answered with 5 modern methods and a live demo.

Last updated: October 2026

Quick answer: For both horizontal and vertical centering, use Flexbox: display: flex; justify-content: center; align-items: center;. For a one-line alternative, use Grid: display: grid; place-items: center;. For horizontal-only centering, use margin: 0 auto;.

🎬 Live Demo

This is the Flexbox method in action. Resize the browser to see it respond.

💡 Right-click the iframe and select "View frame source" to inspect the code.

1

Method 1: Flexbox (Recommended)

Flexbox is the modern standard for centering. Apply these three properties to the parent container:

.parent {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

.child {
    /* your div content */
}
  • justify-content: center — centers along the main axis (horizontal by default).
  • align-items: center — centers along the cross axis (vertical by default).
  • height: 100vh — gives the parent full viewport height so vertical centering has room to work.
2

Method 2: Grid with place-items (One-Liner)

Grid with place-items is the shortest way to center anything:

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

That's it. Two lines on the parent, and the child is centered both ways. place-items: center is shorthand for align-items: center and justify-items: center.

3

Method 3: Grid with justify and align

If you prefer explicit properties, Grid also accepts justify-content and align-content:

.parent {
    display: grid;
    justify-content: center;
    align-content: center;
    height: 100vh;
}

This is functionally equivalent to the Flexbox method but uses Grid's track alignment system.

4

Method 4: Auto Margins (Horizontal Only)

The classic horizontal centering method — still the simplest for that specific case:

.child {
    width: 300px;      /* width is required */
    margin: 0 auto;
}

Important: The child must have a defined width for margin: auto to have space to distribute. Without a width, the div stretches to fill its parent and centering has no effect.

5

Method 5: Absolute Positioning

Use this when you need to center an element without affecting document flow — for example, a modal or overlay:

.child {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}
  • top: 50%; left: 50% — moves the top-left corner to the parent's center.
  • transform: translate(-50%, -50%) — shifts the element back by half its own width and height.

Caveat: position: absolute removes the element from the normal document flow. Use it only when you need overlay-style centering.

🎯 Which Method Should You Use?

Scenario Best Method
Center both axes (most common) Flexbox or Grid place-items
Shortest possible code display: grid; place-items: center
Horizontal only margin: 0 auto
Overlay / modal (no flow impact) absolute + transform

⚡ Tailwind CSS Version

If you're using Tailwind CSS, centering is even simpler:

<!-- Flexbox -->
<div class="flex items-center justify-center h-screen">
    <div>Centered</div>
</div>

<!-- Grid -->
<div class="grid place-items-center h-screen">
    <div>Centered</div>
</div>

👉 Read our Tailwind Flexbox Guide and Tailwind Grid Guide.

⚠️ Common Mistakes

❓ Frequently Asked Questions

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

For most cases, Flexbox is the best modern method: display: flex, justify-content: center, align-items: center. For a one-liner, Grid with place-items: center is even shorter and works identically.

How do I center a div horizontally only?

Set a width on the div and use margin: 0 auto. This centers it horizontally within its parent without affecting vertical position.

How do I center a div vertically and horizontally?

Use Flexbox: display: flex, justify-content: center, align-items: center. Or use Grid: display: grid, place-items: center. Both center on both axes with minimal code.

Does centering with absolute positioning still work?

Yes. position: absolute, top: 50%, left: 50%, transform: translate(-50%, -50%) still works and is useful when you need to center without affecting document flow.

What is the difference between place-items and justify/align in Grid?

place-items: center is a shorthand that sets both align-items: center and justify-items: center. In Grid, justify-content aligns the grid tracks, while justify-items aligns items within their tracks.

🎯 What's Next?

← All CSS Snippets