Do not reinvent the wheel! David DeSandro has already released a couple of jquery plugins that will do this for you. Masonry was the first and works very well, and Isotope is a newer version that has expanded in original thinking along with additional features such as sorting. Both will do the good job of creating the dynamic mesh you are hoping for.
Jason source share