本文翻译自:How to make flexbox children 100% height of their parent?

I'm trying to fill the vertical space of a flex item inside a flexbox. 我正在尝试在flexbox内填充flex项目的垂直空间。

 .container { height: 200px; width: 500px; display: flex; flex-direction: row; } .flex-1 { width: 100px; background-color: blue; } .flex-2 { position: relative; flex: 1; background-color: red; } .flex-2-child { height: 100%; width: 100%; background-color: green; } 
 <div class="container"> <div class="flex-1"></div> <div class="flex-2"> <div class="flex-2-child"></div> </div> </div> 

And here's the JSFiddle 这是JSFiddle

flex-2-child doesn't fill the required height except in the two cases where: 除了以下两种情况外, flex-2-child不会填充所需的高度:

  1. flex-2 has a height of 100% (which is weird because a flex item has a 100% by default + it is buggy in Chrome) flex-2的高度为100%(这很奇怪,因为flex项默认情况下为100%+在Chrome中有问题)
  2. flex-2-child has a position absolute which is also inconvenient flex-2-child具有绝对位置,这也不方便

This doesn't work in Chrome or Firefox currently. 目前,该功能不适用于Chrome或Firefox。


#1楼

参考:https://stackoom.com/question/12XLQ/如何使flexbox子代的父母高度为


#2楼

An idea would be that display:flex; 一个想法是display:flex; with flex-direction: row; 带有flex-direction: row; is filling the container div with .flex-1 and .flex-2 , but that does not mean that .flex-2 has a default height:100%; .flex-1.flex-2填充container div,但这并不意味着.flex-2的默认height:100%; even if it is extended to full height and to have a child element ( .flex-2-child ) with height:100%; 即使扩展到全高并具有height:100%;的子元素( .flex-2-childheight:100%; you'll need to set the parent to height:100%; 您需要将父级设置为height:100%; or use display:flex; 或使用display:flex; with flex-direction: row; 带有flex-direction: row; on the .flex-2 div too. .flex-2 div上也是如此。

From what I know display:flex will not extend all your child elements height to 100%. 根据我所知道的display:flex不会将所有子元素的高度扩展到100%。

A small demo, removed the height from .flex-2-child and used display:flex; 一个小演示,删除了.flex-2-child的高度,并使用display:flex; on .flex-2 : http://jsfiddle.net/2ZDuE/3/ .flex-2 : http : //jsfiddle.net/2ZDuE/3/


#3楼

I have answered a similar question here . 我在这里回答了类似的问题。

I know you have already said position: absolute; 我知道你已经说过position: absolute; is inconvenient but it works. 不方便,但可以。 See below for further information on fixing the resize issue. 有关解决调整大小问题的更多信息,请参见下文。

Also see this jsFiddle for a demo, although I have only added webkit prefixes so open in Chrome. 另请参阅此jsFiddle进行演示,尽管我仅添加了在Chrome中打开的webkit前缀。

You basically have 2 issues which I will deal with separately. 您基本上有2个问题,我将分别处理。

  1. Getting the child of a flex-item to fill height 100% 让弹性项目的子项填充高度100%

    • Set position: relative; 设定position: relative; on the parent of the child. 在孩子的父母身上。
    • Set position: absolute; 设定position: absolute; on the child. 在孩子身上。
    • You can then set width/height as required (100% in my sample). 然后,您可以根据需要设置宽度/高度(在我的示例中为100%)。
  2. Fixing the resize scrolling "quirk" in Chrome 修复Chrome中调整大小的滚动“怪癖”
    • Put overflow-y: auto; overflow-y: auto; on the scrollable div. 在可滚动的div上
    • The scrollable div must have an explicit height specified. 可滚动div必须具有指定的显式高度。 My sample already has height 100% but if none is already applied you can specify height: 0; 我的样本已经具有高度100%,但是如果没有应用height: 0;可以指定height: 0;

See this answer for more information on the scrolling issue. 有关滚动问题的更多信息,请参见此答案 。


#4楼

If I understand correctly, you want flex-2-child to fill the height and width of its parent, so that the red area is fully covered by the green? 如果我理解正确,您是否希望flex-2-child填充其父级的高度和宽度,以便红色区域完全被绿色覆盖?

If so, you just need to set flex-2 to use flexbox: 如果是这样,则只需将flex-2设置为使用flexbox:

.flex-2 {display: flex;
}

Then tell flex-2-child to become flexible: 然后告诉flex-2-child变得灵活:

.flex-2-child {    flex: 1;
}

See http://jsfiddle.net/2ZDuE/10/ 见http://jsfiddle.net/2ZDuE/10/

The reason is that flex-2-child is not a flexbox item, but its parent is. 原因是flex-2-child不是flexbox项,但它的父项是flexbox。


#5楼

I suppose that Chrome's behavior is more consistent with the CSS spec (though it's less intuitive). 我认为Chrome的行为与CSS规范更加一致(尽管不太直观)。 According to Flexbox spec, the default stretch value of align-self property changes only the used value of the element's "cross size property" ( height , in this case). 根据Flexbox规范, align-self属性的默认stretch值仅更改元素的“ cross size属性”(在这种情况下为height ) 的使用值 。 And, as I understand the CSS2.1 spec , the percentage heights are calculated from the specified value of the parent's height , not its used value. 而且,据我了解CSS2.1规范 ,百分比高度是根据父级height指定值(而不是其使用的值)计算得出的。 The specified value of the parent's height isn't affected by any flex properties and is still auto . 父母height的指定值不受任何flex属性影响,并且仍为auto

