JanD
JanD

Reputation: 11

3-column flexbox not stacking properly on some small devices

Firstly, I'm a rank amateur - so hope you will forgive! I'm trying to achieve a 3-column flexbox layout (I chose this only for the equal height divs), which automatically stacks on small devices. I've tried everything I can think of, but the result is that the first two div columns remain together on the same line, while the third div column wraps to the next line. I added a 4th child to the CSS as i initially wanted 4 columns, but deleting this made no difference to the issue I have. I've read loads and can't seem to find the answer. Maybe there is someone out there with a similar issue?

This is the 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%;
    }
}

This is the html (with the descriptive text removed):

<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>

Upvotes: 0

Views: 123

Answers (1)

Ramiz Wachtler
Ramiz Wachtler

Reputation: 5683

A simple way to achieve the desired effect would be the setting of flex-direction to column :

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

SNIPPET (Shrink the output window)

.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>

Upvotes: 1

Related Questions