使用flexbox对齐两行图像

时间:2016-05-26 13:07:51

标签: html css css3 flexbox

我正在尝试创建一个包含6个框的灵活框。

但是,由于某些原因,它们的行为不正确,因为它们的大小不同。

enter image description here

他们必须表现得像这样:

enter image description here

/* ================================= 
      Base Styles
    ==================================== */

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
.row {
  flex: 1;
}
/* ================================= 
      Media Queries
    ==================================== */

@media (min-width: 769px) {
  .main-header,
  .main-nav,
  .row {
    display: flex;
  }
  .main-header {
    flex-direction: column;
    align-items: center;
  }
  .col {
    flex: 1 50%;
  }
  .row {
    flex-wrap: wrap;
  }
  .item-2 {
    order: -1;
  }
}
@media (min-width: 1025px) {
  .main-header {
    flex-direction: row;
    justify-content: space-between;
  }
  .col {
    flex-basis: 0;
  }
  .item-1 {
    flex-grow: 1.4;
  }
}
<div class="row">

  <div class="item-1 col">
    <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
  </div><!--/.primary-->

  <div class="item-2 col">
    <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
  </div><!--/.secondary-->

  <div class="item-3 col">
    <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
  </div><!--/.tertiary-->

  <div class="item-4 col">
    <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
  </div><!--/.tertiary-->

  <div class="item-5 col">
    <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
  </div><!--/.tertiary-->

  <div class="item-6 col">
    <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
  </div><!--/.tertiary-->

</div>

你可以在这里查看JSFIDDLE:https://jsfiddle.net/ju157mnj/2/

3 个答案:

答案 0 :(得分:2)

&#13;
&#13;
.row {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
}

.col {
   flex: 0 0 calc(33.33% - 30px);   /* flex-basis adjustment for margin */
   margin: 5px;
}

img { 
    width: 100%;
}
&#13;
<div class="row">

    <div class="item-1 col">
        <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
    </div><!--/.primary-->

    <div class="item-2 col">
        <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
    </div><!--/.secondary-->

    <div class="item-3 col">
        <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
    </div><!--/.tertiary-->

    <div class="item-4 col">
        <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
    </div><!--/.tertiary-->

    <div class="item-5 col">
        <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
    </div><!--/.tertiary-->

    <div class="item-6 col">
        <img src="http://blackrockdigital.github.io/startbootstrap-freelancer/img/portfolio/cabin.png">
    </div><!--/.tertiary-->

</div>
&#13;
&#13;
&#13;

Revised Fiddle

答案 1 :(得分:0)

您的图片非常庞大,而且您的媒体查询编辑起来有点困难。 但是flex足够强大,您不需要媒体查询。下面是两个如何展示图像的例子(不确定你想要它们的方式)。

裹:

.row {
    display: flex;
    flex-wrap: wrap;
}

.col {
    flex-grow: 1;
    margin: 5px;
    text-align: center;
}

.col img {
    height: 60px;
    width: 90px;
}

小提琴:https://jsfiddle.net/awvpezbt/

每行3次(无包装):

.row {
    display: flex;
}

.col {
    flex-grow: 1;
    margin: 5px;
    text-align: center;
}

.col img {
    height: 60px;
    width: 90px;
}

小提琴:https://jsfiddle.net/awvpezbt/1/

答案 2 :(得分:0)

对您的css进行了以下更改:

img {
  max-width: 100%; //Keeps the img in its container
}
.col {
  padding: 0 1em;
  flex: 1; 
  min-width: 200px;
}
.row {
  flex: 1;
  display: flex;
  flex-wrap: wrap;
}

https://jsfiddle.net/v2tuoojd/