有時我們希望圖片能隨屏幕大小的縮放而縮放,兩種情況:
- 單個圖片縮放
- 圖片在盒子內(nèi)縮放
裸圖片的縮放
<style type="text/css">
img {
width: auto;
height: auto;
max-width: 100%;
max-height: 100%;
}
</style>
<body>
<img src="https://i.loli.net/2018/06/17/5b2639a6e6c61.jpeg" alt="timg.jpeg" title="timg.jpeg" />
</body>

圖片在一個盒子內(nèi)
<style type="text/css">
#box {
width: auto; /* 盒子也要自動縮放 */
width: auto;
max-width: 600px;
max-height: 500px; /* 盒子的高度,需要大于圖片100%寬度時 圖片的高度 */
border: 5px solid yellow;
}
img {
width: auto;
height: auto;
max-width: 100%; /* 只要寬度100% 結(jié)合前面 auto就可以了 */
}
</style>
<body>
<div id="box">
<img src="https://i.loli.net/2018/06/17/5b2639a6e6c61.jpeg" alt="timg.jpeg" title="timg.jpeg" />
下面的文字
</div>
</body>

image.png
ps:
1 、利用width height auto 來縮放
2、利用max-width max-height 來設(shè)置最大寬高,為100%表示可原始最大。