CSSeasyCSS

Perfect Centering with Flexbox

The simplest way to perfectly center content both horizontally and vertically.

01

The problem

You need to center content perfectly in both directions within a container.

02

The solution

CSS
.flex-center {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh; /* or any height */
}

03

See it live

Isolated previewSandboxed
04

Put it to work

Example
<div class="flex-center">
  <div class="centered-content">
    <h1>Perfectly Centered</h1>
    <p>This content is centered both ways!</p>
  </div>
</div>

Worth knowing

This is the most reliable method for centering. Works with any content size.