Explanation for setting “flex:1” or “flex-grow:1” on all ancestor nodes in nested flex layouts given that flex properties are not hierarchical?

人走茶凉 提交于 2019-12-02 10:43:48

...for setting “flex:1” or “flex-grow:1” on all ancestor nodes in nested flex layouts...

To say “on all ancestor nodes” is inaccurate, it should be on all descendant nodes, which, in terms of parent/child, mean, flex-grow is set on the child, not parent, but since an element can be both a parent and a child, general callled nested flex elements, a parent can have flex-grow if it also is a child.

The flex formatting context has a well explained answer here:


...what you're actually doing when setting the property on the ancestor nodes to get your layout to work.

Again, to say “when setting the property on the ancestor nodes is inaccurate, you set the property on the descendant nodes


Explanation for setting “flex:1” or “flex-grow:1” ... in nested flex layouts

First, setting flex: 1 is equal to flex: 1 1 0, and flex-grow: 1 is equal to flex: 1 1 auto, and this is based on that flex defaults to flex: 0 1 auto, well explained here:


Now, flex-grow defaults to 0, which means it won't be bigger than its content, similar to how an inline-block element work, and will as such render like this:

body { margin: 0; }

.flex-container {
  display: flex;
  height: 100vh; 
}

.flex-parent {
  display: flex;
}
.row-direction {
  flex-direction: row;
}
.column-direction {
  flex-direction: column;
}

.flex-child {
  margin: 2px;
  border: 1px dashed gray;
}
<div class="flex-container column-direction">

  <div class="flex-child flex-parent row-direction">

    <div class="flex-child">
      First row, first item
    </div>
    <div class="flex-child">
      First row, second item
    </div>
    <div class="flex-child">
      First row, third item
    </div>
    
  </div>

  <div class="flex-child flex-parent row-direction">

    <div class="flex-child">
      Second row, first item
    </div>
    <div class="flex-child">
      Second row, second item
    </div>
    
  </div>

  <div class="flex-child flex-parent row-direction">

    <div class="flex-child">
      Third row, first item
    </div>
    
  </div>

</div>

So what flex-grow: 1 does, is to make the element grow and fill the available space within its parent, like this, and as you can see, here alternate between row and column as React often does, it works perfect:

body { margin: 0; }

.flex-container {
  display: flex;
  height: 100vh; 
}

.flex-parent {
  display: flex;
}
.row-direction {
  flex-direction: row;
}
.column-direction {
  flex-direction: column;
}

.flex-child {
  flex-grow: 1;
  margin: 2px;
  border: 1px dashed gray;
}
<div class="flex-container column-direction">

  <div class="flex-child flex-parent row-direction">

    <div class="flex-child">
      First row, first item
    </div>
    <div class="flex-child">
      First row, second item
    </div>
    <div class="flex-child">
      First row, third item
    </div>
    
  </div>

  <div class="flex-child flex-parent row-direction">

    <div class="flex-child">
      Second row, first item
    </div>
    <div class="flex-child">
      Second row, second item
    </div>
    
  </div>

  <div class="flex-child flex-parent row-direction">

    <div class="flex-child">
      Third row, first item
    </div>
    
  </div>

</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!