IE8非兼容模式,图像最大宽度和高度:auto

发布于 2024-08-12 03:31:03 字数 379 浏览 2 评论 0原文

我有一个带有此标记的图像

<img src="wedding_00.jpg" width="900" height="600" />

,我正在使用 CSS 将其缩小到 600px 宽度,如下所示:

img {
    max-width:600px;
    height:auto;
}

任何人都可以解释为什么此方法在兼容模式下有效,但在标准模式下无效?有没有办法修改我的CSS,使其在标准模式下工作?

我意识到如果我去掉它就

width="900" height="600"

可以解决问题,但这不是我的选择。

I have an image with this markup

<img src="wedding_00.jpg" width="900" height="600" />

And I am using CSS to downsize it to 600px width, like so:

img {
    max-width:600px;
    height:auto;
}

Can anyone explain why this method works in Compatibility mode, but not in standard mode? Is there a way I can modify my CSS so that it will work in standard mode?

I realize that if I strip out the

width="900" height="600"

that it solves the problem, but that is not an option I have.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

£冰雨忧蓝° 2024-08-19 03:31:03

我不确定根本原因,但如果你添加

width: auto;

它就可以了。

I'm not sure of the root cause but if you add

width: auto;

then it works.

埋葬我深情 2024-08-19 03:31:03

为 ie8 设置 width:inherit

 img {
   width:inherit; //for ie8
   max-width: 100%;
   height: auto;
 }

set width:inherit for ie8

 img {
   width:inherit; //for ie8
   max-width: 100%;
   height: auto;
 }
又爬满兰若 2024-08-19 03:31:03

哇,在那里节省了我很多时间!

我在位置:绝对位置的图像上遇到了类似的问题,其中宽度神奇地采用了最大宽度值。这很奇怪,因为当图像不在位置时它不会这样做:绝对。

width: auto; 
max-width: 200px; 
height: auto; 
max-height: 200px;

在 IE8 中运行得很好!

Wow, saved me a lot of time there!

i had a similar problem with an image in position: absolute where width was magically taking max-width value. Its weird because it doesn't do that when the image wasn't in position: absolute.

width: auto; 
max-width: 200px; 
height: auto; 
max-height: 200px;

works great in IE8!

帅气尐潴 2024-08-19 03:31:03

哇,IE 看起来总是很痛苦。虽然有一个公认的答案,但我发现它并没有解决我的问题。

经过多次搜索,我发现修复它的方法是从相关图像中删除高度和宽度属性。可以在这里找到解释:缩放图像IE8 with CSS max-width

代码如下:

CSS:

.entry img {
    max-width:615px
    height:auto;
}

SCRIPT

var imgs, i, w;
var imgs = document.getElementsByTagName( 'img' );
for( i = 0; i < imgs.length; i++ ) {
    w = imgs[i].getAttribute( 'width' );
    if ( 615 < w ) {
        imgs[i].removeAttribute( 'width' );
        imgs[i].removeAttribute( 'height' );
    }
}

现在我倾向于尽可能多地使用 jQuery,为了解决这个问题我使用了一个一些针对 IE8 的不同功能让我的生活更轻松。我还发现该解决方案几乎有效,但不完全有效。我反复尝试,直到能够达到我想要的结果。我的解决方案如下:

JQUERY:

var maxWidth = 500;

function badBrowser(){
if($.browser.msie && parseInt($.browser.version) <= 8){
    return true;
}   
    return false;
}


if(badBrowser()){
    $('img').each(function(){
        var height = $(this).height();
        var width = $(this).width();
        if (width > maxWidth) {

            var ratio = (maxWidth /width)  
            $(this).css({
                "width":maxWidth,               
                "height":(ratio*height)
            });
            $(this).removeAttr('width'); 
            $(this).removeAttr('height');
        }else{
            $("#cropimage").css({
                "width":width,               
                "height":height
            });
        }
    });       
}

我使用它来定位特定的图像加载函数,但它也可以添加到任何文档就绪或窗口加载函数中。

Wow, what a pain IE always seems to be. Although there is an accepted answer, I found that it did not solve my problem.

After much search I found that the way to fix it is to remove the height and width attributes from the images in question. An explanation can be found here: Scaling Images in IE8 with CSS max-width

The code is as follows:

CSS:

.entry img {
    max-width:615px
    height:auto;
}

SCRIPT

var imgs, i, w;
var imgs = document.getElementsByTagName( 'img' );
for( i = 0; i < imgs.length; i++ ) {
    w = imgs[i].getAttribute( 'width' );
    if ( 615 < w ) {
        imgs[i].removeAttribute( 'width' );
        imgs[i].removeAttribute( 'height' );
    }
}

Now I tend to use jQuery as much as possible, to solve this I used a few different functions to target IE8 and make my life easier. I also found that the solution almost worked, but not quite. I toyed around with it until I was able to achieve the results I was looking for. My solution is as follows:

JQUERY:

var maxWidth = 500;

function badBrowser(){
if($.browser.msie && parseInt($.browser.version) <= 8){
    return true;
}   
    return false;
}


if(badBrowser()){
    $('img').each(function(){
        var height = $(this).height();
        var width = $(this).width();
        if (width > maxWidth) {

            var ratio = (maxWidth /width)  
            $(this).css({
                "width":maxWidth,               
                "height":(ratio*height)
            });
            $(this).removeAttr('width'); 
            $(this).removeAttr('height');
        }else{
            $("#cropimage").css({
                "width":width,               
                "height":height
            });
        }
    });       
}

I use this to target a specific image load function, but it could be added to any document ready or window load function as well.

榕城若虚 2024-08-19 03:31:03

我对这个问题的解决方案是:

  <!--[if IE 8]>
  <style>
   a.link img{
          max-height:500px ;
          width:auto !important;
          max-width:400px;
          height:auto !important;
          width:1px; 

        }
  </style>
  <![endif]-->  

My solution for this issue was:

  <!--[if IE 8]>
  <style>
   a.link img{
          max-height:500px ;
          width:auto !important;
          max-width:400px;
          height:auto !important;
          width:1px; 

        }
  </style>
  <![endif]-->  
你不是我要的菜∠ 2024-08-19 03:31:03

当直接包装器 (div) 具有宽度时,图像的最大宽度在 IE8 上工作正常。

示例:
本例中的图像为 700px;
网页宽度为 900px;

Abc

如果您设置样式:
.wrapper1 { 宽度:50%;浮动:向左; } // 该列的宽度最大为 450px;

图像仍然是 700px,导致布局被破坏。

解决方案:
.wrapper1 { 宽度:50%;浮动:向左; } // 此列的宽度最大为 450px;
.wrapper2 { 宽度 100%;浮动:向左; } // 如果有边框或填充,宽度会变小 100%

当调整窗口大小时,图像将适合列(图像 = 450px),图像将根据wrapper2的宽度而变小。

问候,
江天

max-width of images just work fine on IE8 when it's directly wrapper (div) has width.

Example:
The image in this example is 700px;
The web's width is 900px;

<div class="wrapper1"><div class="wrapper2"><img style="max-width: 100%;" src="abc.jpg" alt="Abc" /></div></div>

if you style:
.wrapper1 { width: 50%; float: left; } // this column's width is 450px max;

The image still 700px and make the layout broken.

Solution:
.wrapper1 { width: 50%; float: left; } // this column's width is 450px max;
.wrapper2 { width 100%; float: left; } // if it has border or padding, width will smaller 100%

The the image will fit the column (image = 450px) when resize window smaller, image will smaller based on wrapper2's width.

Regards,
Cuong Sky

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文