简体   繁体   English

背景图片的高度和宽度在IE11上不起作用

[英]Background image height & width does't work on IE11

That is my client website- http://rubowarkitekter.dk/ I already code to make background image height & width according to adjust screen size/100% height & 100% width. 那是我的客户网站-http : //rubowarkitekter.dk/我已经编写了代码,可以根据调整屏幕尺寸/ 100%高度和100%宽度来制作背景图像的高度和宽度。 But that is not work on IE11. 但这不适用于IE11。

My css code- 我的CSS代码-

.home {
background: url(http://rubowarkitekter.dk/wp-content/uploads/2013/12/forside_carlsberg.jpg) center center no-repeat fixed;
background-size: cover;  
z-index: -500;  
-webkit-background-size: cover;  
-moz-background-size: cover;  
-o-background-size: cover;
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='.http://rubowarkitekter.dk/wp-content/uploads/2013/12/forside_carlsberg.jpg', sizingMethod='scale');/* To make IE work */
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://rubowarkitekter.dk/wp-content/uploads/2013/12/forside_carlsberg.jpg', sizingMethod='scale')"; /* To make IE work */  
}

Any idea how can i make background image to height & width 100% on IE11. 任何想法我怎么能使背景图像在IE11上达到高度和宽度100%。

Thanks 谢谢

IE11 screenshot- IE11截图- IE11截图

Chris Coyier of CSS-Tricks proposes 3 great solutions which works quite well, 2 of which being pure CSS. CSS-Tricks的Chris Coyier提出了3种很好的解决方案,它们效果很好,其中2种是纯CSS。

You can read up on this here 你可以在这里阅读

Examples 例子

Example 1 (fixed position -- ideal option) 示例1(固定位置-理想选择)

img.bg {
  /* Set rules to fill background */
  min-height: 100%;
  min-width: 1024px;

  /* Set up proportionate scaling */
  width: 100%;
  height: auto;

  /* Set up positioning */
  position: fixed;
  top: 0;
  left: 0;
}

@media screen and (max-width: 1024px) { /* Specific to this particular image */
  img.bg {
    left: 50%;
    margin-left: -512px;   /* 50% */
  }
}

Example 2 (inline image -- next best thing) 示例2(内嵌图片-最好的东西)

HTML HTML

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

CSS CSS

#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%;
}

Example 3 (uses filters -- less recommended) 示例3(使用过滤器-不建议使用)

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

  /* IE fallback support */
  filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='.myBackground.jpg', sizingMethod='scale');
  -ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='myBackground.jpg', sizingMethod='scale')";
}

The issue here is that the image is not a background image. 这里的问题是该图像不是背景图像。 From your code- 从您的代码-

<img src="http://rubowarkitekter.dk/wp-content/uploads/2013/12/forside_carlsberg.jpg" class="home">

This is an image element and not a background image added in CSS. 这是一个图像元素,而不是CSS中添加的背景图像

What you should instead be doing is adding the background image either to the "body" element or to your div wrapper. 您应该做的是将背景图像添加到“ body”元素或div包装器中。

There are a number of recommendations I feel it important to make- 我认为有很多建议很重要,

  1. Use the HTML 5 doctype rather than XHTML transitional 使用HTML 5文档类型,而不是XHTML过渡

  2. Remove the oncontextmenu event handler on your body element - it will not prevent someone saving your images if they want to, but will annoy your users. 删除body元素上的oncontextmenu事件处理程序-它不会阻止某人保存您的图像,但是会惹恼您的用户。

  3. Validate your site, there are 33 errors on the home page - which will mean inconsistent results in browsers for your users. 验证您的网站,主页上有33个错误-这意味着用户浏览器中的结果不一致。 Your site does not work correctly in Google Chrome. 您的网站无法在Google Chrome浏览器中正常运行。

  4. Organise your CSS, I cannot see that code you cited is actually exists in any of the loaded stylesheets (is it currently dev only?). 组织您的CSS,我看不到您引用的代码实际上已存在于任何已加载的样式表中(当前仅开发吗?)。

  5. Where-ever you use vendor prefixes (the -moz, -webkit etc.) these should appear before the standard property (without the prefix) so that it is used instead of the vendor prefix once the property is supported by the browser. 无论在何处使用供应商前缀(-moz,-webkit等),这些前缀都应出现标准属性(不带前缀)之前,以便浏览器支持该属性后,将使用该前缀代替供应商前缀。

  6. Clear your floats by using something like the CSS tricks clear-fix code. 通过使用CSS技巧的清晰修正代码清除浮动信息。 The social media widgets for example. 例如,社交媒体小部件。

  7. Do not use position:fixed or position:absolute for layout - you have not control over the viewport/device/window size your users are visiting on, so cannot assume a specific width. 请勿在布局中使用position:fixed或position:absolute-您无法控制用户所访问的视口/设备/窗口的大小,因此不能采用特定的宽度。

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

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