【问题标题】:Max-width and max-height for images in Internet Explorer 7Internet Explorer 7 中图像的最大宽度和最大高度
【发布时间】:2013-07-27 13:23:32
【问题描述】:

Internet Explorer 又一次浪费了我的时间和金钱。

我正在制作一个响应式网站,我需要我的图片不超过其包含元素的 100% 宽度,但也不能超过一定百分比的高度,以防它们从页面上掉下来。

一些 CSS:

#content{

    margin:0 auto;
    min-width:320px;
    max-width:800px;
    width:80%;
    background-color:green;

}

#featured{

    position:relative;
    width:100%;

}

#featured-images{

    text-align:center;

}

#featured-images img{

    max-height:80%;
    height:auto !important; /* IE fix */
    height:80%; /* IE fix */

    max-width:100%;
    width:auto !important; /* IE fix */
    width:100%; /* IE fix *

}

一些标记:

<div id="content">
  <div id="featured">
    <div id="featured-images">
      <img src="lib/imgs/fi-1.jpg"/>
    </div>
  </div>
</div>

目前,此页面适用于 Chrome。它甚至适用于 IE6 和 IE8+。我还没有在 Firefox 或 Opera 中测试过它。但是 IE 7 绝对不会玩。它似乎将图像缩小到相当小的程度,就好像浏览器已被调整为树桩一样。

我知道这不太理想,但我一直在使用IE NetRenderer 进行测试。

【问题讨论】:

  • 您使用的是 min-width hack 而不是 max width hack
  • @Hushme 你能详细说明一下吗?还是提供替代方案?
  • 你正在使用 100% 为什么你需要最大宽度?
  • @Hushme 因为图像的大小可能比包含的元素大。如果没有最大宽度,它们将溢出 #featured-images div。
  • 必须有人对此有一个非 JavaScript、非显式宽度/高度的解决方案......

标签: html css internet-explorer


【解决方案1】:

Internet Explorer 条件注释样式表...

http://www.jabcreations.com/web/css/ieccss

在不启用 JavaScript 的情况下工作。

不需要破解,如果 IE 需要错误的值(例如高度/宽度)而不是您使用的值,那么只有您需要应用这些伪正确值的 IE 版本才能工作。

这将让您将所有与 IE 相关的讨厌的东西排除在您的主样式表之外,并且您可以为每个版本的 IE 使用特定的 IECCSS。

【讨论】:

  • 我知道条件 CSS,但实际上我只是想让它工作。一旦我知道什么有效,什么无效,将实现样式表的整洁(ish)分离。
【解决方案2】:

已修复,您可以在这里查看:

<style type="text/css">
#content {
    margin:0 auto;
    min-width:320px;
    max-width:800px;
    width:80%;
    background-color:green;
}
#featured {
    position:relative;
    width:100%;
}
#featured-images {
    text-align:center;
}
#featured-images img {
    max-height:100%;
    max-width:100%;
    height:auto;
    width:auto;
}
</style>

<div id="content">
    <div id="featured">
        <div id="featured-images">
            <img src="https://www.google.co.in/images/srpr/logo4w.png" />
        </div>
    </div>
</div>

或者在这里提琴http://jsfiddle.net/Fqebe/1/

干杯!

【讨论】:

  • 那么max-height 呢?我不希望图像超过当前浏览器高度的 80%...
  • @shennan:当然可以把max-height:100%;改成max-height:80%;,完全没有问题。
  • 抱歉,有点昏暗。这似乎成功了——尽管它把 IE6 搞砸了。但我对 IE7 目前的工作感到更满意。我可以单独破解 IE6。出于兴趣,为什么在 max-widthmax-height 属性之后粘贴 auto 似乎可以解决问题?
猜你喜欢
  • 2011-02-16
  • 1970-01-01
  • 2021-04-09
  • 2013-09-23
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多