Wanted:仅 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 网格系统能够帮助我实现上述目标,但使用了一段时间后,它似乎需要比我希望的更多的结构,并且响应方面似乎相当具有挑战性。但也许某个地方有一个基于 CSS 网格的答案
进一步说明:我说的是纯 CSS 解决方案,我指的是非 JS 解决方案。如果 HTML 块需要更改(顺序/嵌套/类名称)以支持非 JS 解决方案,那绝对没问题
在这个简单的例子中 - 我们只关注“内容”部分以获得“匹配高度” - 因为我们可以假设标题和价格部分自然具有相同的高度。最好在任何匹配的网格部分(标题/内容/价格/其他)中启用“等效性”,但这可能是另一天的事情......
通过给予.items
display: flex; flex-wrap: wrap;
your 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>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)