Home >Web Front-end >CSS Tutorial >How Can I Maintain Aspect Ratio When Resizing Images with CSS?
Preserving Aspect Ratio During Image Resizing
Correcting the aspect ratio discrepancy in image resizing can be achieved through CSS modifications.
The HTML code originally specifies both width and height attributes for the image, which override the CSS rule. To allow the CSS to take precedence, the height attribute should be removed.
Additionally, the max-width property in the CSS rule limits the width of the image. However, the height is not explicitly set. To ensure the aspect ratio is maintained, the height should also be limited, but in a way that respects the original aspect ratio.
The snippet provided suggests using CSS properties display: block, max-width: 230px, max-height: 95px, width: auto, and height: auto. These settings allow the image to expand within the max-width and max-height constraints while maintaining its original aspect ratio. Therefore, the image will resize without distortion or cropping.
The above is the detailed content of How Can I Maintain Aspect Ratio When Resizing Images with CSS?. For more information, please follow other related articles on the PHP Chinese website!