I initially wanted to structure a grid, however, the use of basic tables and nested divs made it necessary to lay out rows sequentially. This hindered my ability to arrange columns effectively for a responsive layout.
Fortunately, I found a solution using flexboxes to create grids. The first approach allows you to define rows at a time, while the second method enables you to specify columns individually.
Check out this CodePen
HTML:
<h2>By row</h2>
<div class="table table3cols">
<div class="table-cell"><h3>Eddard Stark</h3></div>
<div class="table-cell">Sword named Ice</div>
<div class="table-cell">No direwolf</div>
<div class="table-cell"><h3>Jon Snow</h3></div>
<div class="table-cell">Sword named Longclaw</div>
<div class="table-cell">Direwolf: Ghost</div>
<div class="table-cell"><h3>Arya Stark</h3></div>
<div class="table-cell">Sword named Needle</div>
<div class="table-cell">Direwolf: Nymeria</div>
</div>
<h2>By column</h2>
<div class="table table3cols">
<div style="order:1;" class="table-cell"><h3>Eddard Stark</h3></div>
<div style="order:2;" class="table-cell">Sword named Ice</div>
<div style="order:3;" class="table-cell">No direwolf</div>
<div style="order:1;" class="table-cell"><h3>Jon Snow</h3></div>
<div style="order:2;" class="table-cell">Sword named Longclaw</div>
<div style="order:3;" class="table-cell">Direwolf: Ghost</div>
<div style="order:1;" class="table-cell"><h3>Arya Stark</h3></div>
<div style="order:2;" class="table-cell">Sword named Needle</div>
<div style="order:3;" class="table-cell">Direwolf: Nymeria</div>
</div>
LESS:
/* Variables
================================== */
@bw: 3px; // border width
/* Tables
================================== */
.table {
display: flex;
flex-wrap: wrap;
margin: 0 0 3em 0;
padding: 0;
}
.table-cell {
box-sizing: border-box;
flex-grow: 1;
width: 100%; // Default to full width
padding: 0.8em 1.2em;
overflow: hidden; // Or flex might break
list-style: none;
border: solid @bw white; // margins would overflow row and cause early wrapping
background: fade(slategrey,20%);
> h1, > h2, > h3, > h4, > h5, > h6 { margin: 0; }
}
/* Table column sizing
================================== */
.table2cols > .table-cell { width: 50%; }
.table3cols > .table-cell { width: 33.33%; }
.table4cols > .table-cell { width: 25%; }
/* Page styling
================================== */
body {
border: 1px solid grey;
margin: 0 auto;
max-width: 800px;
padding: 2em;
}
h1, h2, h3, h4, h5, h6 { margin-top: 0; }
Kudos to Davide Rizzo for introducing this technique in his informative article. He also shares useful tips on enhancing design responsiveness.