即使Flexbox产品网格与左对齐 [英] Even Flexbox product grid with left alignment
问题描述
这里是一个用于电子商务产品的Flexbox网格,演示
< div class =l-col_33 - all>
< div class =l-col l-col_33>
< div class =l-col l-col_50>
< img src =http://placehold.it/350x150title =alt =/>
< / div>
< div class =l-col l-col_50>
< p class =product-title>第一个产品装饰粉红高跟鞋< / p>
< p class =product-price> $ 25.99< / p>
< p class =product-title>装饰粉红色高跟鞋< / p>
< / div>
< / div>
< div class =l-col l-col_33>
< div class =l-col l-col_100>
< img src =http://placehold.it/350x150title =alt =/>
< / div>
< div class =l-col l-col_100>
< p class =product-title>第二件产品装饰粉红高跟鞋< / p>
< p class =product-price> $ 25.99< / p>
< / div>
< / div>
< div class =l-col l-col_33>
< div class =l-col l-col_50>
< img src =http://placehold.it/350x150title =alt =/>
< / div>
< div class =l-col l-col_50>
< p class =product-title>第三届产品装饰粉红高跟鞋< / p>
< p class =product-price> $ 25.99< / p>
< / div>
< / div>
< / div>
< style>
img {
max-width:100%;
}
body {
border:1px solid black;
}
p {
padding-right:10px;
padding-left:10px;
}
/ *例如在div包装器上使用--all修饰符类来将列对齐到容器中* /
.l-col_33 - all .l-col_33 {
width:32%;
margin-bottom:40px;
background-color:red;
}
.l-col_33 - all .l-col_33:nth-child(1),
.l-col_33 - all .l-col_33:nth-孩子(3n + 1){
margin-right:2%;
border-bottom:3px solid green;
}
.l-col_33 - all .l-col_33:nth-child(3),
.l-col_33 - all .l-col_33: {
margin-left:2%;
border-bottom:3px solid blue;
}
/ *清除行* /
.l-col_33 - all .l-col_33:nth-child(3n + 1){
clear:剩下;
}
.l-col_33 - all:after {/ * clearfix * /
content:。
display:block;
clear:both;
visibility:hidden;
line-height:0;
height:0;
}
.l-col {
position:relative;
float:left;
}
.l-col_33 {
width:33%;
}
< / style>
I'm trying to get a nice Flexbox grid to use with ecommerce products, but can't quite get it to do what I want.
Here is a demo http://jsbin.com/acejes/9/edit
It may not be possible, I just wanted to check if there's anything else I can do.
Here is my aim…
- Must be a percentage based grid
- The first and last column flush against the sides of the container
- The last row "floats" left
My question is kind of similar to How to align left last row/line in multiple line flexbox, but I specifically want to use %s for the "columns" — I think it's neater and it means I know I'm going to have, say 3 columns in a row if I use a % like 32.5%
I'm almost there, but the last line is being thrown out because of the justify-content property. I'd like the last row to be "floated" left:
Code
It's easier to see my code in the jsbin above, but for the sake of preservation, here is a small snapshot:
<div id="flexbox">
<div class="column">
<img src="http://placehold.it/350x150" title="" alt="" />
</div>
<div class="column">
<p class="product-title">Decorated Pink High Heels</p>
<p class="product-price">$25.99</p>
<p class="product-title">Decorated Pink High Heels</p>
</div>
</div>
CSS
* {
box-sizing: border-box;
}
#flexbox {
display: flex;
flex-flow: row wrap;
width: 100%;
justify-content: space-between;
border: 3px solid black;
}
#flexbox .column {
width: 22%;
margin-bottom: 30px;
background-color: red;
}
img {
max-width: 100%;
}
I don't think this exact solution can be achieved with Flexbox, but you can use advanced selectors instead like this http://jsbin.com/acejes/14/edit
<div class="l-col_33--all">
<div class="l-col l-col_33">
<div class="l-col l-col_50">
<img src="http://placehold.it/350x150" title="" alt="" />
</div>
<div class="l-col l-col_50">
<p class="product-title">1st product Decorated Pink High Heels</p>
<p class="product-price">$25.99</p>
<p class="product-title">Decorated Pink High Heels</p>
</div>
</div>
<div class="l-col l-col_33">
<div class="l-col l-col_100">
<img src="http://placehold.it/350x150" title="" alt="" />
</div>
<div class="l-col l-col_100">
<p class="product-title">2nd product Decorated Pink High Heels</p>
<p class="product-price">$25.99</p>
</div>
</div>
<div class="l-col l-col_33">
<div class="l-col l-col_50">
<img src="http://placehold.it/350x150" title="" alt="" />
</div>
<div class="l-col l-col_50">
<p class="product-title">3rd product Decorated Pink High Heels</p>
<p class="product-price">$25.99</p>
</div>
</div>
</div>
<style>
img {
max-width: 100%;
}
body {
border: 1px solid black;
}
p {
padding-right: 10px;
padding-left: 10px;
}
/* E.g. Use an "--all" modifier class on a div wrapper to flush columns against their container */
.l-col_33--all .l-col_33 {
width: 32%;
margin-bottom: 40px;
background-color: red;
}
.l-col_33--all .l-col_33:nth-child(1),
.l-col_33--all .l-col_33:nth-child(3n+1) {
margin-right: 2%;
border-bottom: 3px solid green;
}
.l-col_33--all .l-col_33:nth-child(3),
.l-col_33--all .l-col_33:nth-child(3n) {
margin-left: 2%;
border-bottom: 3px solid blue;
}
/* Clear rows */
.l-col_33--all .l-col_33:nth-child(3n+1) {
clear: left;
}
.l-col_33--all:after { /* clearfix */
content: ".";
display: block;
clear: both;
visibility: hidden;
line-height: 0;
height: 0;
}
.l-col {
position: relative;
float: left;
}
.l-col_33 {
width: 33%;
}
</style>
这篇关于即使Flexbox产品网格与左对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!