Reputation: 1477
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>
Upvotes: 2
Views: 2319
Reputation: 272937
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>
Upvotes: 0
Reputation: 27421
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>
Upvotes: 2