4

I'm trying to push the limits of CSS to replicate what would be a common grid-layout in print.

Requirements :

  1. Margins between blocks and between blocks and edge of the container must be equal.
  2. The layout must be responsive and the number of blocks on each row must adapt to the size of the window.
  3. The last row must be left aligned
  4. the width/height of the blocks is fixed
  5. no use of empty non-semantic HTML elements
  6. pure CSS solution, no JS

So, I have markup that looks like this:

<ul>
    <li>
       <img src="thumbnail.jpg">
       <span>Introduction and Curriculum</span>
    </li>
    <li>
       <img src="thumbnail.jpg">
       <span>Equipment and Workspace Prep</span>
    </li>
    ...
</ul>

Here is a mock-up of what I'm going for.

grid layout with equal margins

web-tiki
  • 99,765
  • 32
  • 217
  • 249
Ryan Norbauer
  • 1,718
  • 2
  • 17
  • 26
  • 2
    is this the kind of layout you are trying to achieve? http://jsfiddle.net/webtiki/KrA6M/ – web-tiki May 21 '14 at 19:35
  • Sort of? Here is an interactive version of what I'm going for. http://codepen.io/norbauer/pen/bLCpa Unfortunately, it uses a hack that requires inserting empty `
  • ` elements, which I need to avoid. I am considering inserting them via JS, but I would first like to know if what I'm proposing is possible via more conventional CSS. The code proposed in that JSFiddle is even more obtuse. :) (though it does at least have the advantage of leaving the DOM alone.)
  • – Ryan Norbauer May 21 '14 at 20:34
  • Have you already tried floats and `nth-child` for clearing + margin fixes? Those can be changed at different breakpoints. – Will May 21 '14 at 21:28
  • Floats and fixes are things I want to avoid (I consider floats to be properly used for floating images within blocks of text only, not larger-scale layout.) I should perhaps clarify that the point of my question is to see whether there is a proper elegant approach to this in CSS. I don't care about just getting it done, as I know a number of easy hacks (such as adding dummy empty list items with a particular width and using flexbox.) I'm just trying to learn CSS best-practices here. – Ryan Norbauer May 21 '14 at 22:40
  • 1
    You can also take a look at these 2 answers for a grids with close requirements : http://stackoverflow.com/a/23803215/1811992 and http://stackoverflow.com/a/20457076/1811992 – web-tiki May 22 '14 at 23:29