【发布时间】:2020-07-13 17:18:45
【问题描述】:
我做了一个两列两行的简单网格,很简单。但里面的内容却不是。
我在这里添加了 sn-p,当我将 grid-template-rows 与 auto 或 4 1fr 一起使用时,它会根据最大部分给出高度相同的部分。
我想要的是在内容较少时缩小的 4 个部分,就像部分只有一个数字一样。
为了澄清更多,我希望这些具有绿色背景和非常小的内容的部分缩小以仅适合内容。
更新 1:我添加了一张照片来说明我想用这个实现什么。您会注意到左右两边的行根据内容对齐。
浅蓝色部分会扩大其内容以适应内容,蓝色背景部分会扩大,因为浅绿色背景部分更大。
我希望这些部分仅在只有内容时扩展,但我希望两者一起扩展或在没有内容时变小。
section {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: 1fr 1fr 1fr 1fr;
grid-gap: 20px;
}
.grid-section{
background: blue;
display: grid;
grid-template-rows: 1fr 1fr 1fr 1fr;
}
.grid-section div:nth-child(1){
background: yellow;
}
.grid-section div:nth-child(2){
background: green;
}
<section>
<div class="grid-section">
<div>
1
</div>
<div>
1
</div>
<div>
1
</div>
<div>
1
</div>
</div>
<div class="grid-section">
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries
</div>
<div>
1
</div>
<div>
2
</div>
<div>
1
</div>
</div>
<div class="grid-section">
<div>
1
</div>
<div>
1
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s.
</div>
<div>
1
</div>
</div>
<div class="grid-section">
<div>
1
</div>
<div>
1
</div>
<div>
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries
</div>
<div>
1
</div>
</div>
</section>
【问题讨论】:
-
你试过 minmax() 函数吗?它允许您指定最小值/最大值之间的值范围,以设置 CSS Grid 列和/或行的宽度和/或高度。
-
是的,我试过了,这是一个链接codepen.io/tarekhassan/pen/BajPRJw?editors=1100
-
我用照片和解释更新了我的问题,希望这更清楚。
标签: css-grid