javascript Internet Explorer 不支持最大宽度或最大高度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10286860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 09:19:54  来源:igfitidea点击:

Internet Explorer not honoring max-width or max-height

javascripthtmlcssinternet-explorer

提问by IAmYourFaja

I am trying to use CSS to scale my images to precise values:

我正在尝试使用 CSS 将我的图像缩放到精确值:

HTML:

HTML:

<div id="thumbnail-container">
    <img src="sample-pic.jpg"/>
</div>

CSS:

CSS:

#thumbnail-container img {
    max-height: 230px;
    max-width: 200px;
}

In this example, sample-pic.jpgis actually 320x211.

在这个例子中,sample-pic.jpg实际上是 320x211。

In Firefox 11.0, this code works perfectly fine, and FF resizes sample-pic.jpgto the 230x211 max constraints set forth in the CSS rule.

在 Firefox 11.0 中,此代码运行良好,并且 FF 调整sample-pic.jpg到 CSS 规则中规定的 230x211 最大限制。

In IE 9, however, the max-heightand max-widthrules are completely ignored and the image renders as its usual 320x211 size.

然而,在 IE 9 中,max-heightmax-width规则被完全忽略,图像呈现为其通常的 320x211 大小。

How can I modify the HTML/CSS so that both IE 9 and FF 11.0 both honor these max constraints?Thanks in advance!

如何修改 HTML/CSS 以便 IE 9 和 FF 11.0 都遵守这些最大约束?提前致谢!

回答by CWSpear

Make sure you've declared a doctype and that there's NO output or whitespace before it. For example, this is HTML5's doctype and it should go at the very top of your output:

确保你已经声明了一个 doctype 并且在它之前没有输出或空格。例如,这是 HTML5 的 doctype,它应该位于输出的最顶部:

<!doctype html>

Also, if IE is in compatibility mode, that can cause problems. Try adding the following to your <head>:

此外,如果 IE 处于兼容模式,则可能会导致问题。尝试将以下内容添加到您的<head>:

<!-- Forces user OUT of IE's compatibility mode and removes "broken page" icon --> 
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />

edit:I was able to test, and it seems that no doctypedoescause max-width not to work. Compatibility mode didn't seem to affect it (on this issue).

编辑:我能测试,它似乎没有doctype事业最大宽度不工作。兼容模式似乎没有影响它(在这个问题上)。

回答by Gabriele Petrioli

IE7+ supports max-widthand max-height.

IE7+ 支持max-widthmax-height.

Demo at http://jsfiddle.net/gaby/qE6w6/

演示在http://jsfiddle.net/gaby/qE6w6/

check that you page runs in standards mode.. (make sure there is a valid doctype)

检查您的页面是否在标准模式下运行..(确保有一个有效的文档类型