To set the minimal distance between flexbox items I'm using margin: 0 5px on .item and margin: 0 -5px on container. This seems like a hack. Is there another property or method intended to accomplish this goal?
#box {
display: flex;
width: 100px;
margin: 0 -5px;
}
.item {
background: gray;
width: 50px;
height: 50px;
margin: 0 5px;
}
<div id='box'>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
<div class='item'></div>
</div>

Before ~20202, Flexbox didn't have anything akin to
border-spacingfor tables. The answer below is from that time. Now, thegapproperty fulfills this role and is recommended for this application.Flexbox doesn't have collapsing margins1. Therefore, achieving what you are asking for is a bit more difficult.
In my experience, the "cleanest" way that doesn't use
:first-child/:last-childand works without any modification onflex-wrap:wrapis to setpadding:5pxon the container andmargin:5pxon the children. That will produce a10pxgap between each child and between each child and their parent.JSFiddle Demo