有两张图片,一张是300x400像素(高 x 宽),另一张是500x600。我该如何使它们并排显示,缩放后在屏幕上具有相同的高度,填满页面(或容器/ div)的整个宽度,而不改变任何一张图片的纵横比?
我想要一种HTML / CSS方法来实现这一点,如果可能的话,能够适用于超过2张图片的情况。目前,我手动计算每个图像的宽度(下面有数学公式)。
编辑:
我尝试做的事情的例子:
<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 />
使用以下公式(或试错法)得出79/20的拆分比例。请注意,79 + 20 < 100 - 如果我将其设置为80/20,则由于边框而导致图像换行。如何在不进行任何计算的情况下完成此操作?浏览器应该能够为我完成这项工作。
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...