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