Is it possible to remove the height from empty rows in grid with `grid-template-areas`?(能不能用`grid-模板-areas`去掉网格中空行的高度?)
问题描述
我有一个包含八张(或更少)卡的网格。我希望卡片被自动放置在网格中,而不是知道它们的宽度和高度。也就是说,宽度和高度应该在网格样式中指定。当我展示所有8张卡片时,它工作得很好。示例:
grid-template-areas:
'card-1 card-1 card-2'
'card-3 card-4 card-5'
'card-3 card-6 card-5'
'card-7 card-8 card-8';
grid-template-rows: 20px 20px 20px 20px;
grid-template-columns: 20px 20px 20px;
数据-lang="js"数据-隐藏="真"数据-控制台="真"数据-巴贝尔="假">
body {
background: white;
}
.grid {
display: grid;
grid-template-areas:
'card-1 card-1 card-2'
'card-3 card-4 card-5'
'card-3 card-6 card-5'
'card-7 card-8 card-8';
grid-template-rows: 20px 20px 20px 20px;
grid-template-columns: 20px 20px 20px;
grid-gap: 5px;
background: black;
}
.card-1 {
grid-area: card-1;
background: red;
}
.card-2 {
grid-area: card-2;
background: orange;
}
.card-3 {
grid-area: card-3;
background: yellow;
}
.card-4 {
grid-area: card-4;
background: green;
}
.card-5 {
grid-area: card-5;
background: cyan;
}
.card-6 {
grid-area: card-6;
background: blue;
}
.card-7 {
grid-area: card-7;
background: indigo;
}
.card-8 {
grid-area: card-8;
background: gray;
}
<div class="grid">
<div class="card-1"></div>
<div class="card-2"></div>
<div class="card-3"></div>
<div class="card-4"></div>
<div class="card-5"></div>
<div class="card-6"></div>
<div class="card-7"></div>
<div class="card-8"></div>
</div>
但是,当卡片少于8张时,问题就出现了,网格的最后一行没有被卡片占据,但它仍然有高度: 数据-lang="js"数据-隐藏="真"数据-控制台="真"数据-巴贝尔="假">
body {
background: white;
}
.grid {
display: grid;
grid-template-areas:
'card-1 card-1 card-2'
'card-3 card-4 card-5'
'card-3 card-6 card-5'
'card-7 card-8 card-8';
grid-template-rows: 20px 20px 20px 20px;
grid-template-columns: 20px 20px 20px;
grid-gap: 5px;
background: black;
}
.card-1 {
grid-area: card-1;
background: red;
}
.card-2 {
grid-area: card-2;
background: orange;
}
.card-3 {
grid-area: card-3;
background: yellow;
}
.card-4 {
grid-area: card-4;
background: green;
}
.card-5 {
grid-area: card-5;
background: cyan;
}
.card-6 {
grid-area: card-6;
background: blue;
}
.card-7 {
grid-area: card-7;
background: indigo;
}
.card-8 {
grid-area: card-8;
background: gray;
}
<div class="grid">
<div class="card-1"></div>
<div class="card-2"></div>
<div class="card-3"></div>
<div class="card-4"></div>
</div>
我可以通过删除网格模板区域,用网格自动行替换网格模板行,用网格行+网格列替换网格区域来解决这个问题:
数据-lang="js"数据-隐藏="真"数据-控制台="真"数据-巴贝尔="假">body {
background: white;
}
.grid {
display: grid;
grid-auto-rows: 20px;
grid-template-columns: 20px 20px 20px;
grid-gap: 5px;
background: black;
}
.card-1 {
grid-row: 1;
grid-column: 1 / 3;
background: red;
}
.card-2 {
grid-row: 1;
grid-column: 3;
background: orange;
}
.card-3 {
grid-row: 2 / 4;
grid-column: 1;
background: yellow;
}
.card-4 {
grid-row: 2;
grid-column: 2;
background: green;
}
.card-5 {
grid-row: 2 / 4;
grid-column: 3;
background: cyan;
}
.card-6 {
grid-row: 3;
grid-column: 2;
background: blue;
}
.card-7 {
grid-row: 4;
grid-column: 1;
background: indigo;
}
.card-8 {
grid-row: 4;
grid-column: 2 / 4;
background: gray;
}
<div class="grid">
<div class="card-1"></div>
<div class="card-2"></div>
<div class="card-3"></div>
<div class="card-4"></div>
</div>
但这种方法不是很方便。请帮忙,是否可以使用网格模板区域和指定网格样式中的宽度和高度来做我想做的事情?(使用层叠指定栅格子对象的宽度和高度也是不可接受的)
推荐答案
您可以继续使用grid-colum
/grid-row
,但如下所示简化代码,以防您在Html代码中始终保持良好的顺序:
.grid {
display: grid;
grid-auto-rows: 20px;
grid-auto-flow: dense; /*to make sure we fill all the space (not mandatory)*/
grid-template-columns: 20px 20px 20px;
grid-gap: 5px;
background: black;
margin:5px;
}
/*only need this*/
.card-1,.card-8 {
grid-column:span 2;
}
.card-3,.card-5 {
grid-row:span 2;
}
/**/
.card-1 {background: red;}
.card-2 {background: orange;}
.card-3 {background: yellow;}
.card-4 {background: green;}
.card-5 {background: cyan;}
.card-6 {background: blue;}
.card-7 {background: indigo;}
.card-8 {background: gray;}
<div class="grid">
<div class="card-1"></div>
<div class="card-2"></div>
</div>
<div class="grid">
<div class="card-1"></div>
<div class="card-2"></div>
<div class="card-3"></div>
<div class="card-4"></div>
</div>
<div class="grid">
<div class="card-1"></div>
<div class="card-2"></div>
<div class="card-3"></div>
<div class="card-4"></div>
<div class="card-5"></div>
<div class="card-6"></div>
</div>
<div class="grid">
<div class="card-1"></div>
<div class="card-2"></div>
<div class="card-3"></div>
<div class="card-4"></div>
<div class="card-5"></div>
<div class="card-6"></div>
<div class="card-7"></div>
<div class="card-8"></div>
</div>
这篇关于能不能用`grid-模板-areas`去掉网格中空行的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:能不能用`grid-模板-areas`去掉网格中空行的高度?
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- Flexslider 箭头未正确显示 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01