5

需要:一个 CSS 唯一的解决方案,可以在每行的基础上启用等高网格“部分”,这也是响应式的。

该图有望比本文中的文字更好地解释要求:

有用的图表

“项目网格”应该是响应式的——因为它可以根据视口宽度在每行显示不同数量的卡片。在给定的行内,等效部分应该在“每行”的基础上具有相同的高度。

在下面的 HTML 和 CSS 中 - 项目卡被分成我们需要的行(在桌面和移动的两个示例断点处),但内容部分的高度是可变的(恶心):

.items {
  max-width: 1200px;
}

.item {
  width: 25%;
  box-sizing: border-box;
  display: inline-block;
  vertical-align: top;
  padding: 0 12px;
  margin: 24px -4px 24px 0;
}

@media (max-width: 600px) {
  .item {
    width: 50%;
  }
}

.item__heading {
  background-color: #d4d0f5;
  padding: 10px;
  text-align: center;
  border: 1px solid #bbbbbb;
}

.item__content {
  padding: 10px;
  border-left: 1px solid #bbbbbb;
  border-right: 1px solid #bbbbbb;
}

.item__price {
  background-color: #e0f6d9;
  padding: 10px;
  text-align: center;
  border: 1px solid #bbbbbb;
}
<div class="items">

  <div class="item">
    <div class="item__heading">
      Item 1
    </div>
    <div class="item__content">
      Some content that is not that long
    </div>
    <div class="item__price">
      £99.99
    </div>
  </div>


  <div class="item">
    <div class="item__heading">
      Item 2
    </div>
    <div class="item__content">
      Some content that is longer than other items on the same row and sets the height of this section
    </div>
    <div class="item__price">
      £69.99
    </div>
  </div>

  <div class="item">
    <div class="item__heading">
      Item 3
    </div>
    <div class="item__content">
      Some content that is not that long
    </div>
    <div class="item__price">
      £69.99
    </div>
  </div>

  <div class="item">
    <div class="item__heading">
      Item 4
    </div>
    <div class="item__content">
      Some content that is not that long
    </div>
    <div class="item__price">
      £109.99
    </div>
  </div>

  <div class="item">
    <div class="item__heading">
      Item 5
    </div>
    <div class="item__content">
      Some content that is a medium kind of length blah blah
    </div>
    <div class="item__price">
      £29.99
    </div>
  </div>

  <div class="item">
    <div class="item__heading">
      Item 6
    </div>
    <div class="item__content">
      Some content that is not that long
    </div>
    <div class="item__price">
      £99.99
    </div>
  </div>

    
</div>

下面的 codepen 是一个基于 JavaScript 的解决方案,可以实现预期的结果 - 但这是我试图避免的: https ://codepen.io/rusta/pen/KmbVKd

限制

  • 网格列表中要显示的项目数可以是 1-150 之间的任意数字
  • 要显示的项目内容的大小将是真正可变的(因此不能选择“合理的”最小高度)

我希望新的 CSS Grid 系统能帮助我实现上述目标,但是玩了一段时间后,它似乎需要比我希望的更多的结构,而且响应方面似乎相当具有挑战性。但也许某处有一个基于 CSS Grid 的答案

进一步说明:我说的是纯 CSS 解决方案,我的意思是非 JS 解决方案。如果 HTML 块需要更改(订单/嵌套/类名)以支持非 JS 解决方案,这绝对没问题

在这个简单的例子中——我们只关注具有“匹配高度”的“内容”部分——因为我们可以假设标题和价格部分自然是相同的高度。在任何匹配的网格部分(标题/内容/价格/其他)上启用“等效性”会很好,但这可能是另一天......

4

1 回答 1

5

通过给.items display: flex; flex-wrap: wrap;item将成为弹性项目并从左到右流动并在没有更多空间时换行。

然后你给.item display: flex; flex-direction: column;,这将使它们也成为一个弹性容器,并且通过使用列方向,它的子元素将垂直流动,就像块元素一样。

最后,您给出.item__content flex: 1;,这将使其垂直占用任何剩余空间,因此每一行的item高度都相同。

更新了代码笔

堆栈片段

.items {
  display: flex;
  flex-wrap: wrap;
  max-width: 1200px;
}

.item {
  display: flex;
  flex-direction: column;
  width: 25%;
  box-sizing: border-box;
  vertical-align: top;
  padding: 0 12px;
  margin: 24px -4px 24px 0;
}

@media (max-width: 600px) {
  .item {
    width: 50%;
  }
}

.item__heading {
  background-color: #d4d0f5;
  padding: 10px;
  text-align: center;
  border: 1px solid #bbbbbb;
}

.item__content {
  flex: 1 1 auto;                   /* IE need 1 1 auto */
  padding: 10px;
  border-left: 1px solid #bbbbbb;
  border-right: 1px solid #bbbbbb;
}

.item__price {
  background-color: #e0f6d9;
  padding: 10px;
  text-align: center;
  border: 1px solid #bbbbbb;
}
<div class="items">

  <div class="item">
    <div class="item__heading">
      Item 1
    </div>
    <div class="item__content">
      Some content that is not that long
    </div>
    <div class="item__price">
      £99.99
    </div>
  </div>

  <div class="item">
    <div class="item__heading">
      Item 2
    </div>
    <div class="item__content">
      Some content that is longer than other items on the same row and sets the height of this section
    </div>
    <div class="item__price">
      £69.99
    </div>
  </div>

  <div class="item">
    <div class="item__heading">
      Item 3
    </div>
    <div class="item__content">
      Some content that is not that long
    </div>
    <div class="item__price">
      £69.99
    </div>
  </div>

  <div class="item">
    <div class="item__heading">
      Item 4
    </div>
    <div class="item__content">
      Some content that is not that long
    </div>
    <div class="item__price">
      £109.99
    </div>
  </div>

  <div class="item">
    <div class="item__heading">
      Item 5
    </div>
    <div class="item__content">
      Some content that is a medium kind of length blah blah
    </div>
    <div class="item__price">
      £29.99
    </div>
  </div>

  <div class="item">
    <div class="item__heading">
      Item 6
    </div>
    <div class="item__content">
      Some content that is not that long
    </div>
    <div class="item__price">
      £99.99
    </div>
  </div>

</div>

于 2017-05-22T18:32:12.080 回答