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.
Asked: 2022-11-09 11:00:00 +0000
Seen: 9 times
Last updated: Jan 05 '22