<h2>Vertical Centering with Flexbox</h2>
<p>Both the container and the child element are resizable using a bottom-right handle. Try resizing either of them. The child box will remain vertically and horizontally centered inside the parent.</p>
<main class="container">
<div class="item"><p>Try resizing this element or the container.</p></div>
</main>
body {
font-family: Arial, sans-serif;
font-size: 1.3em;
padding: 0 2em 3em;
line-height: 1.4;
}
h2 {
margin-top: 14px;
text-align: center;
}
.container {
display: flex;
align-items: center;
justify-content: center;
background: lightblue;
min-height: 90px;
min-width: 440px;
height: 400px;
padding: 1em;
border: solid 2px;
resize: both;
overflow: auto;
}
.item {
background: pink;
max-width: 60%;
min-width: 400px;
min-height: 2em;
text-align: center;
resize: both;
overflow: hidden;
border: solid 2px;
padding: 1em;
display: flex;
justify-content: center;
align-items: center;
}
.item p {
margin: 0;
text-align: center;
}
@media (max-width: 560px) {
.container, .item {
min-width: auto;
}
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.