Trying to get responsive web layout for grid design

Viewed 19

I have a grid layout with a large image on left and two stacked images in the right column. on a full layout, it looks like I want.

I used the following layout. I have images instead of colors, so have left the image settings in the css. I am trying to place block3 in the first position on a responsive layout (max width 768) and then stack the three images in one column. is this possible?

.container {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  height: 600px;
  width: 100%
}

.container div {
  color: #fff;
  text-align: center;
}

.block1 {
  grid-row: 1 / 5;
  grid-column: 1 / 3;
  background-color: green;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
}

.block2 {
  grid-row: 1 / 3;
  grid-column: 3 / 3;
  background-color: blue;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  height: 300px;
}

.block3 {
  grid-row: 3 / 5;
  grid-column: 3 / 3;
  background-color: coral;
  background-position: center;
  background-repeat: no-repeat;
  background-size: cover;
  height: 300px;
}
<div class="container">
  <div class="block1"></div>
  <div class="block2"></div>
  <div class="block3"></div>
</div>

1 Answers

Just use the order property to change the ordering. And then change the columns to 1.

.container {
  display: grid;
  grid-auto-rows: 1fr;
  height: 600px;
}

.block1 {
  background-color: green;
}

.block2 {
  background-color: blue;
}

.block3 {
  background-color: coral;
}

@media only screen and (min-width: 769px) {
  .container {
    grid-template-columns: repeat(3, 1fr);
  }
  .block1 {
    grid-column: span 2;
    grid-row: span 2;
}
  
@media only screen and (max-width: 768px) {
  .block1 {
    order: 2;
  }
  .block2 {
    order: 3;
  }
  .block1 {
    order: 1;
  }
}
<div class="container">
  <div class="block1"></div>
  <div class="block2"></div>
  <div class="block3"></div>
</div>

Related