简体   繁体   English

如何在CSS中的Flexbox中的img上保持长宽比?

[英]How to maintain aspect ratio on img in flexbox in css?

I have this grid of images. 我有这个图像网格。 I want no spacing in between. 我不希望两者之间有间隔。 Also the width should be 100% of parent. 宽度也应该是父级的100%。 Each column same size, in this case 20%. 每列大小相同,在这种情况下为20%。 The flex takes care of that. flex负责这一点。 However I want the height to main aspect ratio, but that is where the problem is. 但是我想要高度与主要的长宽比,但这就是问题所在。 The height of the images stays constant as I zoom in and out (or resize window). 当我放大和缩小(或调整窗口大小)时,图像的高度保持恒定。 It needs to be same aspect ratio as the current width of the image. 它的长宽比必须与图片的当前宽度相同。

Does anyone know how to fix this? 有谁知道如何解决这一问题?

HTML 的HTML

<div class="image-grid-container">
  <div class="image-grid">
    <img src="images/me/img01.png" />
    <img src="images/me/img02.png" />
    <img src="images/me/img03.png" />
    <img src="images/me/img04.png" />
    <img src="images/me/img05.png" />
  </div>
  <div class="image-grid">
    <img src="images/me/img06.png" />
    <img src="images/me/img07.png" />
    <img src="images/me/img08.png" />
    <img src="images/me/img09.png" />
    <img src="images/me/img10.png" />
  </div>
  <div class="image-grid">
    <img src="images/me/img11.png" />
    <img src="images/me/img12.png" />
    <img src="images/me/img13.png" />
    <img src="images/me/img14.png" />
    <img src="images/me/img15.png" />
  </div>
</div>

CSS 的CSS

.image-grid-container .image-grid {
    display:flex;
}
.image-grid-container .image-grid img {
    width:100%;
}

You should set each image inside div. 您应该在div中设置每个图像。

Something like this: 像这样:

<div class="img">
  <img src="http://placehold.it/200x200" />
</div>

And then in CSS 然后在CSS中

.image-grid-container .image-grid .img img {
  width: 100%;
}

Full example here: https://jsfiddle.net/cvru2yL5/ 此处的完整示例: https : //jsfiddle.net/cvru2yL5/

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

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