All questions

How do I center a div horizontally and vertically in CSS?

Answer

Make the parent a grid container and use `place-items: center`. It works for one child or many, and needs no fixed sizes. Give the parent a height, or it will only be as tall as its content. `min-height: 100dvh` centers in the visible viewport on mobile browsers too.

CSS solution

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

.child {
  width: min(90%, 420px);
  padding: 24px;
  border: 1px solid #d9dfe8;
  border-radius: 12px;
}

Edited by Jack · joysait@rediffmail.com

Add a Q&A

Drop an image here or click to choose. It is saved as images/N.ext.

Live preview

Publish to GitHub

Drafts 0

Drafts live in this browser only. Download or push them to make them public.

    Files to commit

    data/qa.json
    sitemap.xml
    robots.txt

    Push with the GitHub API

    Uses a fine-grained token with Contents: Read and write on your repo. The token stays in this page's memory only: it is never saved or sent anywhere except api.github.com.