简体   繁体   English

如何在具有方向的 flex 容器中垂直拟合图像:列

[英]How to fit an image vertically in a flex container with direction: column

I'm trying to fit an image vertically in a flex container which has a specific height.我正在尝试将图像垂直放置在具有特定高度的 flex 容器中。

The flex-direction is column , and the image is contained in a flex-item with flex-basis: 100% . flex-directioncolumn ,图像包含在flex-basis: 100%flex-item

In addition, the image's max-height is 100% .此外,图像的max-height100%

As you can see in the example, the image does not fit into the red container.正如您在示例中看到的,图像不适合红色容器。

 #container { background-color: red; display: flex; flex-direction: column; flex-wrap: wrap; height: 200px; width: 320px; justify-content: space-between; } #container > * { padding: 5px; } #img { flex: 0 1 100%; /* height: 100%; */ } img { max-height: 100%; max-width: 100%; }
 <div id="container"> <div id="img"> <img src="https://placeholdit.imgix.net/~text?txtsize=33&txt=img&w=340&h=500"> </div> <div> Something else </div> </div>

Shouldn't the image shrink to fit vertically into the flex container, according to the specification?根据规范,图像不应该收缩以垂直放入 flex 容器吗?

The workaround I found is to set the height of #img to 100% , but I have the sensation that it's not the way it should be done.我发现的解决方法是将#imgheight设置为100% ,但我感觉这不是应该完成的方式。

As an additional note, if I set flex-direction: row to the container, it fits the image horizontally (which is the behaviour I would expect).作为附加说明,如果我将flex-direction: row设置为容器,它会水平适合图像(这是我期望的行为)。

You wrote:你写了:

The workaround I found is to set the height of #img to 100%, but I have the sensation that it's not the way it should be done.我找到的解决方法是将#img的高度设置为 100%,但我感觉这不是应该完成的方式。

Actually, it is the way it should be done.事实上,这应该做的。 The predominant implementation of the spec requires that the height property be applied to the parent when using percentage heights on the child.规范的主要实现要求在子级上使用百分比高度时将height属性应用于父级。 (Although this is slowly evolving. See my second reference below.) (尽管这是在慢慢演变。请参阅下面我的第二个参考。)

References:参考:

For some reason, I couldn't get the <img> to behave (probably because it's a replaced element ).出于某种原因,我无法让<img>表现出来(可能是因为它是一个 被替换的元素)。 So I removed it, and used the .img div with an image as background.所以我删除了它,并使用带有图像的.img div 作为背景。

Relevant Changes相关变化

.container {
...
  justify-content: center;
  align-items: center;
}

.img {
  flex: 1 0 auto;
  background: url(https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png) no-repeat;
  background-size: contain;
  background-position: center;
  width: 100%;
}

SNIPPET片段

 .container { background-color: red; display: flex; flex-direction: column; flex-wrap: wrap; height: 200px; width: 320px; justify-content: center; align-items: center; } .container > * { padding: 5px; } .somethingElse { outline: 1px dashed yellow; background: rgba(128,0,255,.3); color: white; } .img { flex: 1 0 auto; background: url(https://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png) no-repeat; background-size: contain; background-position: center; width: 100%; }
 <figure class="container"> <div class="img"> </div> <figcaption class="somethingElse"> True Dimentions: 512 x 512 px </figcaption> </figure>

try this css.试试这个css。 its works fine.它的工作正常。

#container {
    background-color: red;
    /*display: flex;*/
    flex-direction: column;
    flex-wrap: wrap;
    height: 200px;
    width: 320px;
    justify-content: space-between;
}

#img {
  height: 85%;
  width: 100%;
}

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

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