Marc Marc - 4 months ago 40
CSS Question

Flexbox: Two elements on top of each other in flex-direction: row

I am trying to achieve the following:






My first attempt was to use a helper div (green):



JSFiddle

What I could do here, is using JavaScript to move the puple and orange elements out of the helper on mobile screens. But there has to be a plain css way.




My second attempt was to remove the helper and build the Mobile Layout:



JSFiddle




Is there a way to place two elements on top of each other in
flex-direction: row
? (second attempt)

Answer

You could do this with Flexbox but you need to use fixed height on flex container. Here is Fiddle

Basically you use flex-wrap: wrap with flex-direction: column and make first item take 100% height and set width in %. Then you change order with media queries and height.

* {
  box-sizing: border-box;
}
main,
div {
  display: flex;
  padding: 1rem;
}
.desktop {
  flex-direction: column;
  flex-wrap: wrap;
  height: 400px;
  width: 100%;
}
div {
  flex: 1;
  width: 30%;
}
[orange] {
  background-color: #FFAD77;
  flex: 0 0 70%;
}
[yellow] {
  background-color: #FFE377;
  flex: 0 0 100%;
  width: 70%;
}
[purple] {
  background-color: #FF77C8;
}
@media(max-width: 480px) {
  .desktop div {
    flex: 1;
    width: 100%;
  }
  div[orange] {
    order: -1;
    flex: 2;
  }
  div[yellow] {
    flex: 5;
  }
  div[purple] {
    flex: 1;
  }
}
<div class="desktop">
  <div yellow>lorem</div>
  <div orange>lorem</div>
  <div purple>lorem</div>
</div>