You can use flexbox for many tricky layout "hacks" and implementing a grid layout is no different.

Check out the CodePen below to see how you can implement a flexbox grid system that adapts automatically based on how many items you insert per row (maximum 6 children in this example).

Live CodePen Example