title: Flex container width fit content width with flex-wrap: wrap?
body: <p>Here is an example:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.wrap {
    display: flex;
  }
  
  .wrap2 {
    padding: 4px;
    margin: 20px;
    outline: 1px dashed;
    display: flex;
    flex-wrap: wrap;
    max-width: 310px;
  }
  
  .box {
    width: 40px;
    height: 40px;
    outline: 1px solid;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="wrap"&gt;
    &lt;div class="wrap2"&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
      &lt;div class="box"&gt;&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>And here is what I need: </p>
  
  <p><img src="https://i.stack.imgur.com/rnse3.png" alt="Img"></p>
  
  <p>I tried to wrap flex container with another flex, but it didnt work. Is there any CSS only solution, or i should use js to achive desired result?</p>