I have a vertical flex container that centers its content. The height of the container should depend on the viewport height, such that the flexed content appears vertically centered. However, the container height should never be smaller than the content's, otherwise the top of the content would flow out of the viewport's top.
In other words:
100vh
.However, this does not seem to work:
.flex-container {
display: flex;
flex-direction: column;
height: 100vh;
justify-content: center;
}
.flex-item {
flex: 0 0 auto;
}
.content {
background-color: red;
font-size: 50px;
color: white;
height: 115vh;
}
<div class="flex-container">
<div class="flex-item">
<div class="content">
Content
</div>
</div>
</div>
As you can see, the content starts to disappear as soon as we set .content
's height
to something higher than 100vh
.
Note that I can't set a fixed min-height
on .flex-container
since I don't know the content's height beforehand.
How to achieve that?
Getting the child of a flex-item to fill height 100% Set position: absolute; on the child. You can then set width/height as required (100% in my sample).
Instead of max-height: 250px you should use flex: 0 0 250px and with flex-direction: column it will set height of element to 250px.
The flex-basis property sets the initial length of the flex-items inside a flex-container. You can think of it as an improved version of the width or height values. That is, flex-basis has always prevalence over width or height .
Flex-basis will still obey any min / max-width: or height settings. Again, it is based on the flex-direction: Flex-basis in a column overrides height:, this is important because although width: will obey flex-shrink, height: will not.
You can set
.flex-container {
height: auto;
min-height: 100vh;
}
That way if height of content is less than 100vh - container's height will be 100vh (from min-height:100vh;
).
html,body {
margin: 0;
}
.flex-container {
display: flex;
flex-direction: column;
height: auto;
min-height: 100vh;
justify-content: center;
}
.flex-item {
flex: 0 0 auto;
}
.content {
background-color: red;
font-size: 50px;
color: white;
height: 25vh;
}
<div class="flex-container">
<div class="flex-item">
<div class="content">
Content
</div>
</div>
</div>
If height of content is more than 100vh - container's height will be height of it's content (from height:auto;
).
html,body {
margin: 0;
}
.flex-container {
display: flex;
flex-direction: column;
height: auto;
min-height: 100vh;
justify-content: center;
}
.flex-item {
flex: 0 0 auto;
}
.content {
background-color: red;
font-size: 50px;
color: white;
height: 115vh;
}
<div class="flex-container">
<div class="flex-item">
<div class="content">
Content
</div>
</div>
</div>
EDIT: Since you also need max-height
for your content, you can add next media query in the end:
@media (min-height: 1800px) {
.content {
height: 1800px;
}
}
It will force height of .content
to be 1800px
on displays with height >= 1800px
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With