Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS Responsive images – vertical images resize differently than horizontal images

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #36577
    daltonrooney
    Member

    I’ve come across this problem quite a bit when working with responsive images and I wonder if there’s a solution out there anywhere. Vertically oriented images resize differently than horizontally oriented images, based on the width of the screen. The result is that vertical images are often too tall relative to horizontal images on the page.

    As a practical example, say I’ve got a gallery of images of mixed aspect ratios – some landscape and some portrait, and the whole thing is based on a flexible grid. Here’s a screenshot, so far so good: http://cl.ly/0v1K3R3o0c0z2z092L2O

    Now, when I resize the page horizontally, each cell resizes and the images inside resize according to the width of the parent. This is perfect for landscape images, but the portrait images are taller than they are wide and therefore don’t resize at the same time. Now everything is out of whack: http://cl.ly/0K0g0B1b0O430j132R1N

    How does one get the vertical images to resize proportionally in conjunction with the horizontal images? I can share a link if anyone wants to see this in action.

    Cheers,
    Dalton

    #96476
    daltonrooney
    Member

    Here’s a link to an active example, if anyone is interested: http://test.folio.fm/galleries/cezanne/

    #96485
    daltonrooney
    Member

    OK, I’ve solved this for now with a little bit of javascript. Find the width of the parent div for each image, then set the max-height of the images to match. It’s a little wobbly (resizing is not perfectly smooth) but it works.

    Demo: http://test.folio.fm/galleries/cezanne/
    Relevant code:


    function adaptiveResize() {
    var $w, $target;
    $target = $('img');
    $target.each(function() {
    $w = $(this).parent().width();
    $target.css('max-height', $w );
    });
    }

    $(window).load(function() {
    adaptiveResize();
    });

    $(window).resize(function() {
    adaptiveResize();
    });

    Any better solutions out there?

    #96524
    tobeeornot
    Member
    #201738
    outline4
    Participant

    Hi daltonrooney.

    Did you ever find a better solution?
    Maybe in CSS?

    Cheers
    Stefan

    #201939
    rafaeljuarez
    Participant

    the best solution to a grid like that, when images are different height is using columns, not tables, not floats not inline block, like this:

    http://www.proyecto-internet.com/c
    or this http://www.raulsierra.co

    it looks much much better and its super easy to achieve.

    #250957
    Shikkediel
    Participant

    https://api.jquery.com/load-event/

    $(window).load(function() {
    

    I think an important chapter is missing – matching the aspect ratio of the screen with that of the image in order to render it as large as possible.

    Also don’t get why you’d use capitals on some of the CSS properties…

    Ironically, there’s some validation errors for the images.

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘CSS’ is closed to new topics and replies.