So I want to scale an image down with css into the size of a container, while maintaining its aspect ratio. It seems easy enough to achieve this when specifying the minimum height OR width, but not both. Any ideas?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Find out if the height is less or the width is less; whichever dimension is less, set that property of the
imgbut not the other. Assuming you aren’t setting the size of theimgin CSS, this should maintain the aspect ratio while making the image as large as possible within the constraints of the container. This code assumes your container is adiv.EDIT:
If you aren’t using jQuery, you can use the following CSS trick to maintain aspect ratio. Change
100pxto the size of your container:Borrowed from CSS styling image height and width while maintaining aspect ratio.