Adding scroll to flex items
所以我有一个占据屏幕 100% 宽度/高度的应用程序。左侧有一个固定宽度的侧边栏,右侧有一个灵活大小的 div。右边的 div 底部有一个固定高度的工具栏,上面有一个灵活的高度框。如何确保底部的工具栏在溢出时水平滚动,而左侧的侧边栏在溢出时垂直滚动?
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 | html, body, .my-app { padding: 0; margin: 0; width: 100%; height: 100%; background: red; } .my-app { display: flex; flex-direction: row; } .sidebar { background: brown; width: 300px; } .map-container { flex: 1; display: flex; flex-direction: column; } .map { background: blue; flex: 1; } .toolbar { height: 300px; background: green; } |
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 | <p> Sidebar fixed width of 300px; </p> <p> Map should grow to fill available space horizontally and vertically </p> <table class="large-table"> <tr> <td>Content should scroll and be 300px</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> </tr> </table> |
https://jsfiddle.net/tvsgthb7/
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 | html, body, .my-app { padding: 0; margin: 0; width: 100%; height: 100%; background: red; } .my-app { display: flex; flex-direction: row; } .sidebar { background: orange; /* width: 300px; */ flex: 0 0 300px; /* makes 300px width inflexible with flex-shrink: 0 */ overflow: auto; } .sidebar > p { height: 1000px; } .map-container { flex: 1; display: flex; flex-direction: column; min-width: 0; /* allows flex item to shrink past its content size */ } .map { background: aqua; flex: 1; } .toolbar { height: 300px; background: lightgreen; overflow: auto; } |
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 | <p> Sidebar fixed width of 300px; </p> <p><center>[wp_ad_camp_2]</center></p><p> Map should grow to fill available space horizontally and vertically </p> <table class="large-table"> <tr> <td>Content should scroll and be 300px</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> <td>Content</td> </tr> </table> |
jsFiddle 演示
更多信息在这里:
- 为什么弹性项目不缩小超过内容大小?
- flex-basis 和 width 有什么区别?