I trying to create a layout using CSS Grid like the image (any item is square):
Code I'm trying:
CSS
.grid-container {
padding: 20px;
display: grid;
grid-gap: 20px;
grid-auto-rows: 1fr;
grid-template-columns: repeat(2, 1fr);
}
.item {
position: relative;
background: #ccc;
}
/* Square */
.item:after {
content: '';
display: block;
padding-top: 100%;
}
@media screen and (min-width: 640px) and (max-width: 1023px) {
/* 640 ~ 1023 */
.grid-container {
grid-template-columns: repeat(3, 1fr);
}
.item:nth-child(6n + 1) {
grid-column: span 2 / 3;
grid-row: span 2;
}
.item:nth-child(6n + 6) {
grid-column: span 2 / 3;
grid-row: span 2;
grid-column: 2 / 4;
}
.item:nth-child(6n + 5) {
grid-column: span 1 / 2;
}
}
@media print, screen and (min-width: 1024px) {
/* 1024+ */
.grid-container {
grid-template-columns: repeat(4, 1fr);
}
.item:nth-child(10n + 1) {
grid-column: span 2 / 3;
grid-row: span 2;
}
.item:nth-child(10n) {
grid-column: span 2 / 3;
grid-row: span 2;
grid-column-end: 5;
}
.item:nth-child(10n + 8) {
grid-column-start: 1;
}
}
You can find my code here: JSFiddle
Result show:
I think use position: absolute
with JavaScript that calculate the girds position can solve problem.
How to create this layout use pure CSS?
question from:
https://stackoverflow.com/questions/65909209/how-can-i-make-complex-layout-with-css-grid 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…