I want to use CSS to make a 3X3 grid from a set of 9 divs placed one after another.
How do I do that?
.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;
}
<div class="grid">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
Note:I reject the float/clear approach. CSS is the main topic, not HTML restructuring.
If I add content: '\A'; white-space: pre; to:: after output comes out to be ugly.
.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;
}
<div class="grid">
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
<div class="cell"></div>
</div>
How can I achieve a 3X3 row-column layout with all div?