full size background image in ie8

2019-06-04 06:39发布

问题:

I used this property in css but its not working in IE8

html{
     filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/bg.jpg', sizingMethod='scale');
     -ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/bg.jpg', sizingMethod='scale')"; 
    }

body {
    background-image:url(../../images/bg.jpg);
    background-repeat:no-repeat;
    background-size: cover;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    }

I got many solutions on stackoverflow but none of them worked.

How can I solve this?

回答1:

Try this

<div id="bg">
  <img src="images/bg.jpg" alt="">
</div>

#bg {
  position: fixed; 
  top: -50%; 
  left: -50%; 
  width: 200%; 
  height: 200%;
}
#bg img {
  position: absolute; 
  top: 0; 
  left: 0; 
  right: 0; 
  bottom: 0; 
  margin: auto; 
  min-width: 50%;
  min-height: 50%;
}


回答2:

I made a Sass Mixin if this help anyone:

@mixin background-cover($cover-image){
  background: url($cover-image) no-repeat center center;
  -webkit-background-size: cover;
  -moz-background-size: cover;
  -o-background-size: cover;
  background-size: cover;
  -ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='#{$cover-image}', sizingMethod='scale')";
  filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='#{$cover-image}', sizingMethod='scale');
}

Call it by using

@include background-cover('/images/foo.png');


回答3:

I know this is late, but this answer may help someone else:

Download backgroundsize.min.htc and put it inside your project.

Now simply add these lines in your css:

.class_name{
    //your other properties
    background-size: cover;
    -ms-behavior: url(backgroundsize.min.htc);
}

NOTE: use the url according to your project setup.

Enjoy this simple solution. :)



回答4:

I think generic tags should go after the browser-specific ones:

-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
 background-size: cover;

and also:

-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/bg.jpg', sizingMethod='scale')";
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/bg.jpg', sizingMethod='scale');

Hope this works.