I want to use flexbox to set-up a simple elastic* grid...
.container{
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
...but I want the outer margins to be equal to the space between the items.
*By "elastic" I mean that the widths not fixed. They are all percentages, and so different total widths at different screen widths.
This does not appear to be the default behavior of justify-content: space-between
. It generates equal space "around" each item but the space does not collapse the way margins do. So the result is that the "between" spaces are twice as wide as the far right and left (assuming you're flex-direction is row
).
In the sketch below, the first diagram is what justify-content: space-between
does naturally. Notice widths A and B. The second diagram is what I want (only one consistent gutter C).
Is this possible with flexbox?
Update
I would like to avoid having to add "row" wrapping elements if possible. The pseudo element solution sounds like just the ticket (see answers below), but let's work it out so that the items can be of an undetermined number (and wrap onto new rows)