Centering with Flexbox

suggest change

Flexbox centers a child without knowing its dimensions:

<div class="container">
  <img src="example.jpg" alt="A descriptive alternative">
</div>
.container {
  min-block-size: 100dvh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 1rem;
}

.container > img {
  max-inline-size: 100%;
  block-size: auto;
}

justify-content aligns items along the main axis; align-items aligns them along the cross axis. With the default flex-direction: row, these correspond to horizontal and vertical alignment respectively.

Modern browsers support Flexbox without vendor prefixes. Avoid legacy Flexbox polyfills and the incompatible 2009 or 2012 prefixed syntaxes.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents