Here is a solution Masonry Cascading grid layout library.
Masonry is a JavaScript grid layout library. It works by placing elements in optimal position based on available vertical space, sort of like a mason fitting stones in a wall. You’ve probably seen it in use all over the Internet.
You can go with this. Check the link above.
Hope this will help you. 🙂
solved How to loop different size of images in while clause in php [closed]