CSS method to make the image change size with the screen: 1. Use the "width height auto" attribute to scale the image; 2. Use the "max-width max-height" attribute to set the maximum width and height.
The operating environment of this tutorial: windows7 system, css3 version, Dell G3 computer.
Recommended: "css Video Tutorial"
Sometimes we want the image to scale with the screen size, two situations:
Single picture scaling
Picture scaling within the box
Nude picture scaling
<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>
The picture is in a box
<style type="text/css"> #box { width: auto; /* 盒子也要自动缩放 */ height: auto; max-width: 600px; max-height: 500px; /* 盒子的高度,需要大于图片100%宽度时 图片的高度 */ border: 5px solid yellow; } img { width: auto; height: auto; max-width: 100%; /* 只要宽度100% 结合前面 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>
ps:
1. Use width height auto to zoom
2. Use max-width max-height to set the maximum width and height. 100% means the original maximum.
The above is the detailed content of How to make the image change size with the screen in css. For more information, please follow other related articles on the PHP Chinese website!