This is an issue that I've run into on multiple occasions and figured this time I would try and seek a definitive answer. What I'm trying to do, is center an element vertically while having another element act as a "footer" and snapping to the bottom of the same container. Like so:
The problem is that the footer will have a dynamic height. The expected behavior is that if there's enough space, the middle element will stay centered vertically; if the footer becomes too large for that to happen, the centered content will shift upwards making room for the footer.
The closest solution I've found is in this question. But like every other solution I've seen, it requires knowing the height of the footer.
I have a hunch that this is impossible to do with flexbox. The solution I end up always falling back to is the classic position: relative
wrapper with position: absolute
on the footer.
Here's a demo fiddle if you'd like to see where I left off on my last attempt.
flex-container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
border: 4px solid blue;
height: 300px;
width: 300px;
}
flex-container>flex-spacer {
margin-top: auto;
visibility: hidden;
}
flex-container>flex-item {
display: flex;
}
flex-container>flex-footer {
margin-top: auto;
margin-bottom: auto;
}
flex-container>flex-spacer,
flex-container>flex-footer {
border: 4px solid chartreuse;
}
flex-container>flex-item>flex-item {
border: 4px solid aqua;
height: 50px;
width: 50px;
margin: 0 5px;
}
<flex-container>
<flex-spacer></flex-spacer>
<flex-item>
<flex-item></flex-item>
<flex-item></flex-item>
<flex-item></flex-item>
</flex-item>
<flex-footer>
this is the footer element<br /> this is the footer element<br />
</flex-footer>
</flex-container>