So, I need divs to align horizontally and fill in the spaces left in the previous line, for example:
Can this be achieved?
There is no way to do this automatically with pure CSS, but Javascript can help you. There is a good library called Masonry that does exactly what you want.