I’m trying to create an autoflow component to re-arrange content according to screen size. The content blocks always come in pairs: a text box and an image.
On a desktop device, I want the layout to be
| Text1 | Img1 |
| Img2 | Text2 |
| Text3 | Img3 |
| Img4 | Text4 |
... unlimited number of blocks
on the mobile I always want the text before the image
| Text1 |
| Img1 |
| Text2 |
| Img2 |
| Text3 |
| Img3 |
...
I have been using a grid container
.lrcontainer {
display: grid;
width: 100%;
align-self: start;
grid-auto-flow: row;
align-items: center;
justify-items: center;
}
and set the number of columns using media queries. I put the individual text and img blocks into the right order for the desktop view and tried to reorder them for mobile view by using css nth-child and the order property without any success.
I’m beginning to think, that this simply cannot be achieved, and that I need to put the text img pair inside an additional container. Am I right, or does anybody have a nifty trick to implement this type of layout?
2
Answers
Set
grid-auto-flow: dense;
ongrid
container, search cells with:nth-child
and use on themgrid-column
:A simplified version of @imhvost answer. I have a article where I detail such a technique: https://css-tricks.com/exploring-css-grids-implicit-grid-and-auto-placement-powers/