Rus*_*orn 3 html css flexbox css-grid
通缉:一种仅限CSS的解决方案,可以在每行的基础上启用MULTIPLE相等高度的网格"部分",这也是响应式的.
注意:这是这个问题的后续问题,每个项目只有一个"相等高度"部分 - 可以通过flexbox实现
"项目网格"应该是响应式的 - 因为它可以根据视口宽度显示每行不同数量的卡片(桌面上4个,移动设备上2个).在给定的行中,等效的"内容"和"特征"部分应具有相同的高度.
在下面的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__features {
padding: 10px;
border-top: 1px solid #bbbbbb;
border-left: 1px solid #bbbbbb;
border-right: 1px solid #bbbbbb;
background-color: #f7cbb1;
}
.item__features ul {
margin: 0px;
}
.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__features">
<ul>
<li>feature 1</li>
</ul>
</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 as it spans many more lines than the rest of the other content sections on this row
</div>
<div class="item__features">
<ul>
<li>feature 1</li>
</ul>
</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__features">
<ul>
<li>feature 1</li> <li>feature 2</li>
<li>feature 3</li>
</ul>
</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__features">
<ul>
<li>feature 1</li>
</ul>
</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__features">
<ul>
<li>feature 1</li>
</ul>
</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__features">
<ul>
<li>feature 1</li>
<li>feature 2</li>
</ul>
</div>
<div class="item__price">
£99.99
</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
我创建了以下codepen作为基于JavaScript的解决方案,实现了预期的结果 - 但我希望尽可能用CSS解决方案替换它:http://codepen.io/rusta/pen/xdmdxm
限制
基于Flexbox的解决方案似乎无法应对这些项目有多个需要对齐的部分这一事实
我希望新的CSS Grid系统可以帮助实现上述目标,但是我已经做了几次尝试而没有运气,所以我打开了社区,看看我是否只是遗漏了一些东西
进一步说明:我说的是一个仅限CSS的解决方案,我的意思是非JS解决方案.如果HTML块需要更改(订单/嵌套/类名称)以支持非JS解决方案,这是一个可行的选择
根据您自己的答案,将它们分组为4,您也可以使用CSS Flexbox.
为了使它们在小于4的情况下表现,可以使用nth-child选择器来实现它,但是使用last*类更简单,所以我选择了后者.
有人甚至可以在没有.group_of_4包装器的情况下使用一些聪明的nth-child规则来做到这一点,但同样,为了更简单,因为它没有任何明显的限制
.items {
display: flex;
flex-direction: column;
max-width: 1200px;
}
.items .group_of_4 {
display: flex;
flex-wrap: wrap;
justify-content: space-between; /* updated */
}
.items .group_of_4 ~ .group_of_4 {
margin-top: 24px;
}
.items .group_of_4 > div {
width: calc(25% - 12px); /* updated */
box-sizing: border-box;
padding: 12px;
}
.item__heading {
background-color: #d4d0f5;
padding: 10px;
text-align: center;
border: 1px solid #bbbbbb;
order: 1;
}
.item__content {
padding: 10px;
border-left: 1px solid #bbbbbb;
border-right: 1px solid #bbbbbb;
order: 2;
}
.item__features {
padding: 10px;
border-left: 1px solid #bbbbbb;
border-right: 1px solid #bbbbbb;
background-color: #f7cbb1;
order: 3;
}
.item__price {
background-color: #e0f6d9;
padding: 10px;
text-align: center;
border: 1px solid #bbbbbb;
order: 4;
}
/* one item in a group */
.items .group_of_4 .last1 {
margin-right: calc(75% 6px); /* updated */
}
/* two items in a group */
.items .group_of_4 .last2 {
margin-right: calc(50% + 6px); /* updated */
}
/* three items in a group */
.items .group_of_4 .last3 {
margin-right: calc(25% + 6px); /* updated */
}
@media (max-width: 600px) {
.items .group_of_4 > div:nth-child(8) ~ .item__heading {
margin-top: 24px;
order: 5;
}
.items .group_of_4 > div:nth-child(8) ~ .item__content {
order: 6;
}
.items .group_of_4 > div:nth-child(8) ~ .item__features {
order: 7;
}
.items .group_of_4 > div:nth-child(8) ~ .item__price {
order: 8;
}
.items .group_of_4 > div {
width: calc(50% - 12px); /* updated */
}
/* one item in a group */
/* three items in a group */
.items .group_of_4 .last1,
.items .group_of_4 .last3 {
margin-right: 50%;
}
/* two items in a group */
.items .group_of_4 .last2 {
margin-right: 0%;
}
}Run Code Online (Sandbox Code Playgroud)
<div class="items">
<div class="group_of_4">
<div class="item__heading">
Item 1
</div>
<div class="item__content">
Some content that is not that long
</div>
<div class="item__features">
<ul>
<li>feature 1</li>
</ul>
</div>
<div class="item__price">
£99.99
</div>
<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 as it spans many more lines than the rest of the other content sections on this row
</div>
<div class="item__features">
<ul>
<li>feature 1</li>
</ul>
</div>
<div class="item__price">
£69.99
</div>
<div class="item__heading">
Item 3
</div>
<div class="item__content">
Some content that is not that long
</div>
<div class="item__features">
<ul>
<li>feature 1</li>
<li>feature 2</li>
<li>feature 3</li>
</ul>
</div>
<div class="item__price">
£69.99
</div>
<div class="item__heading">
Item 4
</div>
<div class="item__content">
Some content that is not that long
</div>
<div class="item__features">
<ul>
<li>feature 1</li>
</ul>
</div>
<div class="item__price">
£109.99
</div>
</div>
<div class="group_of_4">
<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__features">
<ul>
<li>feature 1</li>
</ul>
</div>
<div class="item__price">
£29.99
</div>
<div class="item__heading last2">
Item 6
</div>
<div class="item__content last2">
Some content that is not that long
</div>
<div class="item__features last2">
<ul>
<li>feature 1</li>
</ul>
</div>
<div class="item__price last2">
£99.99
</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1585 次 |
| 最近记录: |