簡體   English   中英

如何使用純css自動調整圖像的響應式設計?

[英]How to auto resize the image for responsive design with pure css?

我試圖使用CSS屬性max-width自動調整圖像大小,但它在IE7和IE8中不起作用。 有沒有辦法在IE7和IE8中使用純CSS自動調整圖像大小?

使用width: inherit; 使它在IE8中使用純CSS。 (請參閱responsive-base.css 。)像這樣:

img {
  width: inherit;  /* This makes the next two lines work in IE8. */
  max-width: 100%; /* Add !important if needed. */
  height: auto;    /* Add !important if needed. */
}

我不確定它是否適用於IE7-請測試一下,如果您正在測試IE7,請告訴我們。 在我弄清楚width: inherit之前width: inherit技術我在下面使用jQuery,所以你可以試試它,如果你真的需要支持到IE7並且第一種技術不起作用:

<!--[if lt IE 9]><script>
jQuery(function($) {
    $('img').each(function(){
        // .removeAttr supports SSVs in jQuery 1.7+
        $(this).removeAttr('width height');
    });
});
</script><![endif]-->

IE 7和8不識別以下內容:

#my-div img
      {
         max-width:100%;
         _max-width:100%;
      }

嘗試這樣的事情:

width: expression(document.body.clientWidth > 800 ? "800px" : "auto" );

/* If page is wider than 800px then set width to 800px, otherwise set to auto */

來源 (值得一看)

您需要IE 6-7的一次性緩存表達式。

IMG {
zoom:expression(
    function(t){
        t.runtimeStyle.zoom = 1;
        var maxW = parseInt(t.currentStyle['max-width'], 10);
        var maxH = parseInt(t.currentStyle['max-height'], 10);
        if (t.scrollWidth > maxW && t.scrollWidth >= t.scrollHeight) {
            t.style.width = maxW;
        } else if (t.scrollHeight > maxH) {
            t.style.height = maxH;
        }
    }(this)
);
}

示例: http//kizu.ru/lib/ie/minmax.html JS源文件: http//kizu.ru/lib/ie/ie.js

作者: 羅曼科馬羅夫

大多數網絡開發人員都知道IE在標准競爭中落后,能夠展示最新和最好的標准。 許多CSS2屬性不受支持。 一些更有用的屬性是最大寬度 ,最大高度,最小寬度和最終最小高度等屬性。 試試這個:

<html>
<style>
p {
border:1px solid red;
width:expression( 
    document.body.clientWidth > (500/12) * 
    parseInt(document.body.currentStyle.fontSize)?
        "30em":
        "auto" );
}
</style>
<body>
<p>
[alot of text]
</p>

使用max-width: 100% with height:auto ,plus width:auto for IE8:

img 
 {
 max-width: 100%;
 height: auto;
 }

/* Media Query to filter IE8 */
@media \0screen 
  {
  img 
    { 
    width: auto;
    }
  }

因為您還需要支持媒體查詢..您可以使用以下polyfill為IE6-IE8添加對媒體查詢的支持

https://github.com/scottjehl/Respond (非常小,只需1-2kb縮小和gzip)然后使用以下css:

@media screen and (min-width: 480px){
    img{
     max-width: 100%;
     height: auto;
   }
}

我測試了它並為每個瀏覽器工作

img{
    height: auto;
    max-width: 100%;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM