I have two .flex-container
with 3 .box
children that I want to set up differently. See my snippet for a visual and my solution.
The first is easy as I can set the first box to be width: 100%;
and then just use flex-wrap: wrap;
. Good to go.
For the second .flex-container
I found I had to add an extra container .box-container
around the last two boxes and then give it flex: auto;
to get the desired effect.
My question is do I need this extra container .box-container
? If the answer is yes, so be it. I feel like I am missing something fundamental, but I'm thinking it could require it because these layouts aren't height-based and I guess it's vertically oriented? I tried using flex-direction: column;
in different ways.
Any solution using flexbox and no extra containers, like the first layout I presented, would be much appreciated. Thank you.
* {
box-sizing: border-box;
}
section {
display: flex;
margin-bottom: 10px;
padding: 10px;
border-radius: 5px;
}
.flex-container-1 {
flex-wrap: wrap;
border: 3px solid seagreen;
background-color: mediumseagreen;
}
.flex-container-1 .box:first-of-type {
width: 100%;
}
.flex-container-2 {
border: 3px solid firebrick;
background-color: indianred;
}
.box-container {
flex: auto;
}
.box {
display: flex;
justify-content: center;
align-items: center;
flex: auto;
padding: 10px;
border: 1px solid #fff;
}
.box .box-num {
font-size: 40px;
color: #fff;
}
<section class="flex-container-1">
<div class="box">
<span class="box-num">1</span>
</div>
<div class="box">
<span class="box-num">2</span>
</div>
<div class="box">
<span class="box-num">3</span>
</div>
</section>
<section class="flex-container-2">
<div class="box">
<span class="box-num">1</span>
</div>
<div class="box-container">
<div class="box">
<span class="box-num">2</span>
</div>
<div class="box">
<span class="box-num">3</span>
</div>
</div>
</section>