I have a pretty basic grid setup in flex, but I recently came across an oversight I made.
I have columns that have a margin-right: 3em and flex-basis: calc(33% - 3em).
My problem is that the 4th and 5th of these don't line up until there is a full "row" of 3.
Any insights as to why this is happening? I imagine I may be over-complicating things as per usual.
section {
width: 1170px;
margin: 0 auto;
padding: 4em;
background-color: lightgray;
}
.flex {
display: flex;
}
.wrap {
flex-wrap: wrap;
}
.column {
flex: 1;
flex-direction: column;
margin-right: 3em;
}
.column:last-child {
margin-right: 0;
}
.three {
max-width: 33%;
flex-basis: calc(33% - 3em);
}
.three:nth-child(3n) {
margin-right: 0;
}
.debug {
margin-bottom: 3em;
background-color: #ebf5fb;
height: 3em;
border: 1px dashed red;
text-align: center;
}
<section>
<div class="flex wrap">
<div class="column three debug">3 Columns</div>
<div class="column three debug">3 Columns</div>
<div class="column three debug">3 Columns</div>
<div class="column three debug">3 Columns</div>
<div class="column three debug">3 Columns</div>
</div>
</section>
Since you use
flex: 1
, it will make the element take all available space, what's left after theflex-basis
been retracted.What stop the 2 last items from filling the entire row is the
max-width
, and since that its value is wider than theflex-basis
, they will expand to it.Either remove
flex: 1
fromcolumn
, or use the same calc formax-width
as used forflex-basis
Stack snippet
Updated based on a comment
If to also make the items spread equally inside its parent, one need to divide the sum of the actual margin/gutter/gap space with the amount of items.
So in this case it would be (2 gaps * 3em) / 3 items = 2em
Also, one need the closest possible to a 1/3, which can be either e.g. 33.33333% or (100% / 3)
Stack snippet
Thank you LGson for pointing me in the right direction.
My math was incorrect.
I needed to take 100% - the total margins in the row.
Instead of flex-basis: calc(33% - 3em); it needed to be flex-basis: calc( (100% - 6em) / 3);
Updated Snippet