CSS: 100% width or height while keeping aspect ratio?

If you only define one dimension on an image the image aspect ratio will always be preserved.

Is the issue that the image is bigger/taller than you prefer?

You could put it inside a DIV that is set to the maximum height/width that you want for the image, and then set overflow:hidden. That would crop anything beyond what you want.

If an image is 100% wide and height:auto and you think it’s too tall, that is specifically because the aspect ratio is preserved. You’ll need to crop, or to change the aspect ratio.

Please provide some more information about what you’re specifically trying to accomplish and I’ll try to help more!

— EDIT BASED ON FEEDBACK —

Are you familiar with the max-width and max-height properties? You could always set those instead. If you don’t set any minimum and you set a max height and width then your image will not be distorted (aspect ratio will be preserved) and it will not be any larger than whichever dimension is longest and hits its max.

Leave a Comment