在Flexbox中居中和缩放图像

不分心

<img>将容器内的am (尺寸未知)设置为display:flexbox,是否可以满足以下条件:

  1. 如果图像的原始宽度小于容器的宽度,则它将在容器内水平居中。

在此处输入图片说明

  1. 如果图像的原始高度小于容器的高度,则图像在容器中垂直居中。

在此处输入图片说明

  1. 如果图像的原始宽度大于容器的宽度,则将图像缩放到容器的宽度。

在此处输入图片说明

  1. 如果图像的原始高度大于容器的高度,则将图像缩放到容器的高度。

在此处输入图片说明

I've noticed big disparities between how Chrome and Firefox treat images that are flexbox children. Chrome gets halfway there, but squashes the image vertically. Firefox gets halfway there, but squashes the image horizontally: Codepen

body,
html {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

.Container {
  height: 100%;
  width: 100%;
  background: red;
  display: flex;
  justify-content: center;
  align-items: center;
}

img{
  max-height: 100%;
  max-width: 100%;
}
<div class="Container">
    <img src="http://d13rap2ac6f4c9.cloudfront.net/image_assets/quarry_medium.jpg?1410945487">
</div>

This is the closest I can get Codepen, which fails on 4.:

body,
html {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

.Container {
  height: 100%;
  width: 100%;
  background: red;
  display: flex;
  justify-content: center;
  align-items: center;
}

.ImageWrapper {
  max-height: 100%;
  max-width: 100%;
}

img{
  max-height: 100%;
  max-width: 100%;
  width: 100%;
  height: auto;
}
<div class="Container">
  <div class="ImageWrapper">
    <img src="http://d13rap2ac6f4c9.cloudfront.net/image_assets/quarry_medium.jpg?1410945487">
  </div>
</div>

I know that this kind of behaviour was previously impossible without JavaScript, but I'm surprised that it doesn't seem to be achievable using flexbox.

Note: I'm not looking for a JavaScript solution or a solution that doesn't work in modern browsers yet.

In response to David Mann's answer, here is the previous example using object-fit Codepen.

body,
html {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
}

.Container {
  height: 100%;
  width: 100%;
  background: red;
  display: flex;
  justify-content: center;
  align-items: center;
}

img{
  max-height: 100%;
  max-width: 100%;
  object-fit: contain;
}
<div class="Container">
    <img src="http://d13rap2ac6f4c9.cloudfront.net/image_assets/quarry_medium.jpg?1410945487">
</div>

Here is a Codepen using the polyfill. Appears to work OK in IE10 but broken in IE11.

David Mann

CSS is catching up. It doesn't have the greatest support yet, but object-fit: contain does exactly what you want (EDIT 11/17: Edge now has partial support making IE11 the only browser with no support). Just put it in your css rule for img and change max-width: 100% and max-height: 100% to width: 100% and height: 100%. Here's a codepen.

.Container {
  height: 100vh;
  width: 100vw;
  background: red;
  display: flex;
  justify-content: center;
  align-items: center;
}

img {
  height: 100%;
  width: 100%;
  object-fit: contain;
}

body {
  margin: 0;
}
<div class="Container">
  <img src="https://unsplash.it/2000/1500">
</div>

If IE's lack of support for object-fit is a deal breaker, then there is a javascript polyfill for it. Here's a bit more about it from css-tricks.

You can also get the same effect with background-image but that just feels like cheating a bit.

只需将其添加到.Container

background-image: url(http://d13rap2ac6f4c9.cloudfront.net/image_assets/quarry_medium.jpg?1410945487);
background-size: contain;
background-repeat:no-repeat;
background-position: center;

然后完全删除img标签。效果是相同的,但是中没有实际元素.Container应该注意的是,display: flex甚至不需要这种方法

这是此方法的另一个代码笔

.Container {
  height: 100vh;
  width: 100vw;
  background: red;
  background-image: url(https://unsplash.it/2000/1500);
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}

body {
  margin: 0;
}
<div class="Container">
</div>

这里支持要好得多,您可以玩的更多。再次,这是css-tricks年历条目

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章