CSS 粘性页脚 |页脚没有固定高度
我已经从 http://www.cssstickyfooter.com/ 多次实现了粘性页脚。唯一的问题是页脚有固定的高度。有没有一个纯 CSS 的解决方案可以让页脚根据里面的内容增长?
JS 解决方案还不错,但 CSS 是最好的。
预先感谢您的帮助。
I have implemented the sticky footer many times from http://www.cssstickyfooter.com/. The only problem is that the footer has a fixed height. Is there a pure CSS solution to allow the footer to grow based on the content inside?
A JS solution wouldn't be bad, but CSS would be best.
Thanks in advance for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
更新的答案
原始答案已经有五年多了,并且失败的原因是,如果页脚高度增加或减少,填充不会更新。您可以绑定到窗口的 resize 事件,并在每次触发时调用此事件,但这有点过分了。
相反,我鼓励您绑定到
window.onresize
事件,但限制逻辑,这样我们就不会计算样式、破坏 DOM 并导致布局每秒数十次:当页面加载时,我们立即触发
adjustContainerPadding
方法。此方法将正文的paddingBottom
设置为与footer
的计算高度相匹配。请注意,window.getComputedStyle
方法需要 IE9 或更高版本。小提琴: http://jsfiddle.net/jonathansampson/7b0neb6p/
原始答案
我鼓励您(为了简单起见)只使用 cssstickyfooter 代码,并通过 javascript 设置 css 值(jQuery 代码如下)。
代码未经测试,但应该可以正常工作
无论页脚中有多少内容,这都会自动为您重置 CSS 值。
Updated Answer
The original answer is over five years old, and fails in that the padding is not updated in the event the footer height is increased, or decreased. You could bind to the resize event of the window, and call this on every fire, but that would be a bit excessive.
I would instead encourage you to bind to the
window.onresize
event, but throttle the logic such that we aren't computing styles, thrashing the DOM, and causing layouts dozens of times per second:When the page loads, we immediately fire the
adjustContainerPadding
method. This method sets thepaddingBottom
of the body to match the computed height of thefooter
. Note here that thewindow.getComputedStyle
method requires IE9 or greater.Fiddle: http://jsfiddle.net/jonathansampson/7b0neb6p/
Original Answer
I would encourage you (for simplicity) to just use the cssstickyfooter code, and set the css values via javascript (jQuery code follows).
code is untested, but should work just fine
No matter how much content you have in your footer, this will automatically reset the CSS values for you.
我真的不知道为什么大家不使用这种技术。非常简单
没有固定高度、JAVASCRIPT 或表格
当内容更多时展开,当没有内容时它会粘在底部
I really don't know why everyone is not using this technique. It's so easy
NO FIXED HEIGHT, JAVASCRIPT OR TABLES
Expands when more content, and when there isn't it sticks to bottom
DISPLAY TABLE = 无 JS 且无固定高度!
适用于现代浏览器 (IE 8 +) - 我在多个浏览器中测试了它,一切似乎都运行良好。
我发现这个解决方案是因为我需要一个没有固定高度且没有 JS 的粘性页脚。代码如下。
说明:基本上,您有一个包含 2 个子元素的容器 div:一个包装器和一个页脚。将页面上所需的所有内容(页脚除外)放入包装器中。容器设置为
display: table;
包装器设置为display: table-row;
如果将 html、body 和wrapper 设置为height: 100 %
,页脚将粘在底部。页脚也设置为
display: table;
。为了获取子元素的边距,这是必要的。您还可以将页脚设置为display: table-row;
这将不允许您在页脚上设置margin-top
。在这种情况下,您需要发挥创意,使用更多嵌套元素。解决方案: https://jsfiddle.net/0pzy0Ld1/15/
还有更多内容: http://jantimon.nl/playground/footer_table.html
DISPLAY TABLE = NO JS and NO fixed height!
Works in modern browsers ( IE 8 + ) - I tested it in several browser and it all seemed to work well.
I discovered this solution because I needed a sticky footer without fixed height and without JS. Code is below.
Explanation: Basically you have a container div with 2 child elements: a wrapper and a footer. Put everything you need on the page ( exept the footer ) in the wrapper. The container is set to
display: table;
The wrapper is set todisplay: table-row;
If you set the html, body and wrapper toheight: 100%
, the footer will stick to the bottom.The footer is set to
display: table;
as well. This is necessary, to get the margin of child elements. You could also set the footer todisplay: table-row;
This will not allow you to setmargin-top
on the footer. You need to get creative with more nested elements in that case.The solution: https://jsfiddle.net/0pzy0Ld1/15/
And with more content: http://jantimon.nl/playground/footer_table.html
以下方法非常简单,可确保页脚随着窗口大小的调整而调整。
灵感来自 https://getbootstrap.com/examples/sticky-footer/ 和 http://blog.mojotech.com/responsive-dynamic-height-sticky-footers/< /a>
CSS
JS
The following method is extremely simple and ensures that your footer adapts as your window resizes.
Inspiration from https://getbootstrap.com/examples/sticky-footer/ and http://blog.mojotech.com/responsive-dynamic-height-sticky-footers/
CSS
JS
请确保它适用于所有浏览器,但请尝试:
#footer {position:absolute;顶部:100%;宽度:100%; }
Please ensure that it works on all browsers but try:
#footer { position:absolute; top:100%; width:100%; }
检查一下,这对你有用
})
check it out, it will be useful for you
})