I am trying to create a card with a header and content. The card’s maximum height should be restricted by the parent’s height. If the card can’t fit, the content should be vertically scrollable.
EDIT: To clarify, the card should shrink-wrap if the content fits on the screen.
Here’s my draft, which doesn’t work:
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.parent {
position: relative;
height: "100vh";
}
.card {
position: absolute;
background: red;
padding: 8px;
margin: 16px;
}
.content {
overflow-y: scroll;
}
<div class="parent">
<div class="card">
Header
<div class="content">
<pre>
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
This should scroll if it doesn't fit.
</pre>
</div>
</div>
2
Answers
Restrict the height of the card and content to 100%.
You might use
flex
: