如果您不希望图像拉伸,则应固定一个尺寸,而将另一个尺寸设置为auto
。(这将保留图像的长宽比)
请参见下面的示例,其中width
在height
自动调整时保持不变:
img {
display: block;
}
.correct,
.incorrect {
border: 1px solid red;
display: inline-block;
}
.incorrect img {
max-width: 100%;
width: 100px;
height: 100px;
}
.correct img {
max-width: 100%;
width: 200px;
height: auto;
}
<div>This one stretches out:</div>
<div class="incorrect">
<img src="http://placehold.it/150x50" />
</div>
<div>This will preserve aspect ratio and look right:</div>
<div class="correct">
<img src="http://placehold.it/150x50" />
</div>
请参见下面的示例,其中height
在width
自动调整时保持不变:
img {
display: block;
}
.correct,
.incorrect {
border: 1px solid red;
display: inline-block;
}
.incorrect img {
max-height: 100%;
height: 100px;
width: 100px;
}
.correct img {
max-height: 100%;
height: 200px;
width: auto;
}
<div>This one stretches out:</div>
<div class="incorrect">
<img src="http://placehold.it/150x50" />
</div>
<div>This will preserve aspect ratio and look right:</div>
<div class="correct">
<img src="http://placehold.it/150x50" />
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句