I am using css flexbox to distribute a number of items in a container. I have a flex container like this:
.container{
display: flex;
flex-flow: row wrap;
What you're looking for is sometimes referred to as Quantity Queries.
If media queries allow for breakpoints based on viewport dimensions, quantity queries allow for breakpoints based on content quantity (e.g., sibling count).
It's not part of any official standard but I wouldn't call it a hack. The code can be quite elegant. It is a bit involved, however. Here you go: