CSS force image resize and keep aspect ratio

前端 未结 23 1113
野趣味
野趣味 2020-11-22 10:04

I am working with images, and I ran across a problem with aspect ratios.

\"\"

23条回答
  •  一个人的身影
    2020-11-22 10:39

    img {
      max-width: 80px; /* Also works with percentage value like 100% */
      height: auto;
    }

    This image is originally 400x400 pixels, but should get resized by the CSS:

    Let's say the author of the HTML deliberately wants the height to be half the value of the width, this CSS will ignore the HTML author's wishes, which may or may not be what you want:

提交回复
热议问题