Centering with Flexbox
suggest changeFlexbox 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.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents