加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

html – 在flexbox中使用’height:100%’和’align-items:str

发布时间:2020-12-14 18:34:19 所属栏目:资源 来源:网络整理
导读:参见英文答案 Chrome / Safari not filling 100% height of flex parent4个 我有一个带有直接子项的flexbox,它使用align-items:stretch声明. 在这个flexbox的直接子项中,我想有一个div容器也使用其父级的全高(通过设置高度:100%). 但是,div容器不会拉伸到
参见英文答案 > Chrome / Safari not filling 100% height of flex parent4个
我有一个带有直接子项的flexbox,它使用align-items:stretch声明.

在这个flexbox的直接子项中,我想有一个div容器也使用其父级的全高(通过设置高度:100%).

但是,div容器不会拉伸到其父级的100%高度,除非我还在flexbox的直接子级上设置了高度:100%.

它有点像虫子吗?我必须使用align-items设置flexbox的直接子项:stretch和height:100%以实现我想要的效果吗?这对我来说似乎是多余的.

这是一个例子:

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

.flexbox {
  display: flex;
  flex-direction: row;
  height: 100%;
  background-color: green;
}

.flexbox-child {
  // height: 100%; uncommenting this will get it to work
  align-items: stretch;
  background-color: blue;
}

.flexbox-grand-child {
  height: 100%;
  background-color: red;
}
<div class="flexbox">
  <div class="flexbox-child">
    <div class="flexbox-grand-child">
      I want to be stretched till the bottom
    </div>
  </div>
</div>

http://plnkr.co/edit/FACkwsC2y65NcbOaceur?p=preview

谢谢!

解决方法

这是一个复杂的案例.

您的.flexbox-child只是一个flex项,但不是flex容器.因此,忽略仅适用于flex容器的align-items:stretch.

然后,.flexbox-grand-child的百分比为height,其行为如下:

The percentage is calculated with respect to the height of the
generated box’s containing block. If the height of the containing
block is not specified explicitly (i.e.,it depends on content
height),and this element is not absolutely positioned,the value
computes to ‘auto’.

包含块是flex项(.flexbox-child),它没有明确的高度,其高度似乎取决于内容.

但是,这种依赖性仅仅是由于新的min-height:auto.但在考虑最小高度之前,弹性项目的高度将是(由于初始的align-self:stretch)容器的高度(明确指定),忽略内容.

然后,Firefox认为.flexbox-child的高度不依赖于其内容,因此其子项中的高度百分比应该起作用.然后你的代码工作.

但是,Chrome并不这么认为.

我不确定哪一个做得对.仅在旧的CSS2.1和CSS基本框模型中定义高度没有帮助,这是一个不一致的草稿.

为安全起见,请更好地明确设置.flexbox-child的高度.或者将它变成一个弹性容器.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读