Horizontally and vertically centers a child element within a parent element using flexbox
.
<div class="flexbox-centering"><div class="child">Centered content.</div></div>
.flexbox-centering {
display: flex;
justify-content: center;
align-items: center;
height: 100px;
}
Explanation
display: flex
enables flexbox.justify-content: center
centers the child horizontally.align-items: center
centers the child vertically.
Browser support
⚠️ Needs prefixes for full support.