简体   繁体   English

试图将背景拉伸到100%的高度

[英]Trying to stretch background to 100% height

I am trying to create a full page div (with 100% width & height). 我正在尝试创建一个整页的div(宽度和高度为100%)。 But in firefox it is not stretching fully. 但是在Firefox中,它并没有完全伸展。 Here's my code:- 这是我的代码:-

div{
    background:url('image.jpg')no-repeat fixed;
    background-size:cover;
 }

I've tried a lot,but I don't know what's wrong. 我已经尝试了很多,但是我不知道怎么了。

Actually there's nothing wrong with your CSS as such, but in order for it to work properly you need to(unfortunately) add browser prefix 实际上,您的CSS本身没有任何问题,但是为了使其正常工作,您需要(不幸地)添加浏览器前缀
try adding 尝试添加

 div{
    background:url('image.jpg')no-repeat fixed;
    background-size:cover;
    -moz-background-size:cover;
  }

Be sure you set the height of html, body . 确保设置html, body的高度html, body

By default, the html and body elements do not take up the entire height of the window. 默认情况下, htmlbody元素不占据窗口的整个高度。 You have to explicitly set the height of html, body to force them to span the entire height of the window. 您必须显式设置html, body的高度html, body以强制它们跨越窗口的整个高度。

html, body {
    height:100%;
}

By forcing the html, body to take up the entire height of the window and then placing the div { width:100%; height:100%; } 通过强制html, body占据窗口的整个高度,然后放置div { width:100%; height:100%; } div { width:100%; height:100%; } div { width:100%; height:100%; } as a child of the body element you can ensure that the div will stretch to 100% of the window's width and height. div { width:100%; height:100%; }作为body元素的子元素,您可以确保div可以拉伸到窗口宽度和高度的100%。

Here's an example: http://jsfiddle.net/ewF8M/ 这是一个示例: http : //jsfiddle.net/ewF8M/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM