关于css:滚动包含溢出内容的flexbox

Scrolling a flexbox with overflowing content

enter image description here

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

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

我省略了用于样式设计的代码。你可以在笔里看到所有的东西。

上面的工作是有效的,但是当content区域的内容溢出时,它会使整个页面滚动。我只想让内容区本身滚动,所以我将overflow: auto添加到content分区。

现在的问题是,这些列本身没有超出它们的父级高度,因此边界也被切掉了。

这是显示滚动问题的笔。

如何设置content区域独立滚动,同时其子对象仍超出content框的高度?


我已经和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;
}

这是笔:

  • 正在拉伸的短柱。
  • 更长的列溢出和滚动。
  • 这样做的原因是,如果align-items: stretch有一个固有的高度,它就不会缩小其项,这是由min-content完成的。


    经过多次反复试验,我才很好地解决了这个问题。

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

    基本上,要使flexbox单元格可滚动,必须使其所有父级overflow: hidden;,否则它将忽略溢出设置,而使父级变大。


    position:absolute;flex一起工作:

    position: relative定位柔性项目。然后在其中添加另一个元素:

    1
    2
    3
    4
    5
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;

    这会将元素扩展到其相对定位父元素的边界,但不允许扩展它。在内部,overflow: auto;将按预期工作。

    • 答案中包含的代码段-运行代码段或
    • 单击此处获取代码笔
    • 结果:enter image description here

    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

    基本上,你需要把htmlbody放到height: 100%;中,把你所有的内容包装成 中。

    CSS:

    1
    2
    3
    4
    5
    6
    7
    8
    html, body {
      height: 100%;
    }

    .wrap {
      height: 100vh;
      display: flex;
    }

    为我工作。希望它有帮助


    添加:

    1
    align-items: flex-start;

    根据.content {}的规则。至少在Firefox和Chrome中,它可以在你的笔中修复。

    默认情况下,.contentalign-items: stretch,这使得它可以根据http://dev.w3.org/csswg/css flexbox/algo-stretch调整所有自动高度子项的大小以匹配自己的高度。相反,值flex-start允许孩子们计算他们自己的高度,并在开始边缘对齐(和溢出,并触发滚动条)。