我有一个可以填充整个水平空间的布局,由于网格布局,最后一行的宽度仍然与其他行相同。
现在,在调整屏幕大小时,项目的宽度会发生变化,以便填满整个行。是否可以将项目的宽度固定并且将网格间隙调整为可以调整的项目?
https://jsfiddle.net/c60ymrfu/
当前CSS:
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
grid-auto-rows: 20px;
grid-gap: 5px;
}
答案 0 :(得分:0)
您可以添加一些包装元素,并为商品指定固定宽度,如下所示:
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
grid-auto-rows: 20px;
grid-gap: 5px;
}
.item {
background: yellow;
text-align: center;
border: 1px solid red;
width: 100px;
margin: auto;
}
<div class="container">
<div>
<div class="item">1</div>
</div>
<div>
<div class="item">2</div>
</div>
<div>
<div class="item">3</div>
</div>
<div>
<div class="item">4</div>
</div>
<div>
<div class="item">5</div>
</div>
<div>
<div class="item">6</div>
</div>
<div>
<div class="item">7</div>
</div>
<div>
<div class="item">8</div>
</div>
<div>
<div class="item">9</div>
</div>
<div>
<div class="item">10</div>
</div>
</div>
然后,您可以根据需要将其放置在包装的父级中。
答案 1 :(得分:0)
您可以与justify-items
或place-items
一起设置固定的项目宽度,具体取决于您是希望发生在一个轴还是两个轴上
.container {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
grid-auto-rows: 20px;
grid-gap: 5px;
place-items: center;
}
.item {
background: yellow;
text-align: center;
border: 1px solid red;
width: 100px;
}
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
<div class="item">5</div>
<div class="item">6</div>
<div class="item">7</div>
<div class="item">8</div>
<div class="item">9</div>
<div class="item">10</div>
</div>