html - 背景图片和固定高度的粘性页脚

标签 html css

这是我的 html/css 的框架:http://jsfiddle.net/GMg7B/ .

现在我想为 div#content 部分添加背景图片,以便:

  • 它会调整大小以适合其容器(类似于 background: cover)
  • 不与页脚重叠。
  • 保持图像的纵横比。
  • 调整背景图片的高度以适应“屏幕全高减去页脚高度”,必要时用白色背景填充两侧以填满整个宽度。
  • 页脚具有固定高度,它始终被推到页面底部并且在没有滚动条的情况下保持可见,与屏幕高度无关。

非常感谢。

最佳答案

将这个添加到你的 #content cs

  background: url(images/bg.jpg) no-repeat center center fixed; 
  -webkit-background-size: 100% 100%;
  -moz-background-size: 100% 100%;
  -o-background-size: 100% 100%;
  background-size:  100% 100%;

为什么它延伸到页脚?? 因为你的行 margin: 0 auto -120px;footer

working demo

即使 background-size: cover; 如果您删除 -120px 边距也能工作!!

cover demo

保持背景图像的纵横比:

aspect ratio demo
只需从我上面引用的 css 中删除 fixed 即可!

至于页脚留在底部

你可以使用 css calc() 方法:

类似的东西:

#footer{
  margin-top : calc(100% - footer_height_in_px); /* default */
  margin-top : -moz-calc(100% - footer_height_in_px); /* moz */
  margin-top : -webkit-calc(100% - footer_height_in_px); /* webkit browsers */

}

如果您感兴趣,请引用这些链接:

关于html - 背景图片和固定高度的粘性页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21108496/

相关文章:

javascript - 纯 JS 滑动菜单,能够关闭它 "on click outside of the menu"

javascript - Jquery - 使用 radio 在一页上显示多个幻灯片

html - 背景颜色和背景图像在 css 中不起作用

html - 为什么插入文本时红色方 block 会下降?

javascript - 如何使垂直线从一条线的顶部到底部改变不透明度

javascript - 如何将 html5 canvas 绘图下载为图像

html - 前缀和分类一切

html - 同时响应图像和文本

html - 正则表达式查找和替换插入\1 而不是字符串

javascript - 在滚动条上隐藏/显示 webkit 滚动条