图片高宽固定1(利用盒子模型进行定位)
<div class="box">
<img src="./101.jpg" />
</div>
.box {
border: 1px solid red;
width: 200px;
height: 200px;
position: relative;
}
.box img {
width: 100px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
margin: -50px 0 0 -50px;
}
图片高宽固定2(利用行高等于容器高)
<div class="box">
<img src="./101.jpg"/>
</div>
.box {
border: 1px solid red;
width: 200px;
height: 200px;
line-height: 200px;
text-align: center;
}
.box img {
width: 100px;
height: 100px;
vertical-align: middle;
}
父元素高宽已知,图片大小未知
- 图片的高宽不能超过父元素的高宽,否则不能达到预期效果
- html
<div class="box">
<img src="./101.jpg" alt="">
</div>
.box {
width: 250px;
height: 250px;
border: 1px solid red;
display: table-cell;
vertical-align: middle;
text-align: center;
*display: block;
*font-size: 300px;
*font-family: Arial;
}