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="https://via.placeholder.com/150x50" />
</div>
<div>This will preserve aspect ratio and look right:</div>
<div class="correct">
<img src="https://via.placeholder.com/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>
你也可以在 <img>
标签中使用 height
属性。
像这样 <img src="/path/to/image" height="40">
,不需要任何 CSS。
和
标签。height: 100px;
i.e
.users-list>li img {
border-radius: 50%;
max-width: 100%;
height: auto;
width: 100px;
}
max-height:100px;
和max-width:100px;
替代height:100px;
和max-width:100%;
? - Sebastian Brosch