This is what I want:
But this is the closest I've got:
body{
margin: 0;
padding: 0;
border: 1px solid red;
}
.flex{
display: -ms-flexbox;
display: -webkit-box;
display: -webkit-flexbox;
display: -webkit-flex;
display: flex;
}
.flex > *{ margin: 0 10px; }
.flex > :first-child{ margin-left: 0; }
.flex > :last-child{ margin-right: 0; }
.flex.vertical > *{ margin: 10px 0; }
.flex.vertical > :first-child{ margin-top: 0; }
.flex.vertical > :last-child{ margin-bottom: 0; }
.vertical{
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
box-orient: vertical;
-webkit-flex-direction: column;
-moz-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
}
.box{
background: #000;
flex: auto;
min-height: 100px;
}
<div class="flex vertical">
<div class="flex">
<div class="box"> </div>
<div class="box"> </div>
</div>
<div class="flex">
<div class="box"> </div>
<div class="box"> </div>
<div class="box"> </div>
</div>
<div class="flex">
<div class="box"> </div>
<div class="box"> </div>
</div>
</div>
I'm applying a margin on flexbox items, then removing half of it from the first & last children.
The problem is that :first-child
is not always the first visually, because I may alter the layout order using flexbox ordering utilities. For example:
.flex > *{
-webkit-box-ordinal-group: 2;
-moz-box-ordinal-group: 2;
-ms-flex-order: 2;
-webkit-order: 2;
order: 2;
}
#important{
-webkit-box-ordinal-group: 1;
-moz-box-ordinal-group: 1;
-ms-flex-order: 1;
-webkit-order: 1;
order: 1;
}
body{
margin: 0;
padding: 0;
border: 1px solid red;
}
.flex{
display: -ms-flexbox;
display: -webkit-box;
display: -webkit-flexbox;
display: -webkit-flex;
display: flex;
}
.flex > *{ margin: 0 10px; }
.flex > :first-child{ margin-left: 0; }
.flex > :last-child{ margin-right: 0; }
.flex.vertical > *{ margin: 10px 0; }
.flex.vertical > :first-child{ margin-top: 0; }
.flex.vertical > :last-child{ margin-bottom: 0; }
.vertical{
-webkit-box-orient: vertical;
-moz-box-orient: vertical;
box-orient: vertical;
-webkit-flex-direction: column;
-moz-flex-direction: column;
-ms-flex-direction: column;
flex-direction: column;
}
.box{
background: #000;
flex: auto;
min-height: 100px;
}
<div class="flex vertical">
<div class="flex">
<div class="box"> </div>
<div class="box"> </div>
</div>
<div class="flex">
<div class="box"> </div>
<div class="box" id="important"> </div>
<div class="box"> </div>
</div>
<div class="flex">
<div class="box"> </div>
<div class="box"> </div>
</div>
</div>
Is there a way to take the visual order into account when applying the margin?
To set space between the flexbox you can use the flexbox property justify-content you can also visit all the property in that link. We can use the justify-content property of a flex container to set space between the flexbox.
justify-content: space-around , justify-content: space-evenly , justify-content: space-between . They provide spacing between elements and should help. Show activity on this post. If you are using bootstrap you need to add class.
As you see, the gap works perfectly for both CSS grid and flex - on the browsers that support them.
flex-end : items are packed toward to end line. center : items are centered along the line. space-between : items are evenly distributed in the line; first item is on the start line, last item on the end line. space-around : items are evenly distributed in the line with equal space around them.
The CSS spec has recently been updated to apply gap
properties to flexbox elements in addition to CSS grid elements. This feature is supported on the latest versions of all major browsers. With the gap
property, you can get what you want with just gap: 10px
(or whatever size you want).
You can try setting the same margin for all the boxes, and then revert this on the container:
So replace this:
.flex > * { margin: 0 10px; }
.flex > :first-child { margin-left: 0; }
.flex > :last-child { margin-right: 0; }
.flex.vertical > :first-child { margin-top: 0; }
.flex.vertical > :last-child { margin-bottom: 0; }
With this:
.flex.vertical { margin: -20px 0 0 -20px; }
.flex > * { margin: 0 0 0 20px; }
.flex.vertical > * { margin: 20px 0 0 0; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With