Setting explicit height: 100% makes it formally possible to calculate the percentage height of the child, just like setting height: 100% to html makes it possible to calculate the percentage height of body in CSS2.1. 设置显式的height: 100%使得正式计算子项的百分比高度成为可能,就像将height: 100%设置为html使得计算CSS2.1中的body的百分比高度成为可能。


#6楼

Use align-items: stretch 使用align-items: stretch

Similar to David Storey's answer, my work around is 与David Storey的答案类似,我的工作是

.flex-2 {display: flex;  align-items: stretch;
}

Alternatively to align-items , you can use align-self just on the .flex-2-child item you want stretched. 除了align-items ,您还可以在要拉伸的.flex-2-child项上使用align-self

如何使flexbox子代的父母高度为100%?相关推荐

  1. 如何使div的浏览器窗口高度为100%

    我有两列的布局-左div和右div. 右边div有一个灰色background-color,我需要根据用户浏览器窗口的高度垂直扩展它.现在background-color结束于该内容的最后一部分div ...

  2. 如何强制子div为父div的高度的100%而不指定父级的高度?

    我有一个具有以下结构的网站: <div id="header"></div><div id="main"><div i ...

  3. [Web前端] 子元素设置高度为100%, 却没有与父元素对齐高度.

    大概描述一下我遇到的情况. 父元素没有明确指定高度, 但是其中一个子元素的高度是确定的, 并且通过这个高度将父元素的高度撑起来. 另一个子元素的高度是100%, 即, 我想要使它与父元素的高度统一. ...

  4. css3 高度最小100%,100%最小高度CSS布局

    我正在使用以下代码:CSS布局-100%的高度 最小身高 此页面的#container元素的最小高度为100%.这样,如果内容所需的高度大于视口提供的高度,则#content的高度也会强制#conta ...

  5. asp.net 2.0 设置表格高度为100%.

    在asp.net2.0下,默认设置表格的高度为100%时不起作用. 页面代码如下: <%@ Page Language="C#" AutoEventWireup=" ...

  6. uni-app - 设置最外层容器高度为100%

    在小程序中 设置page的页面高度为100%只需要在app.wxss中设置 page {height: 100%; } 即可,但是在uniapp中,想要设置page的页面高度为100%就需要在App. ...

  7. 关于div宽度和高度的100%设定的问题

    设置DIV大小的有两个属性width和height,以前在学习DIV每次给DIV设置100%宽度或高度时都很迷惑,不明白这个100%的宽度(高度)到底有多宽有多高?这个100%是从哪里得到的从哪里继承 ...

  8. 饿了么修改侧边栏的高度为100%

    饿了么修改侧边栏的高度为100% 因为之前都是用公司搭建好的框架进行项目开发,最近刚好得闲,就说自己重新用ele写一个后台模板,引入了菜单组件后,发现高度怎么都不够,设置了100%也没什么用. 然后自 ...

  9. 关于Div的宽度与高度的100%设定

    正像你所知道的那样,设置DIV大小的有两个属性width和height,以前在学习DIV每次给DIV设置100%宽度或高度时都很迷惑,不明确这个100%的宽度(高度)到底有多宽有多高?这个100%是从 ...

最新文章

  1. 洛谷 P2219修筑绿化带 二维单调队列~
  2. 对比 5 种分布式事务方案,还是宠幸了阿里的 Seata(原理 + 实战)
  3. python输出数据到excel-python实现数据导出到excel的示例
  4. ARM体系结构及内核回顾总结(一)
  5. 全新的 Vue3 状态管理工具:Pinia
  6. JS - 按钮倒计时
  7. mysql 存储过程 预处理语句_用于预处理语句的MySQL存储过程游标
  8. php声明js变量类型,js中变量是什么以及有哪些类型
  9. centos 6.5 x64 上安装mariadb10
  10. Qt4_Quit按键
  11. 数据分页模块系列 (二) 完美封装PageModel实现分页模块
  12. WPF XMAL获取元素的父元素,子元素
  13. 麻省理工18年春软件构造课程阅读10“抽象数据类型”
  14. word转pdf免费网站
  15. C++单例模式(懒汉模式)实现
  16. QT图形显示和处理6
  17. 基于socket(TCP)和opencv的实时视频传输
  18. oracle的报告,Oracle数据库(RAC)巡检报告
  19. which在C语言用法,A,B, and C, which ... which指代的是它们三个还是只有C呢?
  20. 第八十三章 Caché 函数大全 $ZDATE 函数

热门文章

  1. openfire 接受消息流程
  2. 算法--------二叉树的前序遍历
  3. Android登录拦截器实现方式(一)
  4. 计算机基础知识自考真题,自考《计算机应用基础》基础试题
  5. swift_018(Swift 的结构体)
  6. 工业机器人演示码垛和卸垛_浅谈饲料自动码垛机的应用及正确操作方法
  7. React router 4 获取路由参数,跨页面参数
  8. 设计模式——设计模式之禅day2
  9. windowsnbsp;下搭建apachenbsp;phpnbsp;mysqlnbsp;p…
  10. 自定义Visual Studio IntelliTrace 智能跟踪