Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS scale several images to fit viewport keeping aspect ratio

Tags:

I'm creating a gallery where I want to list all images from an album on one page : Big Picture style :

Unlike Big Picture, I want the images to proportionally scale to fit to the width of the container div (.section-images, which is set to margin: auto 2em; so its width is the width of the user's browser minus 2*2em) as much as possible without making them larger than 90% height of the viewport.

As you imagine, a 400x600 portrait photo when scaled to fit the width would be so long the user would not see the whole image on screen, thats why I need to limit width scaling to not exceed 90% height.

<section class="section-images">     <div class="image-box">         <div class="image-large">             <a href="#"><img foo1></a>         </div>         <div class="image-description">           blabla1         </div>         ... MORE IMAGES WITH DESCRIPTONS ...     </div>     etc </section> 

Under each image I have the .image-description, the height of this will vary.

What is important is that at any one time a single image (so .image-large) should fit in the viewport, regardless of whether it is landscape or portrait.

I would like to do this using only CSS if possible, and only javascript if CSS is not possible.

like image 302
MindOverflow Avatar asked Apr 16 '14 13:04

MindOverflow


People also ask

How do you preserve aspect ratio when scaling images?

Press-and-hold the Shift key, grab a corner point, and drag inward to resize the selection area. Because you're holding the Shift key as you scale, the aspect ratio (the same ratio as your original photo) remains exactly the same.

How do I resize an image and keep the aspect ratio CSS?

The Simple Solution Using CSSBy setting the width property to 100%, you are telling the image to take up all the horizontal space that is available. With the height property set to auto, your image's height changes proportionally with the width to ensure the aspect ratio is maintained.

How do I fit an image IMG inside a div and keep the aspect ratio?

Answer: Use the CSS max-width Property You can simply use the CSS max-width property to auto-resize a large image so that it can fit into a smaller width <div> container while maintaining its aspect ratio.

How do you scale a proportionally image in CSS?

In that situation we can use CSS max-width or width to fit the image. Use max-width: 100% to limit the size but allow smaller image sizes, use width: 100% to always scale the image to fit the parent container width.


2 Answers

Solution : (I stripped most of your containers to make it simple to understand and work with)

html,  body,  .section-images {    height: 100%;    margin: 0;  }  .section-images {    margin: auto 2em;    text-align: center;  }  img {    display: block;    width: auto;    height: auto;    max-width: 100%;    max-height: 90%;    margin: 20px auto;  }
<section class="section-images">    <a href="#"><img src="http://i.imgur.com/hMRnvUx.jpg" /></a>    <div class="image-description">blabla3<br/>more blablah<br/>and more blablah</div>    <a href="#"><img src="http://i.imgur.com/lBuIEDh.jpg" /></a>    <div class="image-description">blabla2</div>    <a href="#"><img src="http://i.imgur.com/k8BtMvj.jpgg" /></a>    <div class="image-description">blabla3<br/>more blablah<br/>and more blablah</div>  </section>

Explanation :

This solution is based on the fact that percentages sizes (width and height) are relative the size of the parent element.

Considering .section-images is a direct child of <body> start by setting :

html, body, .section-images {     width:100%;     height:100%;     margin:0; } 

So the direct parent of the images equals viewport size.

Then, you can size your images easily using :

img{     width:auto;     height:auto;     max-width:100%;     max-height:90%; } 

Images will never exceed 100% width and 90% height of viewport while keeping their aspect ratio. And they will stay in the flow of the document.

like image 87
web-tiki Avatar answered Sep 22 '22 14:09

web-tiki


You can also use viewport units to do this without changing html, body for IE9+.

img {     width:auto;     height:auto;     max-width:100%;     max-height:90vh; } 
like image 40
DonutReply Avatar answered Sep 19 '22 14:09

DonutReply