以下是我用来实现上述布局的代码:

.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; } <div class="header">Main header</div> <div class="body"> <div class="sidebar">Sidebar</div> <div class="main"> <div class="page-header">Page Header. Content columns are below.</div> <div class="content"> <div class="column">Column 1</div> <div class="column">Column 1</div> <div class="column">Column 1</div> </div> </div> </div>

我省略了用于样式化的代码。你可以在笔里看到所有这些。


上述方法是有效的,但是当内容区域的内容溢出时,会导致整个页面滚动。我只想让内容区域本身滚动,所以我在内容div中添加了overflow: auto。

现在的问题是,列本身并没有超出它们的父高度,所以边界也在那里被切断了。

这支笔显示了滚动问题。

如何将内容区域设置为独立滚动,同时仍然让其子区域超出内容框的高度?


当前回答

这个问题的解决方案是添加overflow: auto;到.content,使内容包装器可滚动。

此外,在Flexbox包装器和溢出可滚动内容时,也会出现类似这种情况。

解决方案是添加overflow: hidden(或auto);到围绕大内容的包装器的父类(使用overflow: auto;设置)。

其他回答

我在任何地方都没有看到这个答案。但我需要的技巧是确保项目具有弹性-收缩:0;否则他们就会受到挤压。

.容器 { 显示:柔性; 溢出:自动 } .container > * { 弹性收缩: 0; 宽度:10em; 高度:10米; 背景:线性渐变(右下角,#F0F,#0FF); } <div class=“container”> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> <div></div> </div>

添加:

align-items: flex-start;

到.content{}的规则。至少(在火狐和Chrome浏览器中),这在你的笔中为我解决了这个问题。

默认情况下,.content有align-items: stretch,这使得它的所有自动高度子项的大小与它自己的高度匹配,每个http://dev.w3.org/csswg/css-flexbox/#algo-stretch。相比之下,flex-start值让子程序计算它们自己的高度,并在起始边缘对齐(并溢出,并触发滚动条)。

经过多次尝试和错误,我刚刚非常优雅地解决了这个问题。

看看我的博客文章:http://geon.github.io/programming/2016/02/24/flexbox-full-page-web-app-layout

基本上,要使一个flexbox单元格可滚动,你必须让它所有的父单元格overflow: hidden;,否则它会忽略你的溢出设置,而让父单元格变大。

工作位置:绝对;与flex一起:

使用Position: relative定位伸缩项。然后在它内部,添加另一个<div>元素:

position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;

这将元素扩展到其相对位置的父元素的边界,但不允许对其进行扩展。内部,溢出:自动;然后将按预期工作。

.all-0 { top: 0; bottom: 0; left: 0; right: 0; } p { text-align: justify; } .bottom-0 { bottom: 0; } .overflow-auto { overflow: auto; } <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet" /> <div class="p-5 w-100"> <div class="row bg-dark m-0"> <div class="col-sm-9 p-0 d-flex flex-wrap"> <!-- LEFT-SIDE - ROW-1 --> <div class="row m-0 p-0"> <!-- CARD 1 --> <div class="col-md-8 p-0 d-flex"> <div class="my-card-content bg-white p-2 m-2 d-flex flex-column"> <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... </div> </div> <!-- CARD 2 --> <div class="col-md-4 p-0 d-flex"> <div class="my-card-content bg-white p-2 m-2 d-flex flex-column"> <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... </div> </div> </div> <div class="row m-0"> <!-- CARD 3 --> <div class="col-md-4 p-0 d-flex"> <div class="my-card-content bg-white p-2 m-2 d-flex flex-column"> <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... </div> </div> <!-- CARD 4 --> <div class="col-md-4 p-0 d-flex"> <div class="my-card-content bg-white p-2 m-2 d-flex flex-column"> <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... </div> </div> <!-- CARD 5--> <div class="col-md-4 p-0 d-flex"> <div class="my-card-content bg-white p-2 m-2 d-flex flex-column"> <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... </div> </div> </div> </div> <div class="col-sm-3 p-0"> <div class="bg-white m-2 p-2 position-absolute all-0 d-flex flex-column"> <h4>Social Sidebar...</h4> <hr /> <div class="d-flex overflow-auto"> <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. 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 </div> </div> </div> </div>

最终结果:

CodePen链接

我遇到的一个问题是,要有一个滚动条,n元素需要指定高度(而不是%)。

诀窍是在每个列中嵌套另一组div,并将列父项的显示设置为flex-direction: column。

<style>
    html, body {
        height: 100%;
        margin: 0;
        padding: 0;
    }

    body {
        overflow-y: hidden;
        overflow-x: hidden;
        color: white;
    }

    .base-container {
        display: flex;
        flex: 1;
        flex-direction: column;
        width: 100%;
        height: 100%;
        overflow-y: hidden;
        align-items: stretch;
    }

    .title {
        flex: 0 0 50px;
        color: black;
    }

    .container {
        flex: 1 1 auto;
        display: flex;
        flex-direction: column;
    }

        .container .header {
            flex: 0 0 50px;
            background-color: red;
        }

        .container .body {
            flex: 1 1 auto;
            display: flex;
            flex-direction: row;
        }

            .container .body .left {
                display: flex;
                flex-direction: column;
                flex: 0 0 80px;
                background-color: blue;
            }
                .container .body .left .content,
                .container .body .main .content,
                .container .body .right .content {
                    flex: 1 1 auto;
                    overflow-y: auto;
                    height: 100px;
                }
                .container .body .main .content.noscrollbar {
                    overflow-y: hidden;
                }

            .container .body .main {
                display: flex;
                flex-direction: column;
                flex: 1 1 auto;
                background-color: green;
            }

            .container .body .right {
                display: flex;
                flex-direction: column;
                flex: 0 0 300px;
                background-color: yellow;
                color: black;
            }

    .test {
        margin: 5px 5px;
        border: 1px solid white;
        height: calc(100% - 10px);
    }
</style>

这是html:

<div class="base-container">
    <div class="title">
        Title
    </div>
    <div class="container">
        <div class="header">
            Header
        </div>
        <div class="body">
            <div class="left">
                <div class="content">
                    <ul>
                        <li>1</li>
                        <li>2</li>
                        <li>3</li>
                        <li>4</li>
                        <li>5</li>
                        <li>6</li>
                        <li>7</li>
                        <li>8</li>
                        <li>9</li>
                        <li>10</li>
                        <li>12</li>
                        <li>13</li>
                        <li>14</li>
                        <li>15</li>
                        <li>16</li>
                        <li>17</li>
                        <li>18</li>
                        <li>19</li>
                        <li>20</li>
                        <li>21</li>
                        <li>22</li>
                        <li>23</li>
                        <li>24</li>
                    </ul>
                </div>
            </div>
            <div class="main">
                <div class="content noscrollbar">
                    <div class="test">Test</div>
                </div>
            </div>
            <div class="right">
                <div class="content">
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>Right</div>
                    <div>End</div>
                </div>
            </div>
        </div>
    </div>
</div>

https://jsfiddle.net/LiamFlavelle/czpjdfr4/