I’m working on a web app where a large number of thumbnails will be displayed. I would like to achieve the behavior used in iTunes when displaying albums as thumbnails in a grid (not Coverflow). The idea is that the thumbnails have a fixed size, while the container div has a fluid width. As many thumbnails as possible should be fit in one row, and the margin between the thumbnails should be adaptive so that the thumbnails always take up 100% width of the container.
See the two images below:
Four thumbnails making use of the full width
If it is possible to achieve this with CSS, that is preferable, otherwise I would appreciate JavaScript/jQuery solutions as well.
Made some small improvements to the script given by Alex, to fully suit my needs. Now the first thumbnail on each row doesn’t get a left-margin, and the last thumbnail on each row doesn’t get a right-margin, so that the thumbnails really make us of the full width of the container div. Also used jQuery’s .outerWidth() instead of .width() to retrieve the width of the thumbnail, so that you can use borders etc. without compromising the calculation. Now the script also run as soon as the DOM is loaded, to calculate the proper margin from the beginning, and not only when the window is re-sized.
Here’s the new script: