Limit image width on portrait orientation images / preserve aspect ratio
Asked Answered
I

2

8

I have flexslider set up on a responsive site. The slider I am creating takes both landscape and portrait orientation images. I have smoothHeight: true set so the height adjusts to fit the images.

At the largest media query the flexslider container is 750px wide. This is fine for landscape orientation images but portrait orientation images become way too big. The width of the portrait image gets set to same size as the container - 750px wide, and so the height is at least double that and so you cannot view the whole image without scrolling.

In the css I tried setting max-height: 750px which solves the height problem but flexslider then stretches the image to fit 750px wide by 750px high.

Does anyone know of any way to get flexslider to preserve the aspect ratio and adjust the width of the image based on the max-height? So that the portrait images would be 750px high but maintain their aspect ratio.

Inscribe answered 23/10, 2012 at 11:35 Comment(0)
S
2

I Don`t understand about which functionality of Flexslider you talking - on zooming image or that thumbnails in scrolled toolbar? Whatever, there is one solution for both situations:

<style>
figure {
    outline: green solid 1px;
    padding: 2px;
}
img {
    width: 100%;
    height: 100%;
    display: block;
    position: relative;

    outline: red solid 1px;
    opacity: .5;
}
.scale img{
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    margin: 0 auto;
    vertical-align: middle;
}
.toheight img{
    max-width: none;
}
</style>
<figure class="scale toheight" width="200" height="200">
    <img src="http://placekitten.com/250/300"/>
</figure>

Solution for crop or scale image within container http://jsfiddle.net/iegik/ZzXru/

Related question: How to align cropped element horizontally?

Samaniego answered 2/11, 2012 at 19:49 Comment(0)
S
0

The image should maintain its ratio by default, unless you had a width: 100%; set too, in which case you should change this to use width: auto, then your max-height: 750px will take precedent

Screak answered 13/8, 2016 at 15:27 Comment(1)
This should be a commentMycosis

© 2022 - 2024 — McMap. All rights reserved.