css - CSS:如何设置动态项目数的最后一个网格行?
问题描述
我有我的第一行作为22px
并使用grid-auto-rows
来制作后续行70px
。有没有办法在22px
不使用模板的情况下制作最后一行,因为当时我不知道div
里面有多少?
.cont {
background: grey;
height: 600px;
display: grid;
grid-template-rows: 22px;
grid-auto-rows: 70px;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 3px;
}
.cont div {
background: red;
}
<div class="cont">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
解决方案
如果您总是有 3 列,请保持模板行自动并定义元素的高度。70px
在某些条件下,除了前 3 个和后 3 个之外,所有的都应该有。
.cont {
background: grey;
display: grid;
grid-template-columns: 1fr 1fr 1fr;
grid-gap: 3px;
margin:10px;
}
.cont div {
background: red;
height:70px;
}
.cont div:nth-child(1), /* 1st */
.cont div:nth-child(2), /* 2nd */
.cont div:nth-child(3), /* 3rd */
.cont div:nth-last-child(1), /* last one */
.cont div:nth-last-child(2):not(:nth-child(3n + 3)), /* before the last if not the last one of a row*/
.cont div:nth-last-child(3):nth-child(3n + 1){ /* before the two last only if the fisrt one of a row*/
height:22px;
}
<div class="cont">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="cont">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="cont">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
推荐阅读
- javascript - ReactJS :: Jest 测试 ::“TypeError:无法读取未定义的属性'then'”
- bash - 用逗号替换新行,但我需要一行文件
- c - C: getchar() 销毁指针
- r - 绘图功能,不同的长度
- php - 如果电子邮件不在数据库中,则 Google 登录重定向回登录页面
- javascript - 如果满足所有条件,如何使 .addEventListener 仅执行函数?
- node.js - Docker 的 Angular 图像因依赖错误而退出
- reactjs - 找不到模块“sass”
- python-3.x - 在循环浏览程序的某些部分时处理菜单输入?
- makefile - 多变量赋值的Makefile问题