gpt4 book ai didi

CSS Grid 重复网格布局

转载 作者:行者123 更新时间:2023-11-28 14:14:34 27 4
gpt4 key购买 nike

我正在创建一个重复的网格系统,我需要在其中重复与前 7 个元素相同的结构。 Div A 到 G 正在生成我想要的结果,所有其他 div 都在正确的位置列,但只有 H 和 M(新行中的第一个和第六个元素)没有达到所需的高度。

H需要等于I和J组合的高度,M需要等于K和L的组合高度,同A和F:

body {
margin: 40px;
}

.wrapper {
display: grid;
grid-template-columns: repeat(3, [col] 1fr);
grid-template-rows: repeat(10, [row] auto);
grid-gap: 1em;
background-color: #fff;
color: #444;
}

.box {
background-color: #444;
color: #fff;
border-radius: 5px;
padding: 20px;
font-size: 150%;
display: flex;
align-items: center;
}

.box:nth-of-type(7n+1) {
grid-column: col / span 2;
}

.box:nth-of-type(7n+3) {
grid-column: col 3 / span 1;
}

.box:nth-of-type(7n+4),
.box:nth-of-type(7n+5) {
grid-column: col 1 / span 1;
}

.box:nth-child(7n+6) {
grid-column: col 2 / span 2;
}

.box:nth-child(7n+7) {
grid-column: col 1 / span 3;
}

.box:first-child {
grid-row: row / span 2;
}

.box:nth-child(2) {
grid-row: row;
}

.box:nth-child(3) {
grid-row: row 2;
}

.box:nth-child(4) {
grid-row: row 3;
}

.box:nth-child(5) {
grid-row: row 4;
}

.box:nth-child(6) {
grid-row: row 3 / span 2;
}
<div class="wrapper">
<div class="box">A</div>
<div class="box">B</div>
<div class="box">C</div>
<div class="box">D</div>
<div class="box">E</div>
<div class="box">F</div>
<div class="box">G</div>

<!-- items with same spans need to be repeted -->
<div class="box">H</div>
<div class="box">I</div>
<div class="box">J</div>
<div class="box">K</div>
<div class="box">L</div>
<div class="box">M</div>
<div class="box">N</div>
</div>

最佳答案

首先我简化了你的代码:

  • 仅使用 nth-child 逻辑来调整行列大小,

  • 删除了 grid-template-rows 和网格线的命名,

我们现在遇到的问题是方框 EF 在行中位置不对:

body {
margin: 40px;
}

.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
/* grid-template-rows: repeat(10, [row] auto); */
grid-gap: 1em;
background-color: #fff;
color: #444;
}

.box {
background-color: #444;
color: #fff;
border-radius: 5px;
padding: 20px;
font-size: 150%;
display: flex;
align-items: center;
}

.box:nth-of-type(7n+1) {
grid-column: span 2;
grid-row: span 2;
}

.box:nth-child(7n+6) {
grid-column: span 2;
grid-row: span 2;
}

.box:nth-child(7n+7) {
grid-column: span 3;
}
<div class="wrapper">
<div class="box">A</div>
<div class="box">B</div>
<div class="box">C</div>
<div class="box">D</div>
<div class="box">E</div>
<div class="box">F</div>
<div class="box">G</div>

<!-- items with same spans need to be repeted -->
<div class="box">H</div>
<div class="box">I</div>
<div class="box">J</div>
<div class="box">K</div>
<div class="box">L</div>
<div class="box">M</div>
<div class="box">N</div>
</div>

现在您可以使用 grid-column: 2/4F 移动到最后两列,然后使用 grid-auto-flow: dense 将其拉起 - 请参见下面的演示:

body {
margin: 40px;
}

.wrapper {
display: grid;
grid-template-columns: repeat(3, 1fr);
/*grid-template-rows: repeat(10, [row] auto);*/
grid-auto-flow: dense; /* fills in the spaces */
grid-gap: 1em;
background-color: #fff;
color: #444;
}

.box {
background-color: #444;
color: #fff;
border-radius: 5px;
padding: 20px;
font-size: 150%;
display: flex;
align-items: center;
}

.box:nth-of-type(7n+1) {
grid-column: span 2;
grid-row: span 2;
}

.box:nth-of-type(7n+5) {
grid-column: 1;
}

.box:nth-child(7n+6) {
grid-column: 2 / 4; /* changed */
grid-row: span 2;
}

.box:nth-child(7n+7) {
grid-column: span 3;
}
<div class="wrapper">
<div class="box">A</div>
<div class="box">B</div>
<div class="box">C</div>
<div class="box">D</div>
<div class="box">E</div>
<div class="box">F</div>
<div class="box">G</div>
<!-- items with same spans need to be repeted -->
<div class="box">H</div>
<div class="box">I</div>
<div class="box">J</div>
<div class="box">K</div>
<div class="box">L</div>
<div class="box">M</div>
<div class="box">N</div>
</div>

关于CSS Grid 重复网格布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56053054/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com