Sam Willis Sam Willis - 2 months ago 17
CSS Question

Flexbox: reorder and stack columns

I have three columns, which I want to swap the order of at a certain screen size.

The current order is two 1/4 width columns with a 1/2 width in between.

I want to make the two 1/4 width columns into 1/2 widths, and stack them at the beginning.

I can replicate it using floats, but can't figure out a way using flexbox.

I'm struggling to get the two 1/4 width columns to stack whilst being 50% of the parent width.

Here is a fiddle.



.col1of2 {
width: 50%;
background: red;
}
.col1of4 {
width: 25%;
background: yellow;
}
.col1of4--last {
background: blue;
}
.col-container {
display: flex;
}
@media all and (max-width: 1000px) {
.col1of2 {
order: 3;
}
.col1of4 {
order: 1;
}
}
/* css for working example */

.ex1of2 {
width: 50%;
background: red;
float: right;
}
.ex1of4 {
display: inline-block;
width: 50%;
background: yellow;
}
.ex1of4--last {
background: blue;
}

<H4>what i have</H4>
<div class="col-container">
<div class="col col1of4">
<div class="col__inner">sssadksadkaslkslasasldkasddsa</div>
<div class="col__inner">sssadksadkaslkslasasldkasddsa</div>
</div>
<div class="col col1of2">
<div class="col__inner">sssadksadkaslkslasasldkasddsa</div>
<div class="col__inner">sssadksadkaslkslasasldkasddsa</div>
<div class="col__inner">sssadksadkaslkslasasldkasddsa</div>
<div class="col__inner">sssadksadkaslkslasasldkasddsa</div>
</div>
<div class="col col1of4 col1of4--last">
<div class="col__inner">sssadksadkaslkslasasldkasddsa</div>
<div class="col__inner">sssadksadkaslkslasasldkasddsa</div>
</div>
</div>

<!-- what i want it to look like when it swaps -->

<H4>what i want to achieve</H4>
<div class="ex-container">
<div class="ex ex1of4">
<div class="ex__inner">sssadksadkaslkslasasldkasddsa</div>
<div class="ex__inner">sssadksadkaslkslasasldkasddsa</div>
</div>
<div class="ex ex1of2">
<div class="ex__inner">sssadksadkaslkslasasldkasddsa</div>
<div class="ex__inner">sssadksadkaslkslasasldkasddsa</div>
<div class="ex__inner">sssadksadkaslkslasasldkasddsa</div>
<div class="ex__inner">sssadksadkaslkslasasldkasddsa</div>
</div>
<div class="ex ex1of4 ex1of4--last">
<div class="ex__inner">sssadksadkaslkslasasldkasddsa</div>
<div class="ex__inner">sssadksadkaslkslasasldkasddsa</div>
</div>
</div>




Answer

The key to this layout is switching the flex container to column wrap in the media query.

You also have to define a height for the container, so the items know where to wrap.

.col-container {
  display: flex;
}
.col1of2 {
  width: 50%;
  background: red;
}
.col1of4 {
  width: 25%;
  background: yellow;
}
.col1of4--last {
  background: blue;
}
@media all and (max-width: 1000px) {
  .col-container {
    flex-direction: column;
    flex-wrap: wrap;
    height: 100px;
  }
  .col1of2 {
    order: 1;
  }
  .col1of4 {
    width: 50%;
  }
}
<div class="col-container">
  <div class="col col1of4">
    <div class="col__inner">sssadksadkaslkslasasldkasddsa</div>
    <div class="col__inner">sssadksadkaslkslasasldkasddsa</div>
  </div>
  <div class="col col1of2">
    <div class="col__inner">sssadksadkaslkslasasldkasddsa</div>
    <div class="col__inner">sssadksadkaslkslasasldkasddsa</div>
    <div class="col__inner">sssadksadkaslkslasasldkasddsa</div>
    <div class="col__inner">sssadksadkaslkslasasldkasddsa</div>
  </div>
  <div class="col col1of4 col1of4--last">
    <div class="col__inner">sssadksadkaslkslasasldkasddsa</div>
    <div class="col__inner">sssadksadkaslkslasasldkasddsa</div>
  </div>
</div>

revised fiddle

Comments