3列flexbox在某些小型设备上无法正确堆叠

时间:2017-02-07 21:31:15

标签: html css flexbox

首先,我是业余爱好者 - 所以希望你能原谅!我正在尝试实现一个3列的flexbox布局(我只选择相同的高度div),它会自动堆叠在小型设备上。我已经尝试了我能想到的一切,但结果是前两个div列在同一行上保持在一起,而第三个div列包含在下一行。我在CSS中添加了第4个孩子,因为我最初想要4列,但删除这个对我的问题没有任何影响。我读过载荷,似乎无法找到答案。也许有人有类似的问题?

这是CSS:

.flexbox {
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  flex-wrap: wrap;
  align-items: stretch;

}


.flexbox .col {
  flex: 1;
  margin: 10px;
}

.flexbox .coltext {
  flex: 1;
  padding: 15px; 
  margin: 10px;
  background: #fff;
   box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19); 

}
.flexbox .col:nth-child(1) {
  background: #fff;
  -webkit-order: 0;
  -ms-flex-order: 0;
  order: 0;
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
}
.flexbox .col:nth-child(2) {
  background: #fff;
   -webkit-order: 1;
  -ms-flex-order: 1;
  order: 1;
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
}
.flexbox .col:nth-child(3) {
  background: #fff;
  -webkit-order: 2;
  -ms-flex-order: 2;
  order: 2;
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
}

.flexbox .col:nth-child(4) {
  background: #fff;
  -webkit-order: 3;
  -ms-flex-order: 3;
  order: 3;
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
}


@media (max-width: 480px) {
    #container div {
        max-width: 98%;
    }
}
@media (min-width: 480px) and (max-width: 1080px) {
    #container div {
        max-width: 48%;
    }
}
@media (min-width: 1080px) {
    #container div {
        max-width: 23%;
    }
}

这是html(删除了描述性文字):

<div class="flexbox">
  <div class="col">
   <img src="images/homepage_images/OVERHEAD (4).jpg" style="width:100%"><br><br>
        <h5><center><strong>Bournemouth Town Centre regeneration</strong></center></h5>
    <p class="justify text-justify w3-padding"> 
<br><br>
     <a class="w3-btn w3-red w3-medium" href="#">learn more..</a></p>
  </div>


  <div class="col">
  <img src="images/homepage_images/Poundstock.jpg" style="width:100%"><br><br>
    <h5><center><strong>contract awards</strong></center></h5>
       <p class="justify text-justify w3-padding"> <br><br>
      <a class="w3-btn w3-red w3-medium" href="#">learn more..</a></p>
  </div>



  <div class="col">
   <img src="images/homepage_images/_MG_0342rawprepx.jpg" style="width:100%"><br><br>
        <h5><center><strong>latest from General Works</strong></center></h5>
       <p class="justify text-justify w3-padding">
  <br><br> <a class="w3-btn w3-red w3-medium" href="#">view</a></p></div>
</div>

1 个答案:

答案 0 :(得分:1)

实现预期效果的一种简单方法是将flex-direction设置为column

@media (max-width: 480px) {
    #container div {
        max-width: 98%;
    }
   .flexbox {
     flex-direction: column;
   }
}

SNIPPET(缩小输出窗口)

.flexbox {
  display: -webkit-flex;
  display: -ms-flexbox;
  display: flex;
  flex-wrap: wrap;
  align-items: stretch;

}


.flexbox .col {
  flex: 1;
  margin: 10px;
}

.flexbox .coltext {
  flex: 1;
  padding: 15px; 
  margin: 10px;
  background: #fff;
   box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19); 

}
.flexbox .col:nth-child(1) {
  background: #fff;
  -webkit-order: 0;
  -ms-flex-order: 0;
  order: 0;
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
}
.flexbox .col:nth-child(2) {
  background: #fff;
   -webkit-order: 1;
  -ms-flex-order: 1;
  order: 1;
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
}
.flexbox .col:nth-child(3) {
  background: #fff;
  -webkit-order: 2;
  -ms-flex-order: 2;
  order: 2;
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
}

.flexbox .col:nth-child(4) {
  background: #fff;
  -webkit-order: 3;
  -ms-flex-order: 3;
  order: 3;
  box-shadow: 0 8px 16px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
}


@media (max-width: 480px) {
    #container div {
        max-width: 98%;
    }
  .flexbox {
    flex-direction: column;
  }
}
@media (min-width: 480px) and (max-width: 1080px) {
    #container div {
        max-width: 48%;
    }
}
@media (min-width: 1080px) {
    #container div {
        max-width: 23%;
    }
}
<div class="flexbox">
  <div class="col">
   <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150" style="width:100%"><br><br>
        <h5><center><strong>Bournemouth Town Centre regeneration</strong></center></h5>
    <p class="justify text-justify w3-padding"> 
<br><br>
     <a class="w3-btn w3-red w3-medium" href="#">learn more..</a></p>
  </div>


  <div class="col">
  <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150" style="width:100%"><br><br>
    <h5><center><strong>contract awards</strong></center></h5>
       <p class="justify text-justify w3-padding"> <br><br>
      <a class="w3-btn w3-red w3-medium" href="#">learn more..</a></p>
  </div>



  <div class="col">
   <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=350%C3%97150&w=350&h=150" style="width:100%"><br><br>
        <h5><center><strong>latest from General Works</strong></center></h5>
       <p class="justify text-justify w3-padding">
  <br><br> <a class="w3-btn w3-red w3-medium" href="#">view</a></p></div>
</div>