Home >Web Front-end >Front-end Q&A >How to make the image change size with the screen in css

How to make the image change size with the screen in css

藏色散人
藏色散人Original
2020-12-08 09:40:528333browse

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.

How to make the image change size with the screen in css

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>

How to make the image change size with the screen in css

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>

How to make the image change size with the screen in css

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn