如何自动调整大小的大图像,使其适应较小宽度的
容器,同时保持其宽度:高度比例?
示例:stackoverflow.com - 当将图像插入到编辑面板上,并且图像太大无法适应页面时,图像会自动调整大小。
示例:stackoverflow.com - 当将图像插入到编辑面板上,并且图像太大无法适应页面时,图像会自动调整大小。
storm.jpg
的图片进行了测试。<html>
<head>
<style type="text/css">
#myDiv
{
height: auto;
width: auto;
}
#myDiv img
{
max-width: 100%;
max-height: 100%;
margin: auto;
display: block;
}
</style>
</head>
<body>
<div id="myDiv">
<img src="images/storm.jpg">
</div>
</body>
</html>
我这样在超链接中水平和垂直居中等比例缩放了一张图片:
#link {
border: 1px solid blue;
display: table-cell;
height: 100px;
vertical-align: middle;
width: 100px;
}
#link img {
border: 1px solid red;
display: block;
margin-left: auto;
margin-right: auto;
max-height: 60px;
max-width: 60px;
}
该内容已在Internet Explorer、Firefox和Safari中进行了测试。
有关居中的更多信息,请参见此处。
您需要告诉浏览器放置位置的高度:
.example {
height: 220px; /* DEFINE HEIGHT */
background: url('../img/example.png');
background-size: 100% 100%;
background-repeat: no-repeat;
}
%
表示相对大小或自动缩放(基于图像容器或显示屏)px
(或其他)表示静态或设置的尺寸<img style="width:100%; height:auto;"
src="https://googledrive.com/host/0BwDx0R31u6sYY1hPWnZrencxb1k/thanksgiving.png"
/>
Thorn007的被接受的答案在图片太小时不起作用。
为了解决这个问题,我添加了一个缩放因子。这样,它会使图像变大并填充div容器。
例子:
<div style="width:400px; height:200px;">
<img src="pix.jpg" style="max-width:100px; height:50px; transform:scale(4); transform-origin:left top;" />
</div>
注意事项:
-webkit-transform:scale(4); -webkit-transform-origin:left top;
。所有提供的答案,包括被接受的答案,仅在假设 div
包装器具有固定大小的情况下工作。因此,无论 div
包装器的大小如何,以下是如何操作的,并且如果您开发响应式页面,则非常有用:
在您的 DIV
选择器中编写以下声明:
width: 8.33% /* Or whatever percentage you want your div to take */
max-height: anyValueYouWant /* (In px or %) */
然后将这些声明放在你的IMG
选择器内:
width: "100%" /* Obligatory */
max-height: anyValueYouWant /* (In px or %) */
非常重要:
maxHeight
的值必须对DIV
和IMG
选择器相同。
最简单的方法是使用object-fit
属性:
<div class="container">
<img src="path/to/image.jpg">
</div>
.container{
height: 300px;
}
.container img{
height: 100%;
width: 100%;
object-fit: cover;
}
img-responsive
类并更改为.container img{
object-fit: cover;
}
// Detects when the window width is too narrow for the current image
// aspect-ratio, and fits it to height 100% instead of width 100%.
const photo = document.images[0]
const onPhotoResize = new ResizeObserver(entries =>
window.requestAnimationFrame(checkRatio)
)
onPhotoResize.observe(photo.parentNode)
function checkRatio(){
const photoParent = photo.parentNode,
imageAspectRatio = photo.clientWidth / photo.clientHeight,
parentAspectRatio = photoParent.clientWidth / photoParent.clientHeight
photo.classList[imageAspectRatio > parentAspectRatio ? 'add':'remove']('max')
}
.box{
width: 20%;
height: 60%;
margin: auto;
position: absolute;
top:0; left:0; right:0; bottom:0;
resize: both;
overflow: hidden;
border: 5px solid red;
}
.box > img{
position: absolute;
top: 50%;
left: 50%;
width: 100%;
transform: translate(-50%, -50%);
}
.box > img.max{ width:auto; height:100%; }
<div class='box'>
<img src="https://upload.wikimedia.org/wikipedia/commons/6/6a/Mona_Lisa.jpg">
</div>
<img class="img-responsive" src="img_chania.jpg" alt="Chania">
通过一点数学,解决方案就容易了...
只需将图片放入一个div中,在HTML文件中指定图片时,使用图像的像素值以百分比设置宽度和高度值,以计算宽高比。
例如,假设有一张宽为200像素,高为160像素的图片。可以安全地说,宽度值将为100%,因为它是较大的值。然后,为计算高度值,只需将高度除以宽度,得出80%的百分比值。在代码中,它看起来像这样...
<div class="image_holder_div">
<img src="some_pic.png" width="100%" height="80%">
</div>