I've been looking for a way to do this for some time, and I haven't yet come across anything. What I'm after is to be able to display a series of pictures in a grid layout, ordered from left to right then up and down. I want it to take up as much space as is available on each row (whilst adhering to certain conditions such as padding between images) and when the window is re-sized, it will automatically re-calculate where the images should be positioned.
I put together the following graphic to help aid what I'm trying to get at:

Is there any easy way to do this in CSS, HTML5 and/or JavaScript?
Thanks, Josh.