Using CSS to make a liquid grid of uniformly sized divs with margins

Tim Mahoney picture Tim Mahoney · May 28, 2012 · Viewed 8.3k times · Source

How would you make a liquid grid of elements with uniform size and margins in CSS?

The main problem I have is the margins. I feel that the obvious solution is to set the widths and margins to a percentage and be done with it. This doesn't work because the vertical margin is a percentage of the container's width. When the container resizes horizontally, the vertical spacing is off.

I have tried two methods. One with percentage margins and one with 'em' margins. Neither of them scale correctly. What can I do?

Answer

saluce picture saluce · May 28, 2012

Are you looking for something like this? http://jsfiddle.net/LMJTu/

On the fiddle, click the button to change the size of the outer div.