我正在尝试让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;
}
<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>


JS Fiddle

最佳答案

你快到了只有两步之遥:

  • 使#body为flex容器。
  • .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;
    }
    <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>


    jsFiddle

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

    您拥有的flex: 1上的
  • #body2
  • margin-bottom: auto或上的
  • #body2margin-top: auto
  • 上的
  • #footer2 容器上的
  • justify-content: space-between(#wrapper2)
  • 10-07 19:05
    查看更多