<img>
将容器内的am (尺寸未知)设置为display:flexbox
,是否可以满足以下条件:
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.
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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句