CSS flex children gutters with margin
<p>I have a gallery layout, and I want to separate every box with margin. The problem is the last box in every row is not align with the grid; there is still space on the right side, how can I do this without using padding? ( that will force me to add another wrapper div )</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-css lang-css prettyprint-override"><code>* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
ul {
  list-style-type: none;
}
.flex {
  display: -webkit-box;
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  -webkit-flex-wrap: wrap;
  -ms-flex-wrap: wrap;
  flex-wrap: wrap;
}
.align-center {
  -webkit-box-align: center;
  -webkit-align-items: center;
  -ms-flex-align: center;
  align-items: center;
}
.space-between {
  -webkit-box-pack: justify;
  -webkit-justify-content: space-between;
  -ms-flex-pack: justify;
  justify-content: space-between;
}
.container {
  max-width: 1000px;
  margin: 0 auto;
  height: 100%;
  padding: 0 1em;
  border: 1px solid;
}
nav {
  background: #464646;
  height: 70px;
}
.box {
  background: lightblue;
  padding: 1em;
  -webkit-box-flex: 0;
  -webkit-flex: 0 0 calc(50% - 1em);
  -ms-flex: 0 0 calc(50% - 1em);
  flex: 0 0 calc(50% - 1em);
  text-align: center;
  margin-right: 1em;
  margin-bottom: 1em;
}</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;nav&gt;
  &lt;div class="container flex align-center space-between"&gt;

    &lt;ul&gt;
      &lt;li&gt;Logo&lt;/li&gt;
    &lt;/ul&gt;

    &lt;ul&gt;
      &lt;li&gt;Home&lt;/li&gt;
    &lt;/ul&gt;

  &lt;/div&gt;
&lt;/nav&gt;

&lt;div class="container"&gt;

  &lt;header&gt;
    &lt;h1&gt;Header&lt;/h1&gt;
  &lt;/header&gt;

&lt;/div&gt;

&lt;div class="container"&gt;

  &lt;div class="gallery flex"&gt;
    &lt;div class="box"&gt;BOX 1&lt;/div&gt;
    &lt;div class="box"&gt;BOX 2&lt;/div&gt;
    &lt;div class="box"&gt;BOX 3&lt;/div&gt;
  &lt;/div&gt;

&lt;/div&gt;</code></pre>
</div>
</div>
</p>

<p><strong>Note:</strong> I can't use flex grow: 1 because I don't always have two boxes in a row.</p>

<p>I can't use space between because the grid is dynamic so for example If the width of the box is 33.33% and you have five boxes, the second row will be with two boxes one on the left side and second on the right side.</p>