2

I would like to switch the 3rd and 4th column of my grid on desktop.

I know that I could assign grid-order to each item or use a own row for two of the divs and then set the order, but I would like to avoid both to reduce code.

Is there any way to achieve this with css-grid? I am open for solutions via css-flex, if that would be easier.

My current code looks like this:

.row{
  display: grid;
  grid-template-columns: 1fr;
}

@media screen and (min-width: 1200px) {
  .row{
    grid-template-columns: 1fr 1fr
  }
}

.row div{border:1px solid black}
  <div class="row">
      <div>[IMG]</div>
      <div>Text</div>
      <div>[IMG]</div>
      <div>Text</div>
      <div>[IMG]</div>
      <div>Text</div>
  </div>

1

2 Answers 2

2

You can use order property with a flex or grid container. If you don't want to use order, you can play with grid-column and grid-row like below.

.row {
  display: grid;
  grid-template-columns: 1fr;
}

@media screen and (min-width: 1200px) {
  .row {
    grid-template-columns: 1fr 1fr
  }
  .row>div:nth-child(3) {
    grid-column: 2 / 3;
  }
  .row>div:nth-child(4) {
    grid-column: 1 / 2;
    grid-row: 2 / 3;
  }
}

.row div {
  border: 1px solid black
}
<div class="row">
  <div>[IMG]</div>
  <div>Text</div>
  <div style="background: red">[IMG]</div>
  <div style="background: yellow">Text</div>
  <div>[IMG]</div>
  <div>Text</div>
</div>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. Works like a charm. I need to wait a bit longer to accept the answer :)
0

You can simply do like below

.row{
  display: grid;
  grid-auto-columns: 1fr;
  grid-auto-flow:dense;
}

@media screen and (min-width: 1200px) {
  .row div:nth-child(3) {
     grid-column:2;
  }
}

.row div{border:1px solid black}
<div class="row">
      <div>[IMG]</div>
      <div>Text</div>
      <div>[IMG]</div>
      <div>Text</div>
      <div>[IMG]</div>
      <div>Text</div>
  </div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.