CSS Flex如何制作动态网格

发布于 2025-02-12 03:31:42 字数 1317 浏览 1 评论 0原文

我正在尝试为我的产品卡图像制作动态网格系统,但我无法弄清楚。

我需要此布局,具体取决于产品的图像数量。问题是子项目是图像,不会裁剪。

“在此处输入图像描述”

我尝试了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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原来是傀儡 2025-02-19 03:31:42

正如评论中已经说过的那样,我将在定义儿童元素数量的容器中添加一个类。您可以使用Ruby有条件地执行此操作(我只是在示例中使用JS做到这一点)。

然后,您起诉Flexbox并根据儿童div的定义,具体取决于子元素的数量(按班级设置)。要适合图像,您可以使用object-fit:cover在图像上:

// JS code just for demonstration purpose
var input = document.querySelector('input');
input.addEventListener('change', function() {
  var amount = input.value;
  var container = document.querySelector('.kit-image-container');
  container.innerHTML = '';
  var class_name = '';
  for (let i = 1; i <= amount; i++) {
    let ele = document.createElement('div');
    ele.innerHTML = '<img src="https://via.placeholder.com/100.jpg">';
    container.appendChild(ele);
    class_name = `child-${i}`;
  }
  container.className = '';
  container.classList.add('kit-image-container', class_name);
})
.kit-image-container {
  display: flex;
  flex-wrap: wrap;
}

.kit-image-container > div {
  width: 100%;
}

.child-4 > div,
.child-5 > div,
.child-6 > div,
.child-7 > div,
.child-8 > div {
  width: 50%;
}

.child-3 > div:nth-last-of-type(-n+2) {
  width: 50%;
}

.child-6 > div {
  width: calc(100% / 3);
}

.child-5 > div:nth-last-of-type(-n+3),
.child-7 > div:nth-last-of-type(-n+3),
.child-8 > div:nth-last-of-type(-n+3) {
  width: calc(100% / 3);
}

.child-8 > div:nth-last-of-type(-n+6) {
  width: calc(100% / 3);
}

.kit-image-container > div > img {
  display: block;
  height: 100%;
  width: 100%;
  object-fit: cover;
}




/* for visualization only */
.kit-image-container {
  margin-top: 2em;
}

.kit-image-container > div {
  border: 1px solid black;
  box-sizing: border-box;
  min-height: 50px;
}
<input type="number" min="1" max="8" step="1" value="1">
<label>Define the amount of child element</label>


<!-- actual Markup -->
<div class="kit-image-container child-3"></div>

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:

// JS code just for demonstration purpose
var input = document.querySelector('input');
input.addEventListener('change', function() {
  var amount = input.value;
  var container = document.querySelector('.kit-image-container');
  container.innerHTML = '';
  var class_name = '';
  for (let i = 1; i <= amount; i++) {
    let ele = document.createElement('div');
    ele.innerHTML = '<img src="https://via.placeholder.com/100.jpg">';
    container.appendChild(ele);
    class_name = `child-${i}`;
  }
  container.className = '';
  container.classList.add('kit-image-container', class_name);
})
.kit-image-container {
  display: flex;
  flex-wrap: wrap;
}

.kit-image-container > div {
  width: 100%;
}

.child-4 > div,
.child-5 > div,
.child-6 > div,
.child-7 > div,
.child-8 > div {
  width: 50%;
}

.child-3 > div:nth-last-of-type(-n+2) {
  width: 50%;
}

.child-6 > div {
  width: calc(100% / 3);
}

.child-5 > div:nth-last-of-type(-n+3),
.child-7 > div:nth-last-of-type(-n+3),
.child-8 > div:nth-last-of-type(-n+3) {
  width: calc(100% / 3);
}

.child-8 > div:nth-last-of-type(-n+6) {
  width: calc(100% / 3);
}

.kit-image-container > div > img {
  display: block;
  height: 100%;
  width: 100%;
  object-fit: cover;
}




/* for visualization only */
.kit-image-container {
  margin-top: 2em;
}

.kit-image-container > div {
  border: 1px solid black;
  box-sizing: border-box;
  min-height: 50px;
}
<input type="number" min="1" max="8" step="1" value="1">
<label>Define the amount of child element</label>


<!-- actual Markup -->
<div class="kit-image-container child-3"></div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文