CSS 粘性页脚实现之间的区别?
我发现了 CSS 粘性页脚的 2 种不同实现:
Ryan Fait 粘性页脚 - http://ryanfait .com/sticky-footer/
Steve Hatcher 粘页脚 - http://www.cssstickyfooter.com/
有人可以解释一下它们各自工作方式之间的区别吗?
如果还有其他已知的实现,您能否发表评论或编辑这个问题?
I've found 2 different implementations of a CSS sticky footer:
Ryan Fait sticky footer - http://ryanfait.com/sticky-footer/
Steve Hatcher sticky footer - http://www.cssstickyfooter.com/
Could someone explain the difference between how each of them work?
And if there are other known implementations, could you please post a comment or edit this question?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它们在功能上非常相似。第一个强制 div 达到页面的整个高度,然后给它一个页脚大小的负边距。
这样做的目的是确保包装 div 内的所有内容都是页面高度减去页脚高度的 100%。这样,只要页脚的大小与负边距相同,它就会粘在左边的间隙中并出现在元素的底部。
第二个还强制内容占页面高度的 100%。
然后,它在主要内容的底部创建一个与页脚大小相同的空间。
然后使用相对位置和负上边距强制页脚显示在其正常位置上方 150 像素(在刚刚创建的空间中)。
注意:仅当您的页面内容分别保存在 .wrapper 内和 #wrap 内的 #main 内,并且页脚位于这些容器之外时,此方法才有效。
如果您不明白其中的任何部分,请给我留言,我会尽力回答。
编辑:响应 user360122
第一个 HTML 标记:
第二个 HTML 标记:
记住包含样式表并声明 doctype .etc(这些不是完整的 html 页面)。
They are pretty similar in terms of function. The first forces a div to the full height of the page and then give it a negative margin the size of the footer.
What this does is makes sure that all content within the wrapping div is 100% of the page height minus the height of the footer. So that as long as the footer is the same size as the negative margin it will stick in the gap left and appear at the bottom of the element.
The second also forces the content to be 100% of the height of the page.
It then creates a space at the bottom of the main content the same size as the footer.
Then using position relative and a negative top margin forces the footer to appear 150px above its normal position (in the space it just made).
Note: This only works so long as your page content is kept within .wrapper and #main inside #wrap respectively, and your footer is outside of these containers.
If you didn't understand any part of that leave me a comment and I'll try to answer it.
Edit: In response to user360122
HTML markup for first:
HTML markup for second:
Remember to include the stylesheet and declare doctype .etc (these aren't full html pages).
bootstrap文档中有一个看起来非常简单的示例: http://getbootstrap.com/examples /sticky-footer/
不需要包装器或推送。
There is an example in the bootstrap documentation which seems to be very simple: http://getbootstrap.com/examples/sticky-footer/
No wrapper or push needed.