How to make side by side equal in height (CSS / HTML)?

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

I need an HTML / CSS way to do this - something that works for more than two images, if possible. I am currently manually calculating 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 />

Use the formula below (or trial and error) to find the 79/20 split. Note that 79 + 20 <100 - if I set it to 80/20, then the wrapper of the images is from abroad. How can I do this without any computation? The browser should do this 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...
+3
source share
4 answers

why not just set the value of the HEIGHT attribute of the image without attributing the width?

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

you can also achieve this through CSS, the same principle: you set the height, not the width. Zoom will be proportional ...

+2
source

If you don’t need to be scaled to the same size (to show the whole image), you can use the CSS property clip

img {

clip: rect(0 300px 300px 0;
position: absolute; /* the problem with using clip is that the element has to be absolutely positioned, which isn't always as awkward as it seems, but any content around it will need to be cleared somehow, perhaps with a top-margin? */

)

0

. , , .

<img src="myimage.jpg" height="100%" />
0
<img id="one" class="equal" style="width:50%;"/>
<img id="two" class="equal" style="width:50%;"/>
0

Source: https://habr.com/ru/post/1713321/


All Articles