我想创建一个网格,但简单的表格和嵌套的 div 迫使我一次布置行。这使得为响应式设计堆叠列变得很困难。
这是一个使用弹性框创建网格的代码笔。第一个允许您一次指定行,而第二个允许您一次指定列。
https://codepen.io/chaimleib/pen/zPPGgj
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>
少:
/* 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; }
这项技术的功劳归功于 Davide Rizzo,他的article 还展示了使设计具有响应性的巧妙方法。