Flexbox:标题,居中的主体和粘性页脚溢出 [英] Flexbox: Header, centered body and sticky footer overflow
问题描述
我试图设置一个非常像圣杯的布局。这里的区别是,我正在寻找一个动态大小的内容部分垂直和水平居中。
我已经能够得到这个工作(用于大型显示器): 此处的Codepen
html,body {width:100%;身高:100%保证金:0;填充:0; text-align:center;}。header {height:60px; line-height:60px;宽度:100%;背景:rgba(255,0,0,0.5);}。content {height:calc(100% - 120px);显示:flex; flex-direction:column; align-items:center; justify-content:center;背景:rgba(0,255,0,0.5);}。content p {width:50%;}。footer {height:60px; line-height:60px;宽度:100%;背景:rgba(0,0,255,0.5);}。header,.footer {font-weight:bold;}
< div class =header>标题< / div>< div class =content> < h1>我是内容< / h1> Lorem Ipsum只是印刷和排版行业的虚拟文本。 Lorem Ipsum自从16世纪以来一直是这个行业的标准虚拟文本,当时一台不知名的打印机拿起了一个类型的厨房,并把它打成一个样本书。它不仅存活了五个世纪,还有电子排版的飞跃,基本保持不变。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset表单的发布以及最近的包括Aldus PageMaker在内的桌面出版软件(包括Lorem Ipsum的版本)而得到推广。< / p>< / div>< div class =footer > 部分太大,或视图太小,溢出不会踢直到div超过100%垂直不(100%的高度 - 页眉和页脚的高度)
将允许它在内容较大时正确增长到高于视口。 href =https://codepen.io/anon/pen/gRKOoz?editors=1100 =nofollow noreferrer>更新了codepen
我试图做到这样,当有重叠时,整个页面将滚动,不仅在内容部分内有一个滚动条。
解决方案如果您将
html,body
规则更改为此
html,body {
margin:0;
text-align:center;
display:flex;
flex-direction:column;
}
然后添加这个新规则
body {
min-height:100vh;
$ / code>然后从
height:calc(100% - 120px); 到
flex-grow:1; 在你的
内容
按照你的要求工作。
这里的技巧是让你的
body
一个flex容器,然后通过设置内容
为flex-grow:1
当内容变小时,它将填充剩余空间, code> min-height:100vh
堆栈snippet
$ b{margin:0; text-align:center;显示:flex; flex-direction:column;} body {min-height:100vh;}。header {height:60px; line-height:60px;背景:rgba(255,0,0,0.5);}。content {flex-grow:1;显示:flex; flex-direction:column; align-items:center; justify-content:center;背景:rgba(0,255,0,0.5);}。content p {width:50%;}。footer {height:60px; line-height:60px;背景:rgba(0,0,255,0.5);}。header,.footer {font-weight:bold;}
< div class =header>标题< / div>< div class =content> < h1>我是内容< / h1> Lorem Ipsum只是印刷和排版行业的虚拟文本。 Lorem Ipsum自从十六世纪以来一直是业界标准的虚拟文本,当时一台不知名的打印机拿起了一个类型的厨房,把它打成一个样本书。它不仅存活了五个世纪,还有电子排版的飞跃,基本保持不变。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset工作表的发布以及最近的包括Aldus PageMaker在内的桌面出版软件(包括Lorem Ipsum的版本)而得到普及。 Lorem Ipsum只是印刷和排版行业的虚拟文本。 Lorem Ipsum自从16世纪以来一直是这个行业的标准虚拟文本,当时一台不知名的打印机拿起了一个类型的厨房,并把它打成一个样本书。它不仅存活了五个世纪,还有电子排版的飞跃,基本保持不变。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset表单的发布以及最近的包括Aldus PageMaker在内的桌面出版软件(包括Lorem Ipsum的版本)而得到普及.Lorem Ipsum只是印刷和排版行业的虚拟文本。 Lorem Ipsum自从十六世纪以来一直是业界标准的虚拟文本,当时一台不知名的打印机拿起了一个类型的厨房,把它打成一个样本书。它不仅存活了五个世纪,还有电子排版的飞跃,基本保持不变。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset表单的发布以及最近的包括Aldus PageMaker在内的桌面出版软件(包括Lorem Ipsum的版本)而得到普及.Lorem Ipsum只是印刷和排版行业的虚拟文本。 Lorem Ipsum自从16世纪以来一直是这个行业的标准虚拟文本,当时一台不知名的打印机拿起了一个类型的厨房,并把它打成一个样本书。它不仅存活了五个世纪,还有电子排版的飞跃,基本保持不变。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset工作表的发布以及最近的包括Aldus PageMaker在内的桌面出版软件(包括Lorem Ipsum的版本)而得到普及。 < / p>< / div>< div class =footer> Footer< / div>
如果有人想让
内容
滚动而不是整个页面,将body 规则更改为
height:100vh
并将overflow:auto
添加到内容
规则
更新的codepen p>
堆栈片段
html,body {margin:0; text-align:center;显示:flex; flex-direction:column;} body {height:100vh;}。header {height:60px; line-height:60px;背景:rgba(255,0,0,0.5);}。content {flex-grow:1;显示:flex; flex-direction:column; align-items:center;背景:rgba(0,255,0,0.5); overflow:auto;}。content p {width:50%;}。footer {height:60px; line-height:60px;背景:rgba(0,0,255,0.5);}。header,.footer {font-weight:bold;}
< div class =header>标题< / div>< div class =content> < h1>我是内容< / h1> Lorem Ipsum只是印刷和排版行业的虚拟文本。 Lorem Ipsum自从十六世纪以来一直是业界标准的虚拟文本,当时一台不知名的打印机拿起了一个类型的厨房,把它打成一个样本书。它不仅存活了五个世纪,还有电子排版的飞跃,基本保持不变。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset工作表的发布以及最近的包括Aldus PageMaker在内的桌面出版软件(包括Lorem Ipsum的版本)而得到普及。 Lorem Ipsum只是印刷和排版行业的虚拟文本。 Lorem Ipsum自从十六世纪以来一直是业界标准的虚拟文本,当时一台不知名的打印机拿起了一个类型的厨房,把它打成一个样本书。它不仅存活了五个世纪,还有电子排版的飞跃,基本保持不变。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset表单的发布以及最近的包括Aldus PageMaker在内的桌面出版软件(包括Lorem Ipsum的版本)而得到普及.Lorem Ipsum只是印刷和排版行业的虚拟文本。 Lorem Ipsum自从16世纪以来一直是这个行业的标准虚拟文本,当时一台不知名的打印机拿起了一个类型的厨房,并把它打成一个样本书。它不仅存活了五个世纪,还有电子排版的飞跃,基本保持不变。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset表单的发布以及最近的包括Aldus PageMaker在内的桌面出版软件(包括Lorem Ipsum的版本)而得到普及.Lorem Ipsum只是印刷和排版行业的虚拟文本。 Lorem Ipsum自从十六世纪以来一直是业界标准的虚拟文本,当时一台不知名的打印机拿起了一个类型的厨房,把它打成一个样本书。它不仅存活了五个世纪,还有电子排版的飞跃,基本保持不变。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset工作表的发布以及最近的包括Aldus PageMaker在内的桌面出版软件(包括Lorem Ipsum的版本)而得到普及。 < / p>< / div>< div class =footer>页脚< / div>
Update This has been solved thanks to LGSon's comment below Updated codepen
I am trying to setup a layout much like the Holy grail. The difference here is that I am looking for a dynamically sized "Content" section to be vertically and horizontally centered.
I have been able to get this working (for large displays) with 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>
The problem is that if the "Content" section is too large, or the view is too small, the overflow wont kick in until the div is more than 100% vertical not (100% height - the header and footer heights)
I am trying to make it so that when there is overlap, that the whole page will scroll and not only have a scrollbar within the "Content section".
解决方案If you change your
html, body
rule to thishtml, body { margin: 0; text-align: center; display: flex; flex-direction: column; }
and then add this new rule
body { min-height: 100vh; }
and then change from
height: calc(100% - 120px);
toflex-grow: 1;
in yourcontent
rule it will work as you asked for.The trick here is to make your
body
a flex container, and then, by setting thecontent
toflex-grow: 1
it will fill the remaining space when the content is smaller, and themin-height: 100vh
will allow it to properly grow higher than the viewport when the content is bigger.Stack snippet
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>
And if someone want to enable the
content
to scroll instead of the whole page, change thebody
rule toheight: 100vh
and addoverflow: auto
to thecontent
ruleStack snippet
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>
这篇关于Flexbox:标题,居中的主体和粘性页脚溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!