简体   繁体   English

Flexbox:如何在Flex项目中使图像高度相等?

[英]Flexbox: How to make images equal height in flex items?

Okay, so I'm trying to display images with equal width and height inside 6 flexbox items. 好的,所以我试图在6个flexbox项中显示宽度和高度相等的图像。

In screen size with min-width: 768px each flex-item grows to 1 when there is 30% space, ie flex: 1 30%; min-width: 768px像素的屏幕尺寸中,当有30%的空间时,每个flex-item都会增加为1,即flex:1 30%; flex-direction: is changed to row; flex-direction:更改为行;

The problem : The flex item divs themselves are equal in height and width but the image inside each flex item are various sizes. 问题 :柔性物品的div本身是在高度和宽度相等的,但每一个柔性物品内部的图像是各种尺寸。

Attempt : Tried align-items: stretch; 尝试 :尝试过的对齐项目:拉伸; and even giving min-height to each image doesn't work? 甚至给每个图像最小高度都行不通吗?

Also is flex-wrap even necessary when using flex: 1 30%? 在使用flex时,甚至还需要flex-wrap:1 30%?

I'm clearly doing something wrong, any ideas? 我显然做错了什么,有什么想法吗?

 /* ## Services */ .services-wrap { text-align: center; padding-top: 4rem; } .services-flex { display: flex; flex-direction: column; flex-wrap: wrap; } .service-img { min-height: 100% !important; /*width: 100%;*/ } @media screen and (min-width: 768px) { .services-flex { flex-direction: row; padding: 0 4rem; flex-wrap: wrap; align-items: stretch; max-width: 1500px; margin: auto; } .flex-box { flex: 1 30%; /*margin: 1rem;*/ } } 
 <section class="services-wrap"> <h3>OUR SERVICE</h3> <hr> <div class="services-flex"> <div id="service-box" class="flex-box"> <img class="service-img" src="<?php bloginfo('template_url'); ?>/images/service-img/comercial.jpg"> </div> <div id="service-box" class="flex-box"> <img class="service-img" src="<?php bloginfo('template_url'); ?>/images/service-img/residential.jpg"> </div> <div id="service-box" class="flex-box"> <img class="service-img" src="<?php bloginfo('template_url'); ?>/images/service-img/rural.jpg"> </div> <div id="service-box" class="flex-box"> <img class="service-img" src="<?php bloginfo('template_url'); ?>/images/service-img/industrial.jpg"> </div> <div id="service-box" class="flex-box"> <img class="service-img" src="<?php bloginfo('template_url'); ?>/images/service-img/other.jpg"> </div> <div id="service-box" class="flex-box"> <img class="service-img" src="<?php bloginfo('template_url'); ?>/images/service-img/other.jpg"> </div> </div> </section> 

UPDATE UPDATE

Okay I think the issue was that 2 of the 6 image sizes were to small like 200 by 250 vs the rest which was 600-1200px. 好的,我认为问题在于6个图像尺寸中的2个尺寸很小,如200 x 250,而其余的尺寸为600-1200px。 So I've fixed this by having larger image sizes initial so the image is not distorted and also grows in equal. 因此,我已经通过较大的初始图像大小来解决此问题,以使图像不会失真,并且图像会均匀增长。

Notice the first and the last image heights are not equal, but why? 注意第一个和最后一个图像高度不相等,但是为什么呢?

Please check the CSS again as I have updated it with the changes to match the image above! 请重新检查CSS,因为我已对其进行了更新,以使其与上面的图像相匹配!

Please help, thank you! 请帮忙,谢谢!

This is not a technical problem, this is conceptual problem. 这不是技术问题,这是概念性问题。 When you have images of varying aspect ratios and you want to display them in an orderly, even mannor, then first you have to think whether you want: 当您拥有不同宽高比的图像并且想要以有序甚至均匀的方式显示它们时,那​​么首先您必须考虑是否要:

a) the images to be distorted or trunctated to leave no whitespace a)要失真或被截断的图像,不留空白

or 要么

