简体   繁体   English

如何使并排图像的高度相等(CSS / HTML)?

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

Two images, one is 300x400 pixels (height x width), the other is 500x600. 两个图像,一个是300x400像素(高x宽),另一个是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? 如何让它们并排显示,缩放以使它们在屏幕上的高度相同,填充整个页面宽度(或容器/ div),而不改变任一图像的纵横比?

I'd like an HTML/CSS way to do this -- something that works for more than 2 images as well if possible. 我想要一种HTML / CSS方式来实现这一点 - 如果可能的话,这也适用于2个以上的图像。 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. 使用下面的公式(或反复试验)得出79/20分割。 Note that 79 + 20 < 100 -- if I set it to 80/20 then the images wrap due to the border. 请注意,79 + 20 <100 - 如果我将其设置为80/20,则图像会因边框而换行。 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...

why not simply setting the image HEIGHT attribute value, without attributing the width ? 为什么不简单地设置图像HEIGHT属性值,而不是归因于宽度?

so: <img height="300" src="path/to/image.png" /> 所以: <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. 你也可以通过CSS实现这一点,原理相同:你设置高度,而不是宽度。 The rescaling will be proportional... 重新缩放将成比例...

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 如果您不需要将它们缩放到相同的大小(以显示整个图像),则可以使用CSS clip属性

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

)

Setting the height should work. 设置高度应该有效。 Remember you can use relative values too, like percentages. 请记住,您也可以使用相对值,例如百分比。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM