【发布时间】:2021-10-13 23:12:53
【问题描述】:
我正在使用 grid-template 为一行四列设置一个简单的网格结构。最左边的两列有固定的宽度,剩下的两列应该填满剩余的空间。
但是,第二列可选 - 它可能根本不存在。在这种情况下,我不想为它保留任何空间。最右边的两列应该填满空间。
这对于网格模板显然是不可能的。有可能吗?
.grid {
display: grid;
grid-template-areas: "one two three four";
grid-template-columns: 8rem 8rem 1fr 1fr;
}
.one { background: #404788aa; grid-area: one; }
.two { background: #287d8eaa; grid-area: two; }
.three { background: #3cbb75aa; grid-area: three; }
.four { background: #dce319aa; grid-area: four; }
<div class="grid">
<div class="one">One</div>
<div class="two">Two</div>
<div class="three">Three</div>
<div class="four">Four</div>
</div>
<hr><p>Three and Four should fill the space:</p>
<div class="grid">
<div class="one">One</div>
<div class="three">Three</div>
<div class="four">Four</div>
</div>
【问题讨论】:
-
如果你有一个固定的宽度,那么你就是在对浏览器说“保持我的空间”!
-
Grid 是为您提前知道布局而设计的,因此在您提前不知道布局的情况下,它不是正确的布局技术。