flex布局案例-网格布局
可用F12开发者工具查看元素及样式,可打开codepen在线编辑代码。
::: demo [vanilla]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| <html> <div class="grid"> <div class="grid-cell">1/2</div> <div class="grid-cell">1/2</div> </div>
<div class="grid"> <div class="grid-cell">1/3</div> <div class="grid-cell">1/3</div> <div class="grid-cell">1/3</div> </div>
<div class="grid"> <div class="grid-cell">1/4</div> <div class="grid-cell">1/4</div> <div class="grid-cell">1/4</div> <div class="grid-cell">1/4</div> </div>
<div class="grid text"> <div class="grid-cell"> 高度会跟随右侧元素变化 </div> <div class="grid-cell"> 内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充内容填充 </div> </div>
<h4>某个网格设置百分比宽度</h4> <div class="grid"> <div class="grid-cell u-full">100%</div> </div> <div class="grid"> <div class="grid-cell u-1of2">50%</div> <div class="grid-cell">auto</div> <div class="grid-cell">auto</div> </div> <div class="grid"> <div class="grid-cell u-1of3">33.33%</div> <div class="grid-cell">auto</div> <div class="grid-cell">auto</div> </div> <div class="grid"> <div class="grid-cell u-1of4">25%</div> <div class="grid-cell">auto</div> <div class="grid-cell">auto</div> <div class="grid-cell">auto</div> <div class="grid-cell">auto</div> <div class="grid-cell">auto</div> <div class="grid-cell">auto</div> </div> </html> <style> .grid { display: flex; } .grid-cell { flex: 1; } .grid-cell.u-full { flex: 0 0 100%; } .grid-cell.u-1of2 { flex: 0 0 50%; } .grid-cell.u-1of3 { flex: 0 0 33.3333%; } .grid-cell.u-1of4 { flex: 0 0 25%; } .grid-cell { background: #eee; text-align: center; margin: 5px; padding: 10px 0; } .text .grid-cell { text-align: left } </style>
|
:::
参考:http://www.ruanyifeng.com/blog/2015/07/flex-examples.html