【问题标题】:Why can't I adjust the height of flex items using "align-items: stretch" when the flex container is a scroll box?当 flex 容器是滚动框时,为什么我不能使用“align-items:stretch”调整 flex 项的高度?
【发布时间】:2019-12-06 04:42:08
【问题描述】:

我在这样的滚动框中有两个元素。但是,这里的左元素与右元素不匹配。左边的元素忽略align-items,匹配滚动框的高度。

这是为什么呢,能不能把左边元素的高度和右边的元素相匹配?

我想关于这个问题有一个重复的问题,但现在我还没有找到它。

.box {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: stretch; /* not worked. why? */
  overflow: hidden scroll;
  background: green;
}

.item1 {
  background: red; /* doesnt stretch */
}

.item2 {
  width: 200px;
  height: 500px;
  background: red;
  margin: auto;
}
<div class="box">
  <div class="item1">
    <span>sample text</span>
  </div>
  <div class="item2"></div>
</div>

【问题讨论】:

    标签: css flexbox overflow


    【解决方案1】:

    对齐项目:居中;在盒子类中

    并根据需要在 item1 和 item2 中给出高度

    item2 没有作为 item1 的全高,因为 margin:auto in item1

    【讨论】:

    • 它使用中心而不是拉伸,所以你不能拉伸两个元素之间的高度,对吧?
    • align-item:stretch 所做的是使 flex-child 高度相等。但是当您在 item2 中使用自动边距时,它只需要高度和宽度或高度和宽度的值,并将其垂直和水平居中
    • but when you used margin auto ...我删除了margin,但仍然无法拉伸。 jsfiddle.net/8kuf5o74
    • 删除高度:500px 也
    • 当然它匹配高度,但我不能改变这个高度。此外,在普通的 flexbox 中,即使为 flex 项目指定了高度,拉伸也会起作​​用。在这种情况下我为什么要删除高度?
    猜你喜欢
    • 1970-01-01
    • 2021-06-30
    • 1970-01-01
    • 2019-08-29
    • 2018-03-10
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多