是否有一种方法来调整(缩小)图像按比例使用CSS?

我用的是JavaScript的方式,只是想看看CSS是否可行。


当前回答

使用这种简单的缩放技术

img {
    max-width: 100%;
    height: auto;
}
@media {
  img { 
    width: auto; /* for ie 8 */
  }
}

其他回答

img{
    max-width:100%;
    object-fit: scale-down;
}

对我有用。它缩小较大的图像以适应方框,但较小的图像保持原始大小。

<img style="width: 50%;" src="..." />

对我来说还行……还是我遗漏了什么?

编辑:但请看肖恩关于不小心放大尺寸的警告。

使用CSS按比例调整图像大小:

img.resize {
    width:540px; /* you can use % */
    height: auto;
}

Try

transform: scale(0.5, 0.5);
-ms-transform: scale(0.5, 0.5);
-webkit-transform: scale(0.5, 0.5);

试试这个:

div.container {
    max-width: 200px;//real picture size
    max-height: 100px;
}

/* resize images */
div.container img {
    width: 100%;
    height: auto;
}