简体   繁体   English

让背景图像“拉伸以适应”我的页面背景

[英]Have background-image “stretch to fit” the background of my page

I have an image I would like to use as a background image but I need the height of it to stretch to fit 100% from header to footer, and then I can drop a background-color:black; 我有一个想用作背景图像的图像,但是我需要将它的高度拉伸到从页眉到页脚的100%,然后我可以删除背景颜色:黑色; behind it to make it perfect. 在它背后使它完美。

Is there any way to do this? 有没有办法做到这一点? Currently I am trying to use this CSS: 目前我正在尝试使用这个CSS:

#wrapper .bg{
    background: url(../images/sky.png) no-repeat; 
    position:absolute; 
    top:0;
    left:0;
    width:100%; 
    height:100%;
    display:block;
    z-index:-1;
}

This ends up leaving some space between the image and the footer, the same amount I am guessing as if the image wasn't there. 这最终会在图像和页脚之间留下一些空间,我估计的数量与图像不在那里相同。

If you aren't supporting older browsers you can use css3 to achieve this. 如果您不支持旧浏览器,则可以使用css3来实现此目的。

body {
  background-image: url(myimage.png);
  background-size: cover;
}

No, what you need to do is add an image tag inside the body and set the height to 100%. 不,您需要做的是在体内添加图像标签并将高度设置为100%。 Apply any z-index tweaks with CSS so it remains behind your content. 使用CSS应用任何z-index调整,使其保留在您的内容之后。

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

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