How to Center a Div in CSS

Written: August 22, 2026

Flexbox is the default modern answer for centering. Grid is equally clean. margin: auto still works for block horizontal centering when a width is set.

Flexbox

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

Grid

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

Keep learning

If this walkthrough on center a div in css helped, open the code again and change one input or assumption. Small experiments beat rereading the same example.

Want more step-by-step tutorials like this? Browse blog.xqa.io — and tell us which topic you want next.

Advertisement

Previous Article

Force Git Pull to Overwrite Local Changes

Next Article

Top 5 Selenium Test Automation Challenges (and Fixes)

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