简体   繁体   English

如何根据容器高度按比例缩放多个图像?

[英]How can I make a number of images scale proportionally based on its container height?

I'm trying to figure out how to do this.我想弄清楚如何做到这一点。 I have 3 images, all in different but specific sizes that will scroll horizontally within a container (container will have overflow: hidden . That container should take up as much space as possible between two other divs and I'm doing that with flex-grow: 1 , and the container looks good.我有 3 张图像,所有图像的大小不同但特定,它们将在容器内水平滚动(容器将overflow: hidden 。该容器应在其他两个 div 之间占用尽可能多的空间,我正在使用flex-grow: 1做到这一点flex-grow: 1 ,容器看起来不错。

However, those images, how can I make sure they resize and keep proportions based on that container height, based on viewport height?但是,那些图像,我如何确保它们根据视口高度根据容器高度调整大小并保持比例?

I know maximum size the images can be in both width and height.我知道图像的最大尺寸可以是宽度和高度。

Please have a look at my example here: https://fvsu2.csb.app/请在这里查看我的示例: https://fvsu2.csb.app/

As you can see, the images wont resize at all if the container they are within is getting a lower height than the image itself...?如您所见,如果图像所在的容器的高度低于图像本身,则图像根本不会调整大小......? It just stops, and the bottom part goes below viewport.它只是停止,底部在视口下方。

Is this even possible?这可能吗?

U want your cardImage to be height:auto width:100%, what u have now is in reverse.你希望你的 cardImage 是高度:自动宽度:100%,你现在拥有的是相反的。

you could try this:你可以试试这个:

  1. do not set height for images, it will distort the image不要为图像设置高度,它会扭曲图像
  2. set container height to auto将容器高度设置为自动
  3. set images width to 100%将图像宽度设置为 100%

example:例子:

.container{height: auto}
.container .images{width: 100%}

in case of overflow, set this在溢出的情况下,设置这个

.container{height: auto; overflow: hidden}

but the images will not overflow out of the container if you set the images width to width: 100% and make sure the width of the container is in px or use bootstrap to set the width of the container eg但如果将图像宽度设置为 width: 100% 并确保容器的宽度以 px 为单位或使用 bootstrap 设置容器的宽度,则图像不会溢出容器,例如

<div class="col-lg-12 col-md-12 col-sm-12"></div>

for the container, with this the images will never overflow the container对于容器,图像永远不会溢出容器

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

相关问题 如何按比例缩小 <img> WxH中的较短者设置为固定数字? - How can I proportionally scale down an <img> element with the shorter of its WxH set to a fixed number? 如何将精灵表图像缩放到容器? - How can I scale sprite sheet images to a container? 如何根据容器的高度从 css 或 javascript 生成行号 - How can I generate line number from css or javascript based on height of container 如何根据 html/css/bootstrap 中的子项数量使父 div 缩放? - How can I make parent div scale, based on number of children in html/css/bootstrap? 如何在容器内对 hover 进行图像缩放? - How can i make image scale on hover inside the container? 如何使用内部宽度按比例缩放我的网页以适应任何显示尺寸? - How can I scale my webpage proportionally using inner width to fit any display size? 如何在按比例调整大小的图像中获得项目的新高度? - How can I get the new height of an item inside a proportionally resized image? 如何在`transform:scale()`之前获取元素的位置(相对于其容器)? - How can I get an element's position (relative to its container) before `transform: scale()`? 如何缩放条形图列以使其正确且成比例地装入其容器? - How can I scale a bar graph column to fit properly and proportionately inside its container? 按比例缩放图像以适合其父 object - scale image to fit its parent object proportionally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM