Flexbox在另一个flexbox里面?

Shi*_*xma 5 html css css3 flexbox

我正在尝试让flexbox在flexbox中工作.虽然第一个(包装)flexbox工作,但内部的一个没有做任何事情.反正有没有让这个工作?

我要做的是有效地拥有两个粘性页脚,并且两者的高度都达到了页脚.

html, body {
  height: 100%;
  margin: 0; padding: 0;  /* to avoid scrollbars */
}

#wrapper {
  display: flex;  /* use the flex model */
  min-height: 100%;
  flex-direction: column;  /* learn more: http://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/ */
}

#header {
  background: yellow;
  height: 100px;  /* can be variable as well */
  
}

#body {
  flex: 1;
  border: 1px solid orange;
  height: 100%:
}
#wrapper2 {
  display: flex;  /* use the flex model */
  min-height: 100%;
  flex-direction: column;
}
#body2 {
  border: 1px solid purple;
  flex: 1;
}
#footer2 {
  background: red;
  flex: 0;
}

#footer{
  background: lime;
}
Run Code Online (Sandbox Code Playgroud)
<div id="wrapper">
  <div id="body">Bodyof<br/>
    variable<br/>
    height<br/>
    <div id="wrapper2">
    <div id="body2">
    blah
    </div>
    <div id="footer2">
    blah<br />
    blah
    </div>    
    </div>
    </div>
  <div id="footer">
    Footer<br/>
    of<br/>
    variable<br/>
    height<br/>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

JS小提琴

Mic*_*l_B 5

你快到了。只需两步:

  1. 制作#body一个弹性容器。
  2. 用 给出.wrapper2全高flex: 1

(我也摆脱了百分比高度。你不需要它们。)

body {
  margin: 0;
}
#wrapper {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}
#header {
  background: yellow;
  height: 100px;
}
#body {
  flex: 1;
  border: 1px solid orange;
  display: flex;            /* new */
  flex-direction: column;   /* new */
}
#wrapper2 {
  display: flex;
  flex-direction: column;
  flex: 1;                  /* new */
}
#body2 {
  border: 1px solid purple;
  flex: 1;
}
#footer2 {
  background: red;
}
#footer {
  background: lime;
}
Run Code Online (Sandbox Code Playgroud)
<div id="wrapper">
  <div id="body">
    Bodyof
    <br>variable
    <br>height
    <br>
    <div id="wrapper2">
      <div id="body2">blah</div>
      <div id="footer2">
        blah
        <br>blah
      </div>
    </div>
  </div>
  <div id="footer">
    Footer
    <br>of
    <br>variable
    <br>height
    <br>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

js小提琴

完成上述调整后,您可以使用以下命令将内部(红色)页脚固定到底部:

  • flex: 1on #body2,这就是你所拥有的,或者
  • margin-bottom: auto#body2,或
  • margin-top: auto#footer2,或
  • justify-content: space-between在容器 ( #wrapper2)