How do I create a 3x3 grid via CSS?
<p>Given 9 <code>div</code>s one after another, I want to create a grid 3X3 via CSS.</p>

<p>How do I do that?</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true">
<div class="snippet-code">
<pre class="snippet-code-css lang-css prettyprint-override"><code>.cell {
  height: 50px;
  width: 50px;
  background-color: #999;
  display: inline-block;
}

.cell:nth-child(3n) {
  background-color: #F00;
  /* what property should I use to get a line break after this element? */
}

/* this doesn't work; at least not in safari */
.cell:nth-child(3n)::after {
  display: block;
}</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="grid"&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
&lt;/div&gt;</code></pre>
</div>
</div>
</p>


  
  
  
  
  
  
  
  
  


<hr>

<p><strong>Note:</strong> I don't want <code>float/clear</code> solution. Focus is on CSS and not HTML restructure.</p>

<p>If I add <code>content: '\A'; white-space: pre;</code> to <code>::after</code> output comes out to be ugly.</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true">
<div class="snippet-code">
<pre class="snippet-code-css lang-css prettyprint-override"><code>.cell {
  height: 50px;
  width: 50px;
  background-color: #999;
  display: inline-block;
}

.cell:nth-child(3n) {
  background-color: #F00;
  /* what property should I use to get a line break after this element? */
}

.cell:nth-child(3n)::after {
  display: inline;
  content: '\A';
  white-space: pre;
}</code></pre>
<pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="grid"&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
  &lt;div class="cell"&gt;&lt;/div&gt;
&lt;/div&gt;</code></pre>
</div>
</div>
</p>

<p>How do I go about getting all div in a 3X3 row-column layout?</p>