Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintaining aspect ratio with min/max height/width?

I have a gallery on my site where users can upload images.

I would like the images to sit in a div that maintains its height, the images should be no larger than 500px in height. The width should be automatic to maintain aspect ratio.

HTML:

<div id="gallery">     <img src="uploads/my-dynamic-img.jpg"> </div> 

I've tried this CSS:

#gallery{     height: 500px;      img{         max-height: 500px;         max-width: 100%;     } } 

The above works well, the gallery is always 500px high and images never exceed 500px in height. I run into problems though with smaller images, if a user uploads a really small image, I would like it 'blown up' to a minimum of 200px. I know this can be achieved by setting a min-height on the image, the problem with this is, if the image is less than 200px in height but say, 2000px wide, the image gets blown up to 200px in height, but then the aspect ratio is screwed, as the image is wider than the images parent div.

How can I have a min height but retain aspect ratio?

like image 780
panthro Avatar asked Nov 23 '14 21:11

panthro


People also ask

How do you maintain an aspect ratio?

In the CSS for the <div>, add a percentage value for padding-bottom and set the position to relative, this will maintain the aspect ratio of the container. The value of the padding determines the aspect ratio. ie 56.25% = 16:9.

What does it mean to maintain aspect ratio?

If you select the Maintain Aspect Ratio check box, the embedded Mimic will retain its original proportions. This means that when you resize the embedded Mimic, its width and height will maintain their relationship, for example, if you reduce the height, the width will also reduce automatically.

Why is it important to maintain the aspect ratio of an image when it is resized?

When scaling your image, it's crucial to maintain the ratio of width to height, known as aspect ratio, so it doesn't end up stretched or warped. If you need a specific width and height, you may need a mixture of resizing and cropping to get the desired result.

What does aspect ratio depend on?

An aspect ratio is a proportional relationship between an image's width and height. Essentially, it describes an image's shape. Aspect ratios are written as a formula of width to height, like this: 3:2. For example, a square image has an aspect ratio of 1:1, since the height and width are the same.


2 Answers

The property you're looking for is object-fit. This is one of Opera's innovations, you can read more about it in their 2011 dev article on object-fit (yep, it's been around that long). A few years ago, I wouldn't have been able to recommend it to you, but caniuse shows that everyone else is starting to catch up:

  • Opera 10.6-12.1 (-o- prefix)
  • Chrome 31+
  • Opera 19+
  • Safari 7.1+
  • iOS 8+
  • Android 4.4+

http://caniuse.com/#search=object-fit

#gallery img {     -o-object-fit: contain;     object-fit: contain; } 

Using a value of contain will force the image to maintain its aspect ratio no matter what.

Alternately, you might want to use this instead:

#gallery img {     -o-object-fit: cover;     object-fit: cover;     overflow: hidden; } 

http://sassmeister.com/gist/9b130efdae95bfa4338e

like image 179
cimmanon Avatar answered Oct 02 '22 10:10

cimmanon


The only way that I know of to possibly accomplish this is by setting either the width or height to auto.

#gallery{     height: 500px;      img{         max-height: 500px;         width: auto;     } } 
like image 41
EternalHour Avatar answered Oct 02 '22 11:10

EternalHour