If you change your html, body
rule to this
html, body {
margin: 0;
text-align: center;
display: flex;
flex-direction: column;
}
and then add this new rule
body {
min-height: 100vh;
}
and then change from height: calc(100% - 120px);
to flex-grow: 1;
in your content
rule it will work as you asked for.
The trick here is to make your body
a flex container, and then, by setting the content
to flex-grow: 1
it will fill the remaining space when the content is smaller, and the min-height: 100vh
will allow it to properly grow higher than the viewport when the content is bigger.
Updated codepen
Stack snippet
html, body {
margin: 0;
text-align: center;
display: flex;
flex-direction: column;
}
body {
min-height: 100vh;
}
.header {
height: 60px;
line-height: 60px;
background: rgba(255, 0, 0, 0.5);
}
.content {
flex-grow: 1;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
background: rgba(0, 255, 0, 0.5);
}
.content p {
width: 50%;
}
.footer {
height: 60px;
line-height: 60px;
background: rgba(0, 0, 255, 0.5);
}
.header, .footer {
font-weight: bold;
}
<div class="header">
Header
</div>
<div class="content">
<h1>I am the content</h1>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
</p>
</div>
<div class="footer">
Footer
</div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…