首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在CSS网格中折叠未使用的行?

如何在CSS网格中折叠未使用的行?
EN

Stack Overflow用户
提问于 2018-06-06 05:10:03
回答 1查看 12.8K关注 0票数 21

因此,我在移动设备上有一个简单的三个段落堆栈,我想在较大的视窗上的网格中设置样式,而不改变源顺序。

第一部分可能只有几行或者根本没有内容。

在这种情况下,如何使第一行折叠以便第二行填充空间?也就是说,当顶部为空时,最后一部分应该出现在顶部

.grid  {
    display: grid;
    grid-template-rows: min-content auto;
    grid-template-columns: 60% 40%;
    grid-template-areas: 
        "main first"
        "main last";
}

.first { grid-area: first; }
.main { grid-area: main; }
.last { grid-area: last; }
<div class="grid">
  <b class="first">Grant me revenge!</b>
  <b class="main">Arnold ipsum. Just bodies. I need your clothes, your boots, and your motorcycle. Grant me revenge! And if you do not listen, then to HELL with you. Make it quick because my horse is getting tired. Come on don't bullshit me. Into the tunnel. Bring your toy back to the carpet.</b>
  <b class="last">Make it quick because my horse is getting tired.</b>
</div>

EN

回答 1

Stack Overflow用户

发布于 2018-06-06 06:17:04

而不是这样:

grid-template-rows: min-content auto

试试这个:

grid-template-rows: min-content 1fr

使用1fr,您告诉第二行消耗该列中所有可用的空闲空间。因此,当.first元素中没有内容时,.second元素将占用空间。

auto的问题是它的长度是relative to other grid items on the track。这意味着它不会总是收缩以适应特定的项目(就像min-content设计的那样),并且如果另一个项目有一定的大小(就像您在代码中看到的那样),它也不会允许项目完全消失。

.grid  {
  display: grid;
  grid-template-rows: min-content 1fr; /* adjustment */
  grid-template-columns: 60% 40%;
  grid-template-areas: 
    "main first"
    "main last";
}

.first { grid-area: first; background-color: orange; }
.main  { grid-area: main;  background-color: aqua; }
.last  { grid-area: last;  background-color: lightgray; }
<div class="grid">
  <b class="first">Grant me revenge!</b>
  <b class="main">Arnold ipsum. Just bodies. I need your clothes, your boots, and your motorcycle. Grant me revenge! And if you do not listen, then to HELL with you. Make it quick because my horse is getting tired. Come on don't bullshit me. Into the tunnel. Bring your toy back to the carpet.</b>
  <b class="last">Make it quick because my horse is getting tired.</b>
</div>

<br>

<div class="grid">
  <b class="first"></b>
  <b class="main">Arnold ipsum. Just bodies. I need your clothes, your boots, and your motorcycle. Grant me revenge! And if you do not listen, then to HELL with you. Make it quick because my horse is getting tired. Come on don't bullshit me. Into the tunnel. Bring your toy back to the carpet.</b>
  <b class="last">Make it quick because my horse is getting tired.</b>
</div>

票数 29
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50708974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档