仅使用flexbox的固定页眉,可滚动内容和固定页脚布局

时间:2020-11-12 17:33:20

标签: html css

我正在尝试使用flex创建HEADER,可滚动的CONTENT和FOOTER结构:

.container {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 400px;
  background-color: white;
  border: 1px solid black;
  text-align: center;
  border-collapse: collapse;
  overflow-y: hidden;
}

.header {
  justify-self: flex-start;
  background-color: #cdcdcd;
  padding: 8px;
}

.content {
  border: 1px solid black;
  padding: 8px;
  overflow-y: scroll;
}

.footer {
  justify-self: flex-end;
  background-color: #cdcdcd;
  padding: 8px;
}
<div class="container">
  <div class="header">Header</div>
  <div class="content">
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
  </div>

  <div class="footer">
    Footer
  </div>
</div>

我不能避免整个屏幕滚动,不仅要滚动内容。我需要页眉和页脚保持固定的位置(分别为顶部和底部),并仅滚动内容(仅在内容上垂直滚动条)。

2 个答案:

答案 0 :(得分:1)

添加flex: 1 1 auto;height: 0px;将使.content可以滚动。

但是,由于height: 100%;上有.container.content会缩小,请使用min-height来防止这种情况;

.container {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 400px;
    background-color: white;
    border: 1px solid black;
    text-align: center;
    border-collapse: collapse;
    overflow-y: hidden;
}

.header {
    justify-self: flex-start;
    background-color: #cdcdcd;
    padding: 8px;
}

.content {
    flex: 1 1 auto;
    height: 0px;
    min-height: 300px;
    
    border: 1px solid black;
    padding: 8px;
    overflow-y: scroll;
}

.footer {
    justify-self: flex-end;
    background-color: #cdcdcd;
    padding: 8px;
}
<div class="container">
    <div class="header">Header</div>
    <div class="content">
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
        <p>Very tall content</p>
    </div>

    <div class="footer">
        Footer
    </div>
</div>

答案 1 :(得分:0)

将容器的高度更改为height: calc(100vh - (body padding + contaienr border thickness);

在这种情况下,我将身高从100%(内容的100%)更改为height: calc(100vh - 12px);

/* for demonstartion purpose only */
body {
  margin: 0;
  padding: 5px;
}

/* for demonstartion purpose only */

.container {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 12px);
  width: 400px;
  background-color: white;
  border: 1px solid black;
  text-align: center;
  border-collapse: collapse;
  overflow-y: hidden;
}

.header {
  justify-self: flex-start;
  background-color: #cdcdcd;
  padding: 8px;
}

.content {
  border: 1px solid black;
  padding: 8px;
  overflow-y: scroll;
}

.footer {
  justify-self: flex-end;
  background-color: #cdcdcd;
  padding: 8px;
}
<div class="container">
  <div class="header">Header</div>
  <div class="content">
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
    <p>Very tall content</p>
  </div>

  <div class="footer">
    Footer
  </div>
</div>