I’m trying to create a full-screen layout in which the content will adjust to the size of the footer.
body {
margin: 0;
padding: 0;
}
.layout {
height: 100vh;
display: flex;
flex-direction: column;
background-color: yellow;
}
.layout__content {
height: 100%;
align-self: stretch;
background-color: blue;
}
.content {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background-color: green;
}
.content__text {
background-color: white;
}
.footer {
height: 20vh;
background-color: red;
}
<div class='layout'>
<main class='layout__content'>
<div class='content'>
<text class='content__text'>Here be dragons</text>
</div>
</main>
<footer class='footer' />
</div>
As you can see, if you change the value of height
in .footer
, then the content will fill all the space remaining from the footer. Everything works as intended.
Now I’m trying to make another layout. If the total height of the content and footer does not exceed 100vh
, this display is similar to what was previously. But if it surpasses due to the content height, then a scroll should appear.
body {
margin: 0;
padding: 0;
}
.layout {
min-height: 100vh;
display: flex;
flex-direction: column;
background-color: yellow;
}
.layout__content {
height: 100%;
align-self: stretch;
background-color: blue;
}
.content {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background-color: green;
}
.content__text {
height: 150vh;
background-color: white;
}
.footer {
height: 20vh;
background-color: red;
}
<div class='layout'>
<main class='layout__content'>
<div class='content'>
<text class='content__text'>Here be dragons</text>
</div>
</main>
<footer class='footer' />
</div>
In the example, the text was 150vh
long. But the problem is if this value will be removed, the content will collapse.
body {
margin: 0;
padding: 0;
}
.layout {
min-height: 100vh;
display: flex;
flex-direction: column;
background-color: yellow;
}
.layout__content {
height: 100%;
align-self: stretch;
background-color: blue;
}
.content {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background-color: green;
}
.content__text {
background-color: white;
}
.footer {
height: 20vh;
background-color: red;
}
<div class='layout'>
<main class='layout__content'>
<div class='content'>
<text class='content__text'>Here be dragons</text>
</div>
</main>
<footer class='footer' />
</div>
Notes:
- I want to change attributes only of
layout
andlayout__content
. footer
may have any height, so it is impossible to just hardcodeheight
,min-height
,max-height
etc in the other elements. In the example it was hardcoded to20vh
, but it is only in the example.
I’ve already tried the following things:
layout
–height: 1px;
layout-content
–min-height: inherit
;layout-content
–min-height: 100%
;
So, any ideas?
2
Answers
So I have solved this with double-dimensional flex-grow:
Content
content__text
is short:Same code, but
content__text
is long: