I'm trying to achieve the following layout with CSS, each number being a block of content of equal width:
Mobile view:
1
2
3
4
5
6
7
8
Desktop view:
1 | 2
-----
4 | 3
-----
5 | 6
-----
8 | 7
Note that 3-4 and 7-8 are reversed in desktop layout.
As 3-4 and 7-8 aren't semantically linked, I'd rather not couple them together with a parent HTML element (so display:table layout is likely not an option).
In desktop view, element 1 needs to be the same height as element 2, and 3 the same as 4 etc, but the height is not known in advance and I don't want to use JavaScript (so floats are out as they would be different heights, unless there was a way of using CSS calc).
So, assuming 1-8 are all section elements from the same parent div, how could this be achieved without JavaScript?
I'm not sure there is a solution but think this is something CSS should be able to support - so answers can include draft or largely unsupported CSS.
Flexbox could do this, but it requires knowing in advance exactly how many elements there are.
http://codepen.io/cimmanon/pen/EDzGt
Be aware that this will only work in browsers that support Flexbox and wrapping. Current browser support: Chrome, Opera, IE10, Blackberry 10 (Firefox will have it soon).
http://caniuse.com/#search=flexbox
As per cimmanon's answer, and my subsequent comments, I was able to use a combination of flexbox, wrapping and relative positioning.
CSS:
HTML:
http://codepen.io/penx/pen/fnrJE
float 3, 4, 7 and 8 right like here: http://jsfiddle.net/KH584/