Can we create a layout like in below image by CSS grid?
Asked
Active
Viewed 3,681 times
-1
Takeshi Tokugawa YD
- 266
- 3
- 27
- 82
-
2Use 6 columns instead of 3. – sol Aug 03 '20 at 01:19
-
1Some popular CSS frameworks use a 12 column grid, because it gives you flexibility with layouts. – fubar Aug 03 '20 at 01:21
-
Thank you for the hint, WFM! It also requires to know how to span N columns without knowing at advance where it started; `grid-column auto / span N` works for me. – Takeshi Tokugawa YD Aug 03 '20 at 01:32
1 Answers
10
You can create a 6 columns grid, and set grid-column: auto / span 3; for the first two elements and auto / span 2 for the rest of them:
.grid {
display: grid;
grid-template-columns: repeat(6, 1fr);
grid-gap: 16px;
grid-auto-rows: 32px;
}
.grid span {
background-color: steelblue;
grid-column: auto / span 2;
}
.grid span:nth-child(1),
.grid span:nth-child(2) {
grid-column: auto / span 3;
}
<div class="grid">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</div>
Hao Wu
- 14,894
- 4
- 17
- 48