tags:

views:

658

answers:

4

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...
+2  A: 

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...

pixeline
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
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?
pixeline
A: 

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

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? */

)

David Thomas
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
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.
David Thomas
[cont]...also using 'clip' means that you don't have to worry about original sizes (unless you want to do a check for the smallest image's dimensions and hold that as the clipped size). Whereas using the CSS/XHTML requires, from the look of it, some complex calculations. Unless you go arbitrary and stick with a particular set of dimensions.
David Thomas
A: 

Setting the height should work. Remember you can use relative values too, like percentages.

<img src="myimage.jpg" height="100%" />
Andrew Mason
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.
Dan
A: 

< img id="one" class="equal" style="width:50%"/> < img id="two" class="equal" style="width:50%"/>

That makes them equal width, not equal height.
Dan