是否有一种方法来调整(缩小)图像按比例使用CSS?
我用的是JavaScript的方式,只是想看看CSS是否可行。
是否有一种方法来调整(缩小)图像按比例使用CSS?
我用的是JavaScript的方式,只是想看看CSS是否可行。
当前回答
如果是背景图片,使用background-size:contain。
css例子:
#your-div {
background: url('image.jpg') no-repeat;
background-size:contain;
}
其他回答
img {
max-width:100%;
}
div {
width:100px;
}
使用这个代码片段,您可以以更有效的方式完成它
2015年回顾:
<img src="http://imageurl" style="width: auto; height: auto;max-width: 120px;max-height: 100px">
我重新访问了它,因为所有常见的浏览器现在都有上面Cherif建议的自动工作,所以它工作得更好,因为你不需要知道图像是否宽于高。
旧版本: 如果你被120x100的盒子限制,你可以这样做
<img src="http://image.url" height="100" style="max-width: 120px">
我相信这是最简单的方法,也可以通过<img>标记中的内联样式属性来实现。
.scaled
{
transform: scale(0.7); /* Equal to scaleX(0.7) scaleY(0.7) */
}
<img src="flower.png" class="scaled">
or
<img src="flower.png" style="transform: scale(0.7);">
控制规模,保持比例:
#your-img {
height: auto;
width: auto;
max-width: 300px;
max-height: 300px;
}
如果是背景图片,使用background-size:contain。
css例子:
#your-div {
background: url('image.jpg') no-repeat;
background-size:contain;
}