b) the images to have whitespace between them or around them. b)图像之间或周围具有空白。 In this case you can also make decisions on how you want to distribute the whitespaces. 在这种情况下,您还可以决定如何分配空白。

Those are your basic choices. 这些是您的基本选择。

For a) you can set the images width to 100% or the image wrapper to overflow-y: hidden 对于a),您可以将图像宽度设置为100%,或将图像包装器设置为溢出-y:隐藏

For b) you can set max-width and max-height and and center your images in the wrappers with centering method of your choice. 对于b),您可以设置max-width和max-height并使用您选择的居中方法将图像在包装器中居中。

As a bonus possibility you can also set the flex-basis to auto and flex-grow to 0 and let the flex-box decide how many images are displayed before the row-wrap. 作为一种额外的可能性,您还可以将flex-basis设置为auto并将flex-grow设置为0,并让flex-box决定行换行之前显示多少个图像。 On large 4k displays you might not want two rows of enlarged images anyways 在大型4k显示器上,您可能始终不希望两行放大的图像

Could you try setting the image as a background image inside the flex div? 您可以尝试在flex div中将图像设置为背景图像吗? As then you can do: background-size:cover; 然后,您可以执行以下操作:background-size:cover; This will ensure all the div is covered by the image, although some of it may be missing depending on its proportions in comparison to the div. 这将确保所有div都被图像覆盖,尽管根据与div的比例可能会丢失其中的一些。 If you still want the alt tag you could try adding a transparent png placeholder inside the div which can contain the alt text. 如果您仍然想要alt标签,则可以尝试在div中添加一个可以包含alt文本的透明png占位符。

I've used this css min-height:100%; 我已经使用过此CSS min-height:100%; for smaller images to expand to parent flex-box items. 以便将较小的图像扩展到父级flex-box项目。 I've also hardcoded min-height: 220px; 我还对min-height: 220px;硬编码min-height: 220px; to parent items just to have all equal height.. see the snippet in full screen to see how it works.. 到父项的高度都相等。.全屏查看摘要,以了解其工作原理。

 /* ## Services */ .services-wrap { text-align: center; padding-top: 4rem; } .services-flex { display: flex; flex-direction: column; flex-wrap: wrap; } @media screen and (min-width: 768px) { .services-flex { flex-direction: row; padding: 0 4rem; flex-wrap: wrap; align-items: stretch; max-width: 1500px; margin: auto; } .flex-box { flex: 1 30%; margin: 1rem; min-height: 220px; } } img { min-height: 100%!important; } 
 <section class="services-wrap"> <h3>OUR SERVICE</h3> <hr> <div class="services-flex"> <div id="service-box" class="flex-box"> <img class="service-img" src="http://placehold.it/125"> </div> <div id="service-box" class="flex-box"> <img class="service-img" src="http://placehold.it/155"> </div> <div id="service-box" class="flex-box"> <img class="service-img" src="http://placehold.it/175"> </div> <div id="service-box" class="flex-box"> <img class="service-img" src="http://placehold.it/195"> </div> <div id="service-box" class="flex-box"> <img class="service-img" src="http://placehold.it/205"> </div> <div id="service-box" class="flex-box"> <img class="service-img" src="http://placehold.it/225"> </div> </div> </section> 

Here's a solution for your problem if you don't want the images to get distorted but rather if you would have them cropped in position just to fill the div containing it inside the flexbox. 如果您不希望图像失真,而是希望将图像裁剪到适当的位置以将包含图像的div填充到flexbox中,则这是解决问题的方法。

.flex-box{
  height:200px;
  overflow-y: hidden;
}

img {
    width: 300px;
    border: 0;
}

This sets the height of the parent container as fixed and hides if the image overflows. 这会将父容器的高度设置为固定值,如果图像溢出则将其隐藏。 And the image maintains a fixed width inside the parent div. 并且图像在父div内保持固定的宽度。

If not, your other alternative is as I mentioned in the comment is to set the width and height for the image to a fixed value. 如果不是这样,您的另一种选择就是我在评论中提到的将图像的宽度和高度设置为固定值。

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

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