Vertical Centering with Flexbox
  • 21 Oct, 2019
  • css
  • flexbox
  • By Sergii

Vertical Centering with Flexbox

With Flexbox you can align anything (vertically or horizontally) quite easily with the align-items, align-self, and justify-content properties.

This box is both vertically and horizontally centered.

With Flexbox the presence of sibling elements doesn’t affect their ability to be vertically aligned:

Top
Centered
Bottom

HTML

<div class="center">
  <div class="center-item center-item--top"></div>
  <div class="center-item"></div>
  <div class="center-item center-item--bottom"></div>
</div>

CSS

.center {
  display: flex;
  align-items: center;
  justify-content: center;
}

.center-item {
  max-width: 50%;
}

.center-item--top {
  align-self: flex-start;
}

.center-item--bottom {
  align-self: flex-end;
}
  • css
  • flexbox