I want to make use of the new Flexbox Gap property, like gap: 1rem
.
This is a simple column layout with blocks that have percentage based sizes. I use flex-basis
to set the size. I want those blocks to be able to "float" over multiple lines. That's why there is flex-wrap: wrap
on the parent.
<div class="flex gap">
<div class="span-half">
<div class="box">
span-half
</div>
</div>
<div class="span-half">
<div class="box">
span-half
</div>
</div>
<div class="span-quarter">
<div class="box">
span-quarter
</div>
</div>
<div class="span-quarter">
<div class="box">
span-quarter
</div>
</div>
<div class="span-half">
<div class="box">
span-half
</div>
</div>
</div>
.box {
background: #ddd;
padding: .5rem;
}
.flex {
display: flex;
flex-wrap: wrap;
}
.gap {
gap: 1rem;
}
.span-half {
flex-basis: 50%;
}
.span-quarter {
flex-basis: 25%;
}
Expected are two lines: The first line is made of the two half (50%) containers. The second line consists of two quarter (25%) containers and a third half (50%) container.
This works when no gap is added. But once the gap property comes in the containers seem to take the availabe space, but the actual gap size is added on top of that.
It also seems to work for one line, when flex-wrap is not enabled, but maybe it is just forcing it into layout that way.
I can imagine that it requires a CSS calc function to substract the space taken by the gap from the total percentage per row but I wonder if and how this can be done. The number of child items is not fixed and it can be any kind of combination of quarter, half, third and fifth elements.
For reference: I am looking to refactor the current Flexbox Grid System of my Teutonic CSS design system based on paddings and margins: https://teutonic.co/examples/flexbox#spans I am also aware of other alternative solutions using CSS grid or floats instead.
I hope the question is clear enough?