每行中的列数(仅CSS解决方案)?
当该行或列中只有一个项目时,如何强制一个项目扩展以填充整个行或列?
我的CSS:
.squareThumbnailContainer {
border-radius: 5px;
display: grid;
grid-template-columns: repeat(auto-fit, 1fr 1fr);
grid-template-rows: repeat(auto-fit, 1fr, 1fr);
gap: 0px 0px;
grid-template-areas:
". ."
". .";
overflow: hidden;
width: 70px;
height: 70px;
}
.smallSquareThumbnailHelper {
overflow: hidden;
}
.smallSquareThumbnail {
object-fit: cover;
width: 100%;
}
我知道最多有4个项目。
我的html(vue.js模板)
<div
class="squareThumbnailContainer"
>
<div
v-if="index <= 3"
class="smallSquareThumbnailHelper"
v-for="(img, index) in images"
>
<img
class="smallSquareThumbnail"
:src="img.imageUrl"
/>
</div>
</div>
这是我当前的结果。当只有一个项目时,它起作用,但是当有2或3个项目...
“ https://i.sstatic.net/tuizd.png” alt =“在此处输入图像说明”>
update2:几乎可以感谢@thpbaxxter,但无法使其适用于两个项目... 。
。
How can I force an item to expand to fill the whole row or column when there's only one item in that row or column?
My CSS:
.squareThumbnailContainer {
border-radius: 5px;
display: grid;
grid-template-columns: repeat(auto-fit, 1fr 1fr);
grid-template-rows: repeat(auto-fit, 1fr, 1fr);
gap: 0px 0px;
grid-template-areas:
". ."
". .";
overflow: hidden;
width: 70px;
height: 70px;
}
.smallSquareThumbnailHelper {
overflow: hidden;
}
.smallSquareThumbnail {
object-fit: cover;
width: 100%;
}
I know there's a max of 4 items.
My HTML (vue.js template)
<div
class="squareThumbnailContainer"
>
<div
v-if="index <= 3"
class="smallSquareThumbnailHelper"
v-for="(img, index) in images"
>
<img
class="smallSquareThumbnail"
:src="img.imageUrl"
/>
</div>
</div>
this is my current result. It works when there's just one item, but not when there are 2 or 3 items...
UPDATE2: almost there thanks to @thpbaxxter , but can't make it work for two items.... if I use .smallSquareThumbnailHelper:nth-child(3):last-child {grid-column: span 2;}
i get this result:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在最后一个/第三张图像上使用
网格列:Span 2;
填充两个列宽度。You can use
grid-column: span 2;
on the last/third image to fill two column widths.