简体   繁体   English

Flex div继承祖父母的高度而不是父母的高度

[英]Flex div inherits grandparent's height instead of parent's

I'm fooling around with flex in order to create a single page application. 为了创建一个单页应用程序,我在鬼混Flex。 I'm using 2 flex boxes: 我正在使用2个弹性盒:

  • Root Flex box (flex 1) (100% width & height) 根伸缩盒(伸缩1)(100%宽度和高度)
    • Nav Bar (55px height) 导航栏(55像素高)
    • App Content (Flex stretched to the bottom of the page) 应用内容(Flex延伸到页面底部)
    • Flex Container (flex 2) (100% height of PARENT) Flex容器(flex 2)(父母的100%高度)
      • Left Nav (240px width) 左导航(240像素宽)
      • Section content (Flex stretched to the right of the page) 板块内容(Flex延伸到页面右侧)

As far as I can tell, my CSS is correct, however, the result I get is that the Flex Container (flex 2) inherits its grandparent height, instead of it's parent's height (obtained via flex stretching). 据我所知,我的CSS是正确的,但是,我得到的结果是Flex容器(flex 2)继承了其祖父母的高度,而不是其父代的高度(通过flex拉伸获得)。 This is super annoying since my page ends up overflowing and looks janky. 这真是太烦人了,因为我的页面最终溢出并看上去很乱。

JSFiddle to repro: https://jsfiddle.net/mdecdawd/ . 要复制的JSFiddle: https ://jsfiddle.net/mdecdawd/。 Please inspect elements to notice the overflow happening. 请检查元素以注意溢出的发生。

CSS: .app { flex: 1; CSS: .app {flex:1; height: 100%; 高度:100%; order: 2; 顺序:2; } }

.container {
  background-color: #884dff;
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
}

.demo {
  background-color: #ffffff;
  flex: 1;
  height: 100%;
  width: 100%;
}

.flexWrapper {
  display: flex;
  flex-flow: row wrap;
  height: inherit;
  width: 100%;
}

.menu {
  background-color: #ffffff;
  border-right: 1px solid #a6a6a6;
  flex: 0 0 auto;
  height: 100%;
  width: 80px;
}

.navBar {
  box-sizing: border-box;
  color: #FFFFFF;
  flex: none;
  padding: 5px 0 0 5px;
  height: 55px;
}

.root {
  height: 350px;
  position: absolute;
  width: 450px;
}

HTML <div class="root"> <div class="container"> <div class="navBar"> Navbar </div> <div class="app"> <div class="flexWrapper"> <div class="menu"> Menu </div> <div class="demo"> Demo Content </div> </div> </div> </div> </div> HTML <div class="root"> <div class="container"> <div class="navBar"> Navbar </div> <div class="app"> <div class="flexWrapper"> <div class="menu"> Menu </div> <div class="demo"> Demo Content </div> </div> </div> </div> </div>

Here are some screenshots to illustrate the issue: Container CSS, width and height 以下是一些截图来说明问题: 容器CSS,宽度和高度 在此处输入图片说明

Flex box 2 (stretched as expected) 伸缩盒2(按预期拉伸) 在此处输入图片说明

Child of Flex box 2, inheriting grandparent's height :( Flex盒子2的子元素,继承祖父母的身高:( 在此处输入图片说明

I'm pulling my hair out even harder since Firefox renders my page correctly, however Chrome and Safari do not. 由于Firefox可以正确呈现我的页面,因此我将更加努力,但是Chrome和Safari却无法。

Any ideas? 有任何想法吗? So far I've tried: position: relative on [flex 2] min-height on everything. 到目前为止,我已经尝试过:position:相对[flex 2]最小高度的相对值。 No effect 没有效果

You're telling div with class app (a flex item in the main flex container) to have the same height of the parent. 您要通过类app (主flex容器中的flex项)告诉div具有与父对象相同的高度。

From your code: 从您的代码:

.app {
   flex: 1;
   height: 100%;
   order: 2;
}

So this div will naturally overflow the container since there is another flex item ( .navBar ) with a height: 55px . 因此,此div自然会溢出容器,因为还有另一个height: 55px flex项( .navBar )。 Hence, the overflow will be 55px. 因此,溢出将为55px。

Try this adjustment: 尝试此调整:

.app {
    flex: 1;
    height: calc(100% - 55px);
    order: 2;
}

Revised Demo 修改后的演示

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

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