【发布时间】:2019-10-22 17:34:49
【问题描述】:
这是 CSS Grid 比 flexbox 做得更好的一个典型例子。像这样的:
#container{
display: flex;
width:100%;
height:100%;
}
#wrapper {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
outline: 1px solid red;
width: 72%;
height: 100%;
padding: 24px 0px;
box-sizing: border-box;
}
.item {
background-color: yellow;
height: 30px;
margin: 15px;
max-width:110px;
border-radius: 8px;
cursor: pointer;
}
Here the cards looks good as the number of cards are more
<div id ="container">
<div id="wrapper">
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
<div class="item"></div>
</div>
</div>
<hr/>
we have so much white space between cards, how can we get rid of the space as the cards can be dynamic like 2 or 1. and a max of 3 per container
<div id ="container">
<div id="wrapper">
<div class="item"></div>
<div class="item"></div>
</div>
</div>
我们如何才能在网格之间留出空间并使它们左对齐,以便它正确地适合顶部卡片,使用 Css 网格动态调整,或者有任何有效的方法来实现这一点。
注意 - 在这里阻止我的一件事是我们不能有一个固定的宽度,然后在 3 列处设置网格,因为我们不希望在调整容器大小时有空白
【问题讨论】:
-
你试过
auto-fill而不是auto-fit吗? -
是的,我做到了,但是在调整大小时它不适合并在容器右侧留下一个空白区域