How to auto-resize an image while maintaining the aspect ratio?
How to auto-resize an image while maintaining aspect ratio?
23831-Jul-2023
Updated on 01-Aug-2023
Home / DeveloperSection / Forums / How to auto-resize an image while maintaining aspect ratio?
How to auto-resize an image while maintaining the aspect ratio?
Aryan Kumar
01-Aug-2023Sure, here are a few ways to auto resize an image while maintaining aspect ratio:
Using CSS
You can use the
width
andheight
properties of theimg
element to auto resize an image while maintaining aspect ratio. For example, the following CSS will resize an image to be 100px wide and will maintain the aspect ratio of the image:CSS
Using JavaScript
You can also use JavaScript to auto resize an image while maintaining aspect ratio. The following JavaScript code will resize an image to be 100px wide and will maintain the aspect ratio of the image:
JavaScript
Using the Sharp library
You can also use the Sharp library to auto resize an image while maintaining aspect ratio. The Sharp library is a popular image processing library for Node.js. The following code will resize an image to be 100px wide and will maintain the aspect ratio of the image:
JavaScript