Rus*_*orn 5 html css flexbox css-grid
想要:一个 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;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
以下 codepen 是基于 JavaScript 的解决方案,可实现预期的结果 - 但这是我试图避免的:https : //codepen.io/rusta/pen/KmbVKd
限制
我希望新的 CSS Grid 系统能够帮助我实现上述目标,但是使用它一段时间后,它似乎需要比我希望的更多的结构,而且响应方面似乎相当具有挑战性。但也许某处有一个基于 CSS Grid 的答案
进一步说明:我说的是仅 CSS 解决方案,我指的是非 JS 解决方案。如果 HTML 块需要更改(顺序/嵌套/类名)以支持绝对没问题的非 JS 解决方案
在这个简单的例子中 - 我们只关注具有“匹配高度”的“内容”部分 - 因为我们可以假设标题和价格部分自然具有相同的高度。在任何匹配的网格部分(标题/内容/价格/其他)中启用“等效性”会很好,但这可能是另一天......
通过给.items display: flex; flex-wrap: wrap;你的itemwill 变成 flex items 并从左到右流动并在没有更多空间时换行。
然后你给.item display: flex; flex-direction: column;,这将使它们成为一个 flex 容器,并且通过使用列方向,它的子元素将垂直流动,就像块元素一样。
最后你给出.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;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2699 次 |
| 最近记录: |