【问题标题】:Flexbox: Header, centered body and sticky footer overflowFlexbox:页眉、居中的正文和粘性页脚溢出
【发布时间】:2017-12-08 02:18:37
【问题描述】:

更新感谢 LGSon 在下面的评论 Updated codepen

我正在尝试设置类似于圣杯的布局。这里的不同之处在于我正在寻找一个动态大小的“内容”部分,使其垂直和水平居中。

我已经能够使用 flexbox 使这个工作(用于大型显示​​器):Codepen here

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  text-align: center;
}

.header {
  height: 60px;
  line-height: 60px;
  width: 100%;
  background: rgba(255, 0, 0, 0.5);
}

.content {
  height: calc(100% - 120px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(0, 255, 0, 0.5);
}
.content p {
  width: 50%;
}

.footer {
  height: 60px;
  line-height: 60px;
  width: 100%;
  background: rgba(0, 0, 255, 0.5);
}

.header, .footer {
  font-weight: bold;
}
<div class="header">
    Header
</div>

<div class="content">
    <h1>I am the content</h1>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</div>

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

问题是,如果“内容”部分太大,或者视图太小,溢出不会启动,直到 div 超过 100% 垂直而不是(100% 高度 - 页眉和页脚高度)

我正在努力做到这一点,当有重叠时,整个页面都会滚动,而不仅仅是“内容部分”中有一个滚动条。

【问题讨论】:

  • 您知道页眉和页脚与页面的其余部分一起滚动吗?这肯定不是我们想要的结果……
  • 这是预期的最终结果吗? codepen.io/anon/pen/gRKOoz?editors=1100
  • 嗨@JamesDouglas,目前不寻找粘性页眉和页脚,除非内容太小而无法像上面链接的桌面屏幕截图中那样查看。我认为这方面的引导术语是 Fixed 我不是在寻找。
  • @LGSon 谢谢你的魔法,这正是我一直在寻找的!

标签: html css flexbox


【解决方案1】:

如果您将 html, body 规则更改为此

html, body {
  margin: 0;
  text-align: center;
  display: flex;
  flex-direction: column;
}

然后添加这个新规则

body {
  min-height: 100vh;
}

然后在您的content 规则中从height: calc(100% - 120px); 更改为flex-grow: 1;,它将按照您的要求工作。

这里的技巧是让你的body 成为一个弹性容器,然后,通过将content 设置为flex-grow: 1,它将在内容较小时填充剩余空间,min-height: 100vh 将允许它当内容更大时,适当地增长到高于视口。

Updated codepen

堆栈sn-p

html, body {
  margin: 0;
  text-align: center;
  display: flex;
  flex-direction: column;
}

body {
  min-height: 100vh;
}

.header {
  height: 60px;
  line-height: 60px;
  background: rgba(255, 0, 0, 0.5);
}

.content {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(0, 255, 0, 0.5);
}

.content p {
  width: 50%;
}

.footer {
  height: 60px;
  line-height: 60px;
  background: rgba(0, 0, 255, 0.5);
}

.header, .footer {
  font-weight: bold;
}
<div class="header">
    Header
</div>

<div class="content">
    <h1>I am the content</h1>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
	Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
	</p>
</div>

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

如果有人想让content 滚动而不是整个页面,请将body 规则更改为height: 100vh 并将overflow: auto 添加到content 规则中

Updated codepen

堆栈sn-p

html, body {
  margin: 0;
  text-align: center;
  display: flex;
  flex-direction: column;
}

body {
  height: 100vh;
}

.header {
  height: 60px;
  line-height: 60px;
  background: rgba(255, 0, 0, 0.5);
}

.content {
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  background: rgba(0, 255, 0, 0.5);
  overflow: auto;
}

.content p {
  width: 50%;
}

.footer {
  height: 60px;
  line-height: 60px;
  background: rgba(0, 0, 255, 0.5);
}

.header, .footer {
  font-weight: bold;
}
<div class="header">
    Header
</div>

<div class="content">
    <h1>I am the content</h1>
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
	Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
	</p>
</div>

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

【讨论】:

    【解决方案2】:

    在页眉和页脚使用position: fixed; 后,我的建议是:

    1. 删除不透明的背景颜色,并用看起来完全相同的不透明的颜色替换它们(我已经这样做了)
    2. 移除文章的背景颜色,并为html, body 提供背景颜色
    3. 在这种情况下使用background-color 而不是background

    html,
    body {
      width: 100%;
      height: 100%;
      margin: 0;
      padding: 0;
      background-color: #7FFF7F;
      text-align: center;
    
    }
    
    .header {
      position: fixed;
      top: 0px;
      height: 60px;
      line-height: 60px;
      width: 100%;
      background-color: #FF7F7F;
    }
    
    .content {
      overflow: scroll;
      padding: 60px 0 60px 0;
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-orient: vertical;
      -webkit-box-direction: normal;
          -ms-flex-direction: column;
              flex-direction: column;
      -webkit-box-align: center;
          -ms-flex-align: center;
              align-items: center;
      -webkit-box-pack: center;
          -ms-flex-pack: center;
              justify-content: center;
    }
    
    .content p {
      width: 50%;
    }
    
    .footer {
      position: fixed;
      bottom: 0px;
      height: 60px;
      line-height: 60px;
      width: 100%;
      background-color: #7F7FFF;
    }
    
    .header,
    .footer {
      font-weight: bold;
    }
    <div class="header">
        Header
    </div>
    
    <div class="content">
        <h1>I am the content</h1>
        <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
    </div>
    
    <div class="footer">
        Footer
    </div>

    CodePen

    【讨论】:

      猜你喜欢
      • 2013-12-27
      • 1970-01-01
      • 2012-12-28
      • 2017-11-04
      • 2012-08-15
      • 2015-05-06
      • 2016-11-08
      • 2014-03-02
      相关资源
      最近更新 更多