How do I make side-by-side images equal height (CSS/HTML)?

12,415

why not simply setting the image HEIGHT attribute value, without attributing the width ?

so: <img height="300" src="path/to/image.png" />

you can also achieve this via CSS, same principle: you set the height, and not the width. The rescaling will be proportional...

Share:
12,415
Dan
Author by

Dan

Updated on June 30, 2022

Comments

  • Dan
    Dan almost 2 years

    Two images, one is 300x400 pixels (height x width), the other is 500x600. How can I make them appear side-by-side, scaled so they are the same height on screen, filling the whole width of the page (or container/div), without changing the aspect ratio of either image?

    I'd like an HTML/CSS way to do this -- something that works for more than 2 images as well if possible. Currently I manually calculate the width of each image (math below).

    EDIT:
    An example of what I'm trying to do:

    <img src="http://stackoverflow.com/content/img/so/logo.png"
         width="79%" style="float:left" border=1 />
    <img src="http://stackoverflow.com/content/img/so/vote-favorite-off.png"
         width="20%" border=1 />
    

    Used formula below (or trial and error) to come up with 79/20 split. Note that 79 + 20 < 100 -- if I set it to 80/20 then the images wrap due to the border. How can I do this without having to do any computation? The browser should be able to do it for me.

    ah1 = 300 // actual height of image 1
    aw1 = 400 // actual width of image 1
    ah2 = 500 // actual height of image 2
    aw2 = 600 // actual width of image 2
    
    // need to calculate these:
    dw1 // display width of image 1, expressed as percent
    dw2 // display width of image 2, expressed as percent
    dw1 + dw2 == 100%
    
    s1 = dw1 / aw1 // scale of image 1 (how much it has been shrunk)
    s2 = dw2 / aw2
    
    dh1 = ah1 * s1 // display height of image 1 = its actual height * its scale factor
    dh2 = ah2 * s2
    
    // need to solve this equality to get equal display heights:
                dh1 == dh2
           s1 * ah1 == s2 * ah2
    dw1 / aw1 * ah1 == dw2 / aw2 * ah2
    dw1 / aw1 * ah1 == (1 - dw1) / aw2 * ah2
    dw1 * aw2 * ah1 == (1 - dw1) * aw1 * ah2
    dw1 * aw2 * ah1 == aw1 * ah2 - aw1 * ah2 * dw1
    dw1 * aw2 * ah1 + aw1 * ah2 * dw1 == aw1 * ah2
    dw1 * (aw2 * ah1 + aw1 * ah2) == aw1 * ah2
    dw1 == aw1 * ah2 / (aw2 * ah1 + aw1 * ah2)
        == 400 * 500 / (600 * 300 + 400 * 500)
        == 20 / (18 + 20)
        == 20 / 38
        == 52.63% // which ends up as style="width:53%" which isn't quite right...
    
  • Dan
    Dan almost 15 years
    If I set the height though, how will I end up with both images taking up the full width of the screen? I would still have to manually calculate the correct height so the two images fill the whole width.
  • Dan
    Dan almost 15 years
    First time I've heard of 'clip'. Sounds like it will crop my image. I would like to have the whole images displayed, with original aspect ratios.
  • Dan
    Dan almost 15 years
    That sorta works, except I have to tweak or calculate the correct height to use so the width of the two images fills the whole width of the container.
  • pixeline
    pixeline almost 15 years
    so it means we have to calculate the width/height values for each image independently. You can only do that either on the clientside (javascript) or the serverside (via php for example) when outputting your gallery html markup. What's your choice?
  • Sikshya Maharjan
    Sikshya Maharjan almost 15 years
    It does, indeed, crop (or 'clip') the image ;) Yeah, I wasn't sure if you'd want them cropped or not, but since almost no-one uses clip I thought I'd point it out as a useful property. I would warn you that using CSS/(X)HTML to scale images is browser-intensive and not necessarily the most flattering way to show the images. It's not worse that way, it's just a cost of the method.