Odd number of cards
If I have a 2 column layout with an unknown number of cards, how would I make the last one spans the two columns if the space is empty?
Going a step further, if as in some of Kevin
s tutorials I had some
feature` cards that span 2 columns, how would I ensure the last card spans 2 columns if there is space?
Many thanks8 Replies
for your first question, if the last child is odd (in a 2 column grid)
I think you can achieve both results with the following:
If there are an even number of cards, or an even number of cards in the last row, this won't affect anything, because the last-child will already end at -1, but if it's a singleton, this will make it span all remaining columns.
I`ll give the a try
@londonpete_harrison
i visited back the second part of your question, and i think i came up with a selector that might work for you. It's quite a big one and does rely on the :has selector
here you can test it out https://codepen.io/MarkBoots/pen/YzmwaJr?editors=1100
This is completely unnecessary though. It doesn't matter whether it's odd or even or whether there are featured cards elsewhere, that's a distraction, and selecting for it makes your code harder to read, raises specificity and lowers browser support.
All you actually want is for the last card to span the width if its a standalone, so just make the last card end at the last column line.
As an exercise, it's interesting, and the solution you've come up with is cool, but for production, it's overkill.
only setting
grid-column-end: -1
does not work. It will not span the item over the columns, it will just place it at the last columnAh damn, well there goes my theory! 😅 apologies, this is what I get for answering on my phone and not testing my theories