我需要使这张图像拉伸到最大尺寸,而不溢出它的<div>或倾斜图像。

我无法预测图像的纵横比,所以没有办法知道是否使用:

<img src="url" style="width: 100%;">

or

<img src="url" style="height: 100%;">

我不能同时使用(即style="width: 100%;高度:100%;"),因为这将拉伸图像以适应<div>。

<div>的大小由屏幕的百分比设置,这也是不可预测的。


当前回答

设置外部容器div的宽度和高度。然后在img上使用下面的样式:

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

这将帮助你保持你的img的纵横比

其他回答

这里发现的许多解决方案都有一些限制:一些不能在IE(对象适合)或旧浏览器中工作,其他解决方案不能缩放图像(只缩小它),许多解决方案不支持调整窗口大小,许多不是通用的,要么期望固定分辨率或布局(纵向或横向)

如果使用javascript和jquery不是一个问题,我有这个解决方案基于@Tatu Ulmanen的代码。我修复了一些问题,并添加了一些代码,以防图像是动态加载的,在开始时不可用。基本上,这个想法是有两个不同的css规则,并在需要时应用它们:一个是当限制是高度时,所以我们需要在两侧显示黑条,另一个是当限制是宽度时,所以我们需要在顶部/底部显示黑条。

function applyResizeCSS(){
    var $i = $('img#imageToResize');
    var $c = $i.parent();
    var i_ar = Oriwidth / Oriheight, c_ar = $c.width() / $c.height();  
    if(i_ar > c_ar){
        $i.css( "width","100%");
        $i.css( "height","auto");          
    }else{
        $i.css( "height","100%");
        $i.css( "width","auto");
    }
}   
var Oriwidth,Oriheight;
$(function() {
    $(window).resize(function() {
        applyResizeCSS();
    });

    $("#slide").load(function(){
        Oriwidth  = this.width,
        Oriheight = this.height; 
        applyResizeCSS();
    }); 

    $(window).resize();
}); 

对于像这样的HTML元素:

<img src="images/loading.gif" name="imageToResize" id="imageToResize"/> 

使用这种方法,你可以在div中填充不同比例的div和图片。

jQuery:

$(window).load(function(){
   $('body').find(.fillme).each(function(){
      var fillmeval = $(this).width()/$(this).height();
      var imgval = $this.children('img').width()/$this.children('img').height();
      var imgClass;
      if(imgval > fillmeval){
          imgClass = "stretchy";
      }else{
          imgClass = "stretchx";
      }
      $(this).children('img').addClass(imgClass);
   });
});

HTML:

<div class="fillme">
   <img src="../images/myimg.jpg" />
</div>

CSS:

.fillme{
  overflow:hidden;
}
.fillme img.stretchx{
  height:auto;
  width:100%;
}
.fillme img.stretchy{
  height:100%;
  width:auto;
}

如果可以的话,使用背景图像并设置background-size: cover。这将使背景覆盖整个元素。

CSS

div {
  background-image: url(path/to/your/image.png);
  background-repeat: no-repeat;
  background-position: 50% 50%;
  background-size: cover;
}

如果你坚持使用内联图像,有几个选项。首先,有

object-fit

此属性作用于图像、视频和其他类似于background-size: cover的对象。

CSS

img {
  object-fit: cover;
}

遗憾的是,浏览器的支持并不是很好,直到IE版本11都不支持它。下一个选项使用jQuery

CSS + jQuery

HTML

<div>
  <img src="image.png" class="cover-image">
</div>

CSS

div {
  height: 8em;
  width: 15em;
}

自定义jQuery插件

(function ($) {
  $.fn.coverImage = function(contain) {
    this.each(function() {
      var $this = $(this),
        src = $this.get(0).src,
        $wrapper = $this.parent();

      if (contain) {
        $wrapper.css({
          'background': 'url(' + src + ') 50% 50%/contain no-repeat'
        });
      } else {
        $wrapper.css({
          'background': 'url(' + src + ') 50% 50%/cover no-repeat'
        });
      }

      $this.remove();
    });

    return this;
  };
})(jQuery);

像这样使用插件

jQuery('.cover-image').coverImage();

它将获取一张图像,将其设置为图像包装器元素的背景图像,并从文档中删除img标记。最后你可以用

纯CSS

你可以用这个作为后备。图像会放大以覆盖它的容器,但不会缩小。

CSS

div {
  height: 8em;
  width: 15em;
  overflow: hidden;
}

div img {
  min-height: 100%;
  min-width: 100%;
  width: auto;
  height: auto;
  max-width: none;
  max-height: none;
  display: block;
  position: relative;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

希望这对大家有所帮助,编码愉快!

如果你想设置一个最大宽度或高度(这样它就不会很大),同时保持图像的纵横比,你可以这样做:

img{
   object-fit: contain;
   max-height: 70px;
}

你可以用object-fit: cover;在父div上。

https://css-tricks.com/almanac/properties/o/object-fit/