I’m trying to build a table-like layout with 2 columns where only some (few of) the cells have actually any data. About 50%-75% of the “cells” are gonna be blank/not existant, so I would like to accomplish this using divs if possible.
For example, for the following HTML:
<div id="wrapper">
<div id="left">
<div class="p1">l1</div>
<div class="p3">l3</div>
<div class="p4">l4</div>
<div class="p5">l5</div>
</div>
<div id="right">
<div class="p1">r1</div>
<div class="p2">r2</div>
<div class="p3">r3</div>
<div class="p5">r5</div>
</div>
</div>
“Cell” l1 should be at the same vertical position as r1, l3 as r3, l5 as r5, like this:
l1 r1
r2
l3 r3
l4
l5 r5
I’ve been unable to accomplish this, so far I’ve tried:
- wrapper with position:relative, “cells” with position:absolute and positioning them with top: xxpx;
- floats
But nothing is working for me. Is there any way of doing this without actually filling in all the div “cells”, even if they are blank? That would really be like using a table, which is the only solution that is working for me at the moment.
Cool. In that case, I’m going to suggest
display: table-celland friends.It won’t work in IE7, but that won’t be a problem if you only care about the latest browsers.
I don’t particularly like the “divitus”, but there’s little that can be done about it.
JSFiddle
CSS
HTML
In that case, you can also consider something simpler, and closer to what you originally had:
JSFiddle
CSS
HTML
A third idea, specifically catering to:
You can omit empty cells now.
Personally, I’d rather just put up with having empty
divs, because this is a little complicated.I commented out rather than remove the “empty cells”.
JSFiddle
CSS
HTML
This should be a reasonably extensive HTML test case.