I am trying to create a display blog post component with Flexbox that primarily uses justify-content: space-between
. In a 4-column layout, it looks great when there are 4 posts to fill the row. The issue is when there are not 4 posts, but 2 or 3. At this point, the posts get spread out and it just looks sloppy. I want to use flexbox without any JavaScript, and I want to be able to have the layout be fully responsive. So at other breakpoints the column layout will drop to 3, 2, or even 1.
One of the things that I found that works great is adding an :after
pseudo element to the wrapper with a set width that will fill in the empty space. The problem with that is it is not dynamic enough and will not work properly if I have a different number of post then the one I planned for.
For example: If I have 2 posts, each at 25% width, and then a pseudo element at 50% width, everything looks great. Once I have 3 posts it is not so great because the 50% pseudo element now needs to be 25%. Finally, if there is 4 I wouldn't need a pseudo element at all.
I am trying to work with using some of the SASS selectors, but for some reason when I try to target the parent with my :after
element, it targets something else way up the DOM tree.
.content {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
.entry:nth-child( 2 ):last-of-type {
:after & {
content: "";
flex-grow: 0;
width: 50%;
}
}
.entry:nth-child( 3 ):last-of-type {
:after & {
content: "";
flex-grow: 0;
width: 50%;
}
}
}
At this point I expected the :after element to target the parent class "content" but it doesn't. I also didn't think about how I would adjust it to be more dynamic and be able to handle multiple rows. I guess I could just do the math and make it something like .entry:nth-child( 3n+4 ):last-of-type {}
, but I haven't even gotten there yet.