Centering Content of Unknown Size
suggest changeGrid centers content without knowing either element's dimensions:
.container {
min-block-size: 100dvh;
display: grid;
place-items: center;
padding: 1rem;
}
.content {
inline-size: min(100%, 40rem);
}
100dvh follows the dynamic viewport height, which is useful when mobile browser controls expand or collapse. The padding prevents the content from touching the viewport edge, while min-block-size allows the container to grow when its content is taller than the viewport.
Use 100vh as a deliberate fallback only when the target browser policy requires it:
.container {
min-block-size: 100vh;
min-block-size: 100dvh;
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents