Ask Your Question
3

How can CSS be used to maintain the aspect ratio of images while restricting maximum height and width?

asked 2022-11-09 11:00:00 +0000

lalupa gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-01-05 19:00:00 +0000

plato gravatar image

One way to maintain the aspect ratio of images while restricting maximum height and width using CSS is to set the max-width and max-height properties to restrict the size of the image, and set either the width or height property to auto to maintain the aspect ratio of the image.

Here is an example CSS code:

img {
  max-width: 500px; /* restrict the maximum width of the image to 500px */
  max-height: 400px; /* restrict the maximum height of the image to 400px */
  width: auto; /* maintain the aspect ratio of the image by setting width to auto */
  height: auto; /* maintain the aspect ratio of the image by setting height to auto */
}

This code will ensure that the image stays within the maximum allowed height and width, and will also maintain its original aspect ratio.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-11-09 11:00:00 +0000

Seen: 9 times

Last updated: Jan 05 '22