css - 即使是左对齐的 Flexbox 产品网格

标签 css layout flexbox

我正在尝试获得一个漂亮的 Flexbox 网格以用于电子商务产品,但无法完全按照我的意愿进行。

这是一个演示 http://jsbin.com/acejes/9/edit

这可能是不可能的,我只是想看看还有什么我可以做的。

这是我的目标......

  1. 必须是基于百分比的网格
  2. 第一列和最后一列与容器的侧面齐平
  3. 最后一行向左“ float ”

我的问题有点类似于 How to align left last row/line in multiple line flexbox ,但我特别想将 %s 用于“列”——我认为它更整洁,这意味着我知道如果我使用 32.5% 之类的 %,我将连续 3 列

我快到了,但是由于 justify-content 属性,最后一行被扔掉了。我希望最后一行向左“ float ”:

Almost, except the last row

代码

在上面的jsbin中更容易看到我的代码,但为了保存,这里放一个小截图:

<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%;
}

最佳答案

我不认为这种精确的解决方案可以通过 Flexbox 实现,但您可以使用高级选择器来代替,就像这样 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>

关于css - 即使是左对齐的 Flexbox 产品网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18218668/

相关文章:

javascript - 修复 jQuery 动画的背景问题

java - 刷新 JPanel-移动 JLabel

html - Bootstrap 3 的最佳实践方法

html - 在 Bootstrap 中相邻的 div 之间创建间隙

javascript - 防止标签点击页面跳转JS

android - PagerAdapter 中的 instantiateItem(ViewGroup, int) 和 ViewPager 中的 AddView 混淆

xaml - 网格不占用 100% 宽度

html - 使用 flex 在没有设置宽度的情况下向下推送内容

html - 如何在全屏上拉伸(stretch) flexbox?

css - 如何使文本与图标在同一行?