我有一个简单的2列布局,带有一个脚注,可以清除标记中的左右div。我的问题是,我不能让页脚留在所有浏览器的页面底部。如果内容向下推页脚,它就会起作用,但情况并不总是如此。
当前回答
下面是一个使用jQuery的解决方案,它就像一个魅力。它检查窗口的高度是否大于主体的高度。如果是,则更改页脚的边距顶部以进行补偿。在Firefox, Chrome, Safari和Opera中测试。
$( function () {
var height_diff = $( window ).height() - $( 'body' ).height();
if ( height_diff > 0 ) {
$( '#footer' ).css( 'margin-top', height_diff );
}
});
如果你的页脚已经有一个边距顶部(50像素,例如),你将需要改变最后一部分:
css( 'margin-top', height_diff + 50 )
其他回答
react友好的解决方案-(不需要分隔符div)
Chris coyer(值得尊敬的CSS-Tricks网站)在sticky - footer上保持了最新的页面,现在至少有五种创建sticky footer的方法,包括使用FlexBox和CSS-Grid。
为什么这很重要?因为,对我来说,我多年来使用的早期/旧方法对React不起作用——我不得不使用Chris的flexbox解决方案——这很简单,也很有效。
下面是他的CSS-Tricks flexbox粘性页脚-看看下面的代码,它不可能更简单。
(下面的StackSnippet示例没有完美地呈现示例的底部。页脚会延伸到屏幕底部,这在现实生活中是不会发生的。)
html,身体{高度:100%;} 身体{显示:flex;flex-direction:列;} .content {flex: 1 0 auto;} /* flex:增长/收缩/伸缩基础;* / .footer {flex-shrink: 0;} /* ----以下仅为演示---- */ .footer{背景:苍绿色;} 身体< > <div class="content">Page content - height扩展到填充空间</div> <footer class="footer"> footer Content</footer> . 身体< / >
Chris还为那些喜欢网格的人演示了这个CSS-Grid解决方案。
引用:
CSS-Tricks - Flexbox的完整指南
要获得一个粘性页脚:
为你的内容设置一个<div> with class="wrapper"。 在包装器的</div>结束之前放置 < div class = "推" > < / div >。 在包装器的</div>结束后放置 < div class = "脚注" > < / div >。
* {
margin: 0;
}
html, body {
height: 100%;
}
.wrapper {
min-height: 100%;
height: auto !important;
height: 100%;
margin: 0 auto -142px; /* the bottom margin is the negative value of the footer's height */
}
.footer, .push {
height: 142px; /* .push must be the same height as .footer */
}
一个快速简单的解决方案,使用flex
将html和body的高度设置为100%
html,
body {
width: 100%;
height: 100%;
}
将主体显示为具有列方向的flex:
body {
min-height: 100%;
display: flex;
flex-direction: column;
}
为main添加flex-grow: 1
main {
flex-grow: 1;
}
flex-grow指定应将伸缩容器中剩余空间的多少分配给项(伸缩增长因子)。
*, *::after, *::before{ margin: 0; padding: 0; box-sizing: border-box; } html, body { width: 100%; height: 100%; } body { min-height: 100%; display: flex; flex-direction: column; } main { flex-grow: 1; } footer{ background-color: black; color: white; padding: 1rem 0; display: flex; justify-content: center; align-items: center; } <body> <main> <section > Hero </section> </main> <footer > <div> <p > © Copyright 2021</p> </div> </footer> </body>
很多人把这个简单问题的答案放在这里,但我有一件事要补充,考虑到我是多么沮丧,直到我弄清楚我做错了什么。
如前所述,最直接的方法是这样做。
html {
position: relative;
min-height: 100%;
}
body {
background-color: transparent;
position: static;
height: 100%;
margin-bottom: 30px;
}
.site-footer {
position: absolute;
height: 30px;
bottom: 0px;
left: 0px;
right: 0px;
}
然而,post中没有提到的属性是body标签上的position: static,大概是因为它通常是默认的。相对位置无效!
我的wordpress主题覆盖了默认的主体显示,这让我困惑了很长一段时间。
带有显示的粘性页脚:flex
解决方案的灵感来自菲利普沃尔顿的粘性页脚。
解释
此解决方案仅在以下情况下有效:
Chrome≥21.0 Firefox浏览器≥20.0 Internet Explorer浏览器≥10 Safari≥6.1
它基于伸缩显示,利用了flex-grow属性,该属性允许元素在高度或宽度上增长(当流动方向分别设置为列或行时),以占用容器中的额外空间。
我们还将利用vh单位,其中1vh被定义为:
视口高度的1/100
因此,100vh的高度是告诉一个元素跨越整个视口高度的简洁方法。
这是你如何组织你的网页:
----------- body -----------
----------------------------
---------- footer ----------
----------------------------
为了让页脚固定在页面底部,您希望正文和页脚之间的空间增加到将页脚推到页面底部所需的大小。
因此我们的布局变成:
----------- body -----------
----------------------------
---------- spacer ----------
<- This element must grow in height
----------------------------
---------- footer ----------
----------------------------
实现
身体{ 保证金:0; 显示:flex; flex-direction:列; 最小高度:100 vh; } .spacer { flex: 1; } /*为演示的目的使它可见*/ .footer { 高度:50 px; 背景颜色:红色; } 身体< > <div class="content">Hello World!< / div > < div class = "隔离" > < / div > <页脚类=“页脚”> < /页脚> 身体< / >
你可以在JSFiddle玩。
Safari怪癖
请注意,Safari对flex-shrink属性的实现有缺陷,该属性允许项缩小到显示内容所需的最小高度以上。 要解决这个问题,你必须在上面的例子中显式地将.content和页脚的flex-shrink属性设置为0:
.content {
flex-shrink: 0;
}
.footer {
flex-shrink: 0;
}
或者,将spacer元素的flex样式更改为:
.spacer {
flex: 1 0 auto;
}
这种3值简写样式相当于以下完整设置:
.spacer {
flex-grow: 1;
flex-shrink: 0;
flex-basis: auto;
}
优雅的工作无处不在。