EDIT: Updating to clarify that this doesn’t need to use flex
and looks like grid
/subgrid
is the direction to go. My example uses flex
because that’s what I was familiar with.
How do I, with CSS only, get the title (blue) box to match the height of the biggest title box in the row. The red box should then fill the rest of the space and the stat cards will evenly fill the red box.
I’ve seen solutions that use JavaScript, but would like to avoid that if possible. The "Desired Layout" screenshot is using a fixed height on the title box to demonstrate.
body {
background-color: #454545;
color: #fafafa;
}
.cards {
display: flex;
gap: 2rem;
}
.card {
display: flex;
flex-direction: column;
width: 300px;
background-color: green;
padding: 1em;
&__title {
background-color: blue;
//height: 100px;
padding: 0 1em;
}
&__content {
height: 100%;
background-color: red;
padding: 1em;
}
}
.stats {
display: flex;
flex-direction: column;
gap: 1rem;
height: 100%;
}
.stat {
display: flex;
flex-direction: column;
justify-content: center;
background-color: grey;
padding: 0 1em;
min-width: 145px;
height: 100%;
}
<div class="cards">
<div class="card">
<div class="card__title">
<h3>Title</h3>
</div>
<div class="card__content">
<div class="stats">
<div class="stat">
<div class="stat__title">
<h4>Stat 1</h4>
</div>
<div class="stat__content">
<p>Content</p>
</div>
</div>
<div class="stat">
<div class="stat__title">
<h4>Stat 2</h4>
</div>
<div class="stat__content">
<p>Content</p>
</div>
</div>
<div class="stat">
<div class="stat__title">
<h4>Stat 3</h4>
</div>
<div class="stat__content">
<p>Content</p>
</div>
</div>
</div>
</div>
</div>
<div class="card">
<div class="card__title">
<h3>TItle lorem ipsum dolor sit amet, consectetur adipiscing elit</h3>
</div>
<div class="card__content">
<div class="stats">
<div class="stat">
<div class="stat__title">
<h4>Stat 1</h4>
</div>
<div class="stat__content">
<p>They're just warming up!</p>
</div>
</div>
</div>
</div>
</div>
<div class="card">
<div class="card__title">
<h3>Title</h3>
</div>
<div class="card__content">
<div class="stats">
<div class="stat">
<div class="stat__title">
<h4>Stat 1</h4>
</div>
<div class="stat__content">
<p>Content</p>
</div>
</div>
<div class="stat">
<div class="stat__title">
<h4>Stat 2</h4>
</div>
<div class="stat__content">
<p>Content</p>
</div>
</div>
</div>
</div>
</div>
</div>
Code Pen: https://codepen.io/Maynards/pen/NWJYvzq
4
Answers
As a few have pointed out, using
grid
is the way to go. I was able to achieve the desired result usingsubgrid
👍. Browser support seems to be pretty good now forsubgrid
, so I'll be using that.Thank you everyone!
What type of layout design do you want ?
You can either give it a fixed height or use
css grid
.something like that ?