Vertical Alignment with Box Alignment
suggest changeWhen the parent uses Grid, center its child in the block direction with align-items:
.parent {
min-block-size: 20rem;
display: grid;
align-items: center;
}
To center on both axes, use place-items: center. When the parent uses Flexbox, use align-items: center for the cross axis and justify-content: center for the main axis.
The older position: relative; top: 50%; transform: translateY(-50%) technique remains useful in unusual positioning contexts, but it should not be the default for ordinary layout.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents