Categories
CSS

How to force image to resize and keep aspect ratio with CSS?

Spread the love

To force image to resize and keep aspect ratio with CSS, we set the width and height to auto.

For instance, we write

img {
  display: block;
  max-width: 230px;
  max-height: 95px;
  width: auto;
  height: auto;
}

to set the max width and height of the img element.

And we set the width and height to auto to make the image keep its aspect ratio while keeping the size as close to the max width and height as possible.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *