简体   繁体   English

flexbox中长宽比未知的等高图像

[英]Equal height images in flexbox with unknown aspect ratio

I have images in a horizontal flex container.我在水平 flex 容器中有图像。 They should all be responsive, have the same height, while keeping their aspect ratio, and without being cropped:它们都应该是响应式的,具有相同的高度,同时保持它们的纵横比,并且没有被裁剪:

flexbox 中相同高度的图像

This can be accomplished with this CodePen: https://codepen.io/sarus/pen/PJGPmy这可以通过这个 CodePen 来完成: https ://codepen.io/sarus/pen/PJGPmy

The main problem with this approach is that you need to know the aspect ratios of all images in advance:这种方法的主要问题是你需要提前知道所有图像的纵横比:


img {
  width: 100%;
  height: auto;
}

.pics_in_a_row {
  display: flex;
}

.img1 { flex: 1.3344; }
.img2 { flex: 1.3345; }
.img3 { flex: 0.7505; }

Is there a way to make this work with arbitrary images and unknown aspect ratios, with pure CSS (no JavaScript)?有没有办法使用纯 CSS(无 JavaScript)来处理任意图像和未知纵横比?

You can use the css property object-fit .您可以使用 css 属性object-fit

img {
  object-fit: cover;
  height:50px;
  width:100px;
  }

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

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