Another classic: the 12-span grid. You can quickly write grids in CSS with the repeat()
function. Using: repeat(12, 1fr);
for the grid template columns gives you 12 columns each of 1fr
.
.parent {
display: grid;
grid-template-columns: repeat(12, 1fr);
}
.child-span-12 {
grid-column: 1 / 13;
}
Now you have a 12 column track grid, you can place child elements on the grid. One way to do this would be to place them using grid lines. For example, grid-column: 1 / 13
would span all the way from the first line to the last (13th) and span 12 columns. grid-column: 1 / 5;
would span the first four.
Another way to write this is by using the span
keyword. With span
, you set the starting line and then how many columns to span into from that starting point. In this case, grid-column: 1 / span 12
would be equivalent to grid-column: 1 / 13
, and grid-column: 2 / span 6
would be equivalent to grid-column: 2 / 8
.
.child-span-12 {
grid-column: 1 / span 12;
}
HTML
<div class="parent">
<div class="span-12 section coral">Span 12</div>
<div class="span-6 section green">Span 6</div>
<div class="span-4 section yellow">Span 4</div>
<div class="span-2 section blue">Span 2</div>
</div>
CSS
.parent {
display: grid;
grid-template-columns: repeat(12, 1fr);
}
.span-12 {
grid-column: 1 / span 12;
}
.span-6 {
grid-column: 1 / span 6;
}
.span-4 {
grid-column: 4 / span 4;
}
.span-2 {
grid-column: 3 / span 2;
}
/* centering text */
.section {
display: grid;
place-items: center;
text-align: center
}