Imagine this layout:
[BOX] [BOX] [BOX]
[ BOX ]
Which is a flexbox design, and – if the screen shrinks – will look like this:
[ BOX ] [ BOX ]
[ BOX ]
[ BOX ]
And would become a set of 4 rows if the screen is too narrow to display even two boxes next to one another.
So far so good.
I am looking for a way to target the last element in the upper row, no matter how the flexbox has currently adjusted to its environment.
Naturally i thought of CSS pseudo-classes, but i was unable to find anything other than the typical ones, such as :last-child
and :last-of-type
or :nth-child
, which would not help in this situation.
QUESTION
Is there a way to do this purely in CSS? If yes, how?
Flex items are displayed in the same order as they appear in the source document by default. The order property can be used to change this ordering.
Flexbox is different from many other CSS properties: instead of targeting an element and acting directly upon it, we target the parent element and that controls the layout of its children. We're making a flex container and all the children of that container become flex items.
The solution is to force them by adding a collapsed row (height 0) which takes up the entire width of the container, making it occupy an entire row, thus pushing the 3rd item on the next row. Think of it like a <br> tag.
You could conceivably use a media query to target the second item after the subsequent items have wrapped. Barring that option, it's not possible with CSS.
Wrapping is not a behavior that is tracked by CSS. For example, a container has no idea when its children wrap. Therefore, there is no way to target items with CSS based on wrapping scenarios (except with media queries, as mentioned above).
More details here: Make container shrink-to-fit child elements as they wrap
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With