Vertically-Center Anything

No, it’s not black magic, you really can center elements vertically. You can do this with flexbox…

  1. html,
  2. body {
  3. height: 100%;
  4. margin: 0;
  5. }
  6. body {
  7. -webkit-align-items: center;
  8. -ms-flex-align: center;
  9. align-items: center;
  10. display: -webkit-flex;
  11. display: flex;
  12. }

…and also with CSS Grid:

  1. body {
  2. display: grid;
  3. height: 100vh;
  4. margin: 0;
  5. place-items: center center;
  6. }

Want to center something else? Vertically, horizontally…anything, anytime, anywhere? CSS-Tricks has a nice write-up on doing all of that.

Note: Watch for some buggy behavior with flexbox in IE11.

Demo