Scrolling a flexbox with overflowing content
下面是我用来实现上述布局的代码:
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 | .header { height: 50px; } .body { position: absolute; top: 50px; right: 0; bottom: 0; left: 0; display: flex; } .sidebar { width: 140px; } .main { flex: 1; display: flex; flex-direction: column; } .content { flex: 1; display: flex; } .column { padding: 20px; border-right: 1px solid #999; } |
1 2 3 4 5 6 7 8 9 10 | Main header Sidebar Page Header. Content columns are below. Column 1 Column 1 Column 1 |
我省略了用于样式设计的代码。你可以在笔里看到所有的东西。
上面的工作是有效的,但是当
现在的问题是,这些列本身没有超出它们的父级高度,因此边界也被切掉了。
这是显示滚动问题的笔。
如何设置
我已经和TabAtkins(flexbox规范的作者)讨论过这个问题,这就是我们想到的:
HTML:
1 2 3 | Column 1 Column 1 Column 1 |
CSS:
1 2 3 4 5 6 7 8 9 10 | .content { flex: 1; display: flex; overflow: auto; } .box { min-height: min-content; /* needs vendor prefixes */ display: flex; } |
这是笔:
这样做的原因是,如果
经过多次反复试验,我才很好地解决了这个问题。
查看我的博客帖子:http://geon.github.io/programming/2016/02/24/flexbox-full-page-web-app-layout
基本上,要使flexbox单元格可滚动,必须使其所有父级
与
用
1 2 3 4 5 | position: absolute; top: 0; left: 0; right: 0; bottom: 0; |
这会将元素扩展到其相对定位父元素的边界,但不允许扩展它。在内部,
- 答案中包含的代码段-运行代码段或
- 单击此处获取代码笔
- 结果:。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | .all-0 { top: 0; bottom: 0; left: 0; right: 0; } p { text-align: justify; } .bottom-0 { bottom: 0; } .overflow-auto { 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 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 | <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet"/> <!-- LEFT-SIDE - ROW-1 --> <!-- CARD 1 --> <img class="img img-fluid" src="https://via.placeholder.com/700x250"> <h4>Heading 1</h4> <p> Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old... <!-- CARD 2 --> <img class="img img-fluid" src="https://via.placeholder.com/400x250"> <h4>Heading 1</h4> <p> Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old... <!-- CARD 3 --> <img class="img img-fluid" src="https://via.placeholder.com/400x250"> <h4>Heading 1</h4> <p> Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old... <!-- CARD 4 --> <img class="img img-fluid" src="https://via.placeholder.com/400x250"> <h4>Heading 1</h4> <p> Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old... <!-- CARD 5--> <img class="img img-fluid" src="https://via.placeholder.com/400x250"> <h4>Heading 1</h4> <p> Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old... <h4>Social Sidebar...</h4> <hr /> <p> Topping candy tiramisu soufflé fruitcake ice cream chocolate bar. Bear claw ice cream chocolate bar donut sweet tart. Pudding cupcake danish apple pie apple pie. Halva fruitcake ice cream chocolate bar. Bear claw ice cream chocolate bar donut sweet tart. opping candy tiramisu soufflé fruitcake ice cream chocolate bar. Bear claw ice cream chocolate bar donut sweet tart. Pudding cupcake danish apple pie apple pie. Halva fruitcake ice cream chocolate bar. Bear claw ice cream chocolate bar donut sweet tart. opping candy tiramisu soufflé fruitcake ice cream chocolate bar. Bear claw ice cream chocolate bar donut sweet tart. Pudding cupcake danish apple pie apple pie. Halva fruitcake ice cream chocolate bar. Bear claw ice cream chocolate bar donut sweet tart. Pudding cupcake danish apple pie apple pie. Halvafruitcake ice cream chocolate bar. Bear claw ice cream chocolate bar donut sweet tart. Pudding cupcake danish apple pie apple pie. Halvafruitcake ice cream chocolate bar. Bear claw ice cream chocolate bar donut sweet tart. Pudding cupcake danish apple pie apple pie. Halvafruitcake ice cream chocolate bar. Bear claw ice cream chocolate bar donut sweet tart. Pudding cupcake danish apple pie apple pie. Halvafruitcake ice cream chocolate bar. Bear claw ice cream chocolate bar donut sweet tart. Pudding cupcake danish apple pie apple pie. Halva |
祝你好运。。。
稍晚一点,但这有助于:http://webdesign.tutsplus.com/tutorials/how-to-make-responsive-scrollable-panels-with-flexbox--cms-23269
基本上,你需要把
CSS:
1 2 3 4 5 6 7 8 | html, body { height: 100%; } .wrap { height: 100vh; display: flex; } |
为我工作。希望它有帮助
添加:
1 | align-items: flex-start; |
根据
默认情况下,