CSS Flex如何制作动态网格
我正在尝试为我的产品卡图像制作动态网格系统,但我无法弄清楚。
我需要此布局,具体取决于产品的图像数量。问题是子项目是图像,不会裁剪。
我尝试了flex,但我无法将图像裁剪。
html
<div class="kit-product-card">
<div class="kit-image-container">
<% kit.products.each do |kit_product| %>
<div>
<img src="test.png">
</div>
<% end %>
</div>
</div>
是否有什么想法?也许玩溢出:隐藏
+ nth-childs?非常感谢!
CSS
.kit-product-card {
background: white;
border-radius: 4px;
display: flex;
justify-content: flex-start;
text-align: center;
align-items: center;
padding: 12px;
margin: 10px 0;
border: 1px solid darken(#f7f8fd, 4%);
}
.kit-image-container {
display: inline-flex;
flex-wrap: wrap; /* enable the wrap */
vertical-align: top;
width: 100%;
overflow: hidden;
padding: 10px;
div {
flex-basis: 50%; /* width = 50% */
flex-grow: 1; /* grow if alone in the last row */
border: 1px solid white;
box-sizing: border-box;
}
I'm trying to make a dynamic grid system for my product cards images, but I can't figure it out.
I need this layout depending on the number of images the product has. The problem is the child items are images and won't crop.
I tried with Flex but I can't make the images crop.
HTML
<div class="kit-product-card">
<div class="kit-image-container">
<% kit.products.each do |kit_product| %>
<div>
<img src="test.png">
</div>
<% end %>
</div>
</div>
¿Any ideas? Maybe playing with overflow:hidden
+ nth-childs? Thanks a lot!
CSS
.kit-product-card {
background: white;
border-radius: 4px;
display: flex;
justify-content: flex-start;
text-align: center;
align-items: center;
padding: 12px;
margin: 10px 0;
border: 1px solid darken(#f7f8fd, 4%);
}
.kit-image-container {
display: inline-flex;
flex-wrap: wrap; /* enable the wrap */
vertical-align: top;
width: 100%;
overflow: hidden;
padding: 10px;
div {
flex-basis: 50%; /* width = 50% */
flex-grow: 1; /* grow if alone in the last row */
border: 1px solid white;
box-sizing: border-box;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如评论中已经说过的那样,我将在定义儿童元素数量的容器中添加一个类。您可以使用Ruby有条件地执行此操作(我只是在示例中使用JS做到这一点)。
然后,您起诉Flexbox并根据儿童div的定义,具体取决于子元素的数量(按班级设置)。要适合图像,您可以使用
object-fit:cover
在图像上:As already said in the comment, I would add a class to the container that defines the amount of child elements. You can do this with Ruby conditional (I just did it with JS in the example).
Then you sue Flexbox and define the with of the child div depending on the amount of child elements (as set through the class). To fit the image you can use
object-fit: cover
on the image: