To achieve vertical centering and bottom alignment in a flex container, you can apply the CSS property margin-top: auto
to both the first and last child divs. Adding margin-top: auto
to the last-child div will push it to the end of the parent container, but it will also push the other two divs to the top. Therefore, adding margin-top: auto
to the first-child div as well will help position all three divs in the center of the space from the top to the last-child div.
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
}
.container div {
margin: 5px;
padding: 5 px 10px;
border: 1px solid;
}
.container > div:last-child,
.container > div:first-child{
margin-top: auto;
}
<div class="container">
<div>This div should be vertically centered</div>
<div>This one too</div>
<div>And this div shoud be placed at the bottom of the flex container</div>
</div>