【问题标题】:Why doesn't flex direction column with align-items=center center image properly为什么使用 align-items=center center 图像不能正确弯曲方向列
【发布时间】:2019-11-18 06:43:16
【问题描述】:

我在direction:column 的弹性框中偶然发现了图像居中问题。

假设你在一个 flexbox 中有两个元素,其中第一个元素包含一个图像:

<div class="container">
    <div class="image-container">
        <img class="img" src="https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg">
    </div>
    <div class="another-flex-child">
        Random content here
    </div>
</div>


.container {
  height: 300px;
  background-color: green;
  display: flex;
  flex-direction: column;

  .image-container {
    flex: 1;
    align-self: center;

    .img {
      height: 100%;
    }
  }

  .another-flex-child {
    flex: none;
    background-color: red;
  }
}

我希望图像在 div 内水平居中,但图像的左边框似乎正好位于 div 的中心。

当我用另一个包含一些文本的 div 替换图像时,它按预期放置。

谁能给我解释一下那里发生了什么?

Checkout this fiddle

【问题讨论】:

  • 你需要检索什么?两个div必须水平对齐?我的意思是左边的图像和右边的其他 div?
  • 我不明白你的评论...
  • 我的意思是,您只想将图像和下方的文本水平居中吗?
  • 不,我想使用整个屏幕,图像应该填满所有剩余空间,而其他元素应该只占据它们需要的空间
  • 这不是有效的 CSS。您必须使用 LESS 或 SCSS 或其他一些处理器。最好标记或以其他方式让人们知道那是什么。

标签: html css flexbox less


【解决方案1】:

因为您的包含图像的&lt;div&gt;(并且上面有align-self: center)默认是块级元素,并且默认情况下具有100% 的width。因此,它相对于 parent 是受限的。

为了使您的图像正确居中,您需要添加display: contents:

container .image-container {
  display: inline;
}

这可以在下面看到:

.container {
  height: 300px;
  background-color: green;
  display: flex;
  flex-direction: column;
}

.container .image-container {
  flex: 1;
  align-self: center;
  display: contents;
}

.container .image-container .img {
  height: 100%;
}

.another-flex-child {
  flex: none;
  background-color: red;
}

.spacer {
  height: 20px;
}
<div class="container">
  <div class="image-container">
    <img class="img" src="https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg">
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

<div class="spacer"></div>

<div class="container">
  <div class="image-container">
    <div>Properly centered content</div>
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

【讨论】:

  • 也只添加“溢出:自动;”在 image-container 上使一切看起来都符合预期
  • 默认是块级元素,默认宽度为100%。因此,它相对于父级受到限制。 --> 你的意思是什么?那个 div 是一个弹性项目并且没有宽度:100% 你的图像也没有真正居中,因为它被拉伸以填充所有容器(添加一些边框来查看这个)
  • @TemaniAfif 实际上是对的——接受他的回答...
【解决方案2】:

问题是您使用的 SVG 没有固有尺寸,只有固有比率,因此您的图像的宽度等于 0,这使得其居中容器的宽度也等于 0。

这里是之前使用height:100%

.container {
  height: 300px;
  background-color: green;
  display: flex;
  flex-direction: column;
}

.image-container {
  flex: 1;
  align-self: center;
  border:2px solid blue;
}

.img {
  /*height: 100%;*/
}

.another-flex-child {
  flex: none;
  background-color: red;
}

.spacer {
  height: 20px;
}
<div class="container">
  <div class="image-container">
    <img class="img" src="https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg">
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

<div class="spacer"></div>

<div class="container">
  <div class="image-container">
    <div>Properly centered content</div>
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

设置height:100%后,图片将填满所有空间并保持其比例,但您会出现溢出,因为浏览器不会再返回计算容器的宽度:

.container {
  height: 300px;
  background-color: green;
  display: flex;
  flex-direction: column;
}

.image-container {
  flex: 1;
  align-self: center;
  border:2px solid blue;
}

.img {
  height: 100%;
}

.another-flex-child {
  flex: none;
  background-color: red;
}

.spacer {
  height: 20px;
}
<div class="container">
  <div class="image-container">
    <img class="img" src="https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg" >
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

<div class="spacer"></div>

<div class="container">
  <div class="image-container">
    <div>Properly centered content</div>
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

为避免这种情况,请给图像一个宽度并确保添加min-height:0 to the container to allow it to shrink

.container {
  height: 300px;
  background-color: green;
  display: flex;
  flex-direction: column;
}

.image-container {
  flex: 1;
  align-self: center;
  border:2px solid blue;
  min-height:0;
}

.img {
  height: 100%;
}

.another-flex-child {
  flex: none;
  background-color: red;
}

.spacer {
  height: 20px;
}
<div class="container">
  <div class="image-container">
    <img class="img" src="https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg" width="250">
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

<div class="spacer"></div>

<div class="container">
  <div class="image-container">
    <div>Properly centered content</div>
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

如果您最初使用的是具有固有尺寸的图像,则不会遇到此问题,也无需定义宽度。您只需添加min-height:0 即可避免溢出:

.container {
  height: 300px;
  background-color: green;
  display: flex;
  flex-direction: column;
}

.image-container {
  flex: 1;
  align-self: center;
  border:2px solid blue;
  min-height:0;
}

.img {
  height: 100%;
}

.another-flex-child {
  flex: none;
  background-color: red;
}

.spacer {
  height: 20px;
}
<div class="container">
  <div class="image-container">
    <img class="img" src="https://picsum.photos/id/1/400/400">
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

<div class="spacer"></div>

<div class="container">
  <div class="image-container">
    <div>Properly centered content</div>
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

请注意,上述方法在 Firefox 中的工作方式不同,您需要添加 text-aling:center 以确保它在任何地方都可以正常工作:

.container {
  height: 300px;
  background-color: green;
  display: flex;
  flex-direction: column;
}

.image-container {
  flex: 1;
  align-self: center;
  border:2px solid blue;
  text-align:center;
  min-height:0;
}

.img {
  height: 100%;
}

.another-flex-child {
  flex: none;
  background-color: red;
}

.spacer {
  height: 20px;
}
<div class="container">
  <div class="image-container">
    <img class="img" src="https://picsum.photos/id/1/400/400">
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

<div class="spacer"></div>

<div class="container">
  <div class="image-container">
    <div>Properly centered content</div>
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

您会注意到差异与容器的宽度计算有关,由于使用height:100%而有点复杂

如果图像的尺寸非常小,情况可能会变得更糟:

.container {
  height: 300px;
  background-color: green;
  display: flex;
  flex-direction: column;
}

.image-container {
  flex: 1;
  align-self: center;
  border:2px solid blue;
  text-align:center;
  min-height:0;
}

.img {
  height: 100%;
}

.another-flex-child {
  flex: none;
  background-color: red;
}

.spacer {
  height: 20px;
}
<div class="container">
  <div class="image-container">
    <img class="img" src="https://picsum.photos/id/1/50/50">
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

<div class="spacer"></div>

<div class="container">
  <div class="image-container">
    <div>Properly centered content</div>
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

在 Firefox 中,text-align:center 什么都不做,你可能需要一个嵌套的 flexbox 容器

.container {
  height: 300px;
  background-color: green;
  display: flex;
  flex-direction: column;
}

.image-container {
  flex: 1;
  align-self: center;
  justify-content:center;
  border:2px solid blue;
  display:flex;
  min-height:0;
}

.img {
  height: 100%;
}

.another-flex-child {
  flex: none;
  background-color: red;
}

.spacer {
  height: 20px;
}
<div class="container">
  <div class="image-container">
    <img class="img" src="https://picsum.photos/id/1/50/50">
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

<div class="spacer"></div>

<div class="container">
  <div class="image-container">
    <div>Properly centered content</div>
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

以下问题几乎与您使用初始 SVG 时遇到的问题相同,该问题可以使用相同的代码进行修复,但不会消除溢出:

.container {
  height: 300px;
  background-color: green;
  display: flex;
  flex-direction: column;
}

.image-container {
  flex: 1;
  align-self: center;
  display:flex;
  justify-content:center;
  border:2px solid blue;
}

.img {
  height: 100%;
}

.another-flex-child {
  flex: none;
  background-color: red;
}

.spacer {
  height: 20px;
}
<div class="container">
  <div class="image-container">
    <img class="img" src="https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg" >
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

<div class="spacer"></div>

<div class="container">
  <div class="image-container">
    <div>Properly centered content</div>
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

要注意的另一件有趣的事情是,如果您将height:100% 添加到容器中,您的初始代码可能会正常工作,从而更容易计算嵌套高度:

.container {
  height: 300px;
  background-color: green;
  display: flex;
  flex-direction: column;
}

.image-container {
  flex: 1;
  align-self: center;
  border:2px solid blue;
  box-sizing:border-box;
  height:100%;
}

.img {
  height: 100%;
  display:block;
}

.another-flex-child {
  flex: none;
  background-color: red;
}

.spacer {
  height: 20px;
}
<div class="container">
  <div class="image-container">
    <img class="img" src="https://interactive-examples.mdn.mozilla.net/media/examples/firefox-logo.svg" >
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

<div class="spacer"></div>

<div class="container">
  <div class="image-container">
    <div>Properly centered content</div>
  </div>
  <div class="another-flex-child">
    Random content here
  </div>
</div>

【讨论】:

  • 我认为这个问题可能与 svg 尺寸有关,但我发现 png 也有同样的问题...
  • @Tobi 不,您想对 PNG 或具有固有宽度/高度的图像有问题。示例:jsfiddle.net/f58c2zj1
  • 嗯,你是对的......只是想知道我在试图让它发挥作用时在我的狂躁实验中实际尝试了什么;)
  • 但看来你的答案是一个非常可行的解决方案,虽然我还没有完全明白
  • @Tobi 有时计算有点棘手,难以弄清楚......我再次更新以在最后添加另一个解决方案,仍然难以解释,但也会解决您的问题
【解决方案3】:

添加 justify-content 如下:

.image-container {
    flex: 1;
    align-self: center;
    justify-content:center;
}

应该可以的

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    相关资源
    最近更新 更多