flex布局中的可滚动容器

时间:2017-10-03 10:47:10

标签: css flexbox

我目前正在开发一个简单的CSS布局,它由两个并排放置的窗格和一个页脚组成。我有以下限制:

  • 左侧窗格是固定宽度。
  • 页脚是固定的高度和 不得落入包含div的底部。
  • 剩余尺寸会扩大以填充可用空间。
  • 右侧窗格包含大量内容。它应该水平包裹并垂直滚动。

我试图使用flexbox制作合适的东西。 A working example can be found on jsfiddle.

HTML:

<div class="container">
  <div class="top">
    <div class="left">
       Top-left content
    </div>
    <div class="right">
      <div class="rightInternal">
        Large content goes here.
      </div>
    </div>
  </div>
  <div class="bottom">
    Bottom pane content should not collapse or be pushed outside the bounds of 'container'.
  </div>
</div>

CSS:

.container {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: stretch;
}

.top {
  width: 100%;
  flex-grow: 1;
  display: flex;
  flex-direction: row;
  align-items: stretch;
}

.left {
  width: 30%;
  min-width: 30%;
}

.right {
  flex-grow: 1;
  overflow-y: auto;
}

.rightInternal {
  display: flex;
  flex-direction: column;
}

.bottom {
  padding: 10px;
  flex-shrink: 0;
  display: flex;
  justify-content: flex-end;
}

问题:

这在Chrome中运行良好,但内容在&#39; top&#39;超出了容器的范围。在IE和FF中由于内容很大。在&#39; top&#39;的内容上设置高度导致页脚被推离底部(高度:100%)或内容未扩展以填充可用的垂直空间(例如,高度:100px)。

如何创建所需的布局?

注意:

  • JSFiddle示例已经过轻微修改,可以很好地制作容器大小并使所有内容变得丰富多彩,以便轻松识别。在我的实际环境中,高度100%将使容器填充其所在的可见空间,这是所希望的。
  • 我没有要求使用flexbox。

1 个答案:

答案 0 :(得分:0)

我认为您可以通过将overflow属性添加到.top

来实现您想要使用的布局

overflow设为hidden

jsfiddle

&#13;
&#13;
.container {
  width: 90%;
  height: 300px;
  background: rgba(255, 125, 125, 1);
  padding: 10px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: stretch;
}

.top {
  width: 100%;
  background: rgba(238, 130, 238, 1);
  flex-grow: 1;
  display: flex;
  flex-direction: row;
  align-items: stretch;
  overflow: hidden;
}

.left {
  width: 30%;
  min-width: 30%;
  background: rgba(135, 206, 250, 1);
  padding: 10px;
  margin: 10px;
}

.right {
  background: rgba(152, 251, 152, 1);
  padding: 10px;
  margin: 10px;
  flex-grow: 1;
  overflow-y: auto;
}

.rightInternal {
  display: flex;
  flex-direction: column;
}

.bottom {
  background: rgba(255, 255, 153, 1);
  padding: 10px;
  flex-shrink: 0;
  display: flex;
  justify-content: flex-end;
}
&#13;
<div class="container">
  <div class="top">
    <div class="left">
      Top-left content
    </div>
    <div class="right">
      <div class="rightInternal">
        This rainbow should scroll!
        <br/>
        <br/>
        <div style="background-color:red;height:60px">Richard</div>
        <div style="background-color:orange;height:60px">of</div>
        <div style="background-color:yellow;height:60px">York</div>
        <div style="background-color:green;height:60px">gained</div>
        <div style="background-color:blue;height:60px">battle</div>
        <div style="background-color:indigo;height:60px">in</div>
        <div style="background-color:violet;min-height:60px">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris in congue ipsum. Suspendisse maximus non magna sed blandit. Fusce mattis nisl elit, a sollicitudin justo porta ac. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices
          posuere cubilia Curae; Donec elementum nunc ac vehicula dictum. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nulla ultricies ipsum dignissim quam venenatis, in vehicula eros ultrices. Maecenas ut malesuada
          nulla.
        </div>
      </div>
    </div>
  </div>
  <div class="bottom">
    Bottom pane content should not collapse or be pushed outside the bounds of 'container'.
  </div>
</div>
&#13;
&#13;
&#13;