1

Currently I have this layout.

    * {
      padding: 0;
      margin: 0;
      box-sizing: border-box;
    }
    html,
    body {
      height: 100%;
      min-height: 100%;
    }
    #wrapper {
      height: 100%;
      min-height: 100%;
      position: relative;
      background-color: red;
    }
    header {
      height: 100%;
      min-height: 100vh;
      position: relative;
      background-color: green;
      text-align: center;
    }
    #header-top {
      position: fixed;
      left: 0;
      right: 0;
      top: 0;
      width: 100%;
      outline: 1px dotted red;
      background-color: blue;
    }
    #header-middle {
      position: relative;
      top: 50%;
      transform: translateY(-50%);
      background-color: yellow;
      outline: 1px dotted red;
    }
    #header-bottom {
      position: absolute;
      left: 0;
      right: 0;
      bottom: 0;
      width: 100%;
      background-color: grey;
      outline: 1px dotted red;
    }
<div id="wrapper">
  <header>
    <div id="header-top">
      <p>I am fixed at the top</p>
    </div>
    <div id="header-middle">
      <p>I am vertically centered</p>
    </div>
    <div id="header-bottom">
      <p>I am stuck at the bottom but not fixed</p>
    </div>
  </header>
</div>

How do I use flexbox here to get the same layout.

  • The html, body and #wrapper needs to expand visually to surround all the child elements.
  • The header is to fill the entire viewport.
  • The #header-top is fixed at the top containing a logo floated to left and navigation floated to right with no explicit height.
  • The #header-middle is to be vertically centered inside the header.
  • The #header-bottom is like a sticky footer stuck at the bottom but no fixed.

Fiddle

DaBler
  • 2,695
  • 2
  • 26
  • 46
Jawad Asghar Ali
  • 45
  • 1
  • 1
  • 7

1 Answers1

2

Use this:

header {
  display: flex; /* Magic begins */
  flex-direction: column; /* Stack vertically */
  height: 100%; /* As tall as the containing block */
  justify-content: space-between; /* Distribute the flex items */
}

* {
  padding: 0;
  margin: 0;
}
html, body, header {
  height: 100%;
}
header {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  background-color: green;
  text-align: center;
}
header > div {
  outline: 1px dotted red;
}
#header-top {
  background-color: blue;
}
#header-middle {
  background-color: yellow;
}
#header-bottom {
  background-color: grey;
}
<header>
  <div id="header-top">
    <p>I am fixed at the top</p>
  </div>
  <div id="header-middle">
    <p>I am vertically centered</p>
  </div>
  <div id="header-bottom">
    <p>I am stuck at the bottom but not fixed</p>
  </div>
</header>
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • The html, body and #wrapper needs a min-height: 100%, the header needs a min-height: 100vh, the #header-top needs a position: fixed, top: 0, left: 0, right: 0, width: 100%. And if I remove the #header-top, the #header-content does not center vertically and moves top. Can't get it to work with align-self: center and align-self: flex-end. – Jawad Asghar Ali Apr 18 '15 at 10:34
  • @JawadAsgharAli Not sure why you need those, but add them if you want. If you want to center `#header-middle` even if `#header-top` is removed, use `#header-middle{margin: auto 0}`. – Oriol Apr 18 '15 at 10:39
  • hmm. not sure what you getting at. no such thing as margin: auto 0 for vertical center. It's margin: 0 auto for horizontal center. The #header-middle needs to be vertically center even if there is no #header-top. – Jawad Asghar Ali Apr 18 '15 at 10:53
  • @JawadAsgharAli Yes, `margin: 0 auto` centers horizontally and `margin: auto 0` centers vertically. So use the second one. – Oriol Apr 18 '15 at 10:54
  • margin: auto 0; does NOT center vertically! – Jawad Asghar Ali Apr 18 '15 at 11:00
  • http://stackoverflow.com/questions/12415661/using-marginauto-to-vertically-align-div – Jawad Asghar Ali Apr 18 '15 at 11:00
  • http://stackoverflow.com/questions/8620200/why-does-auto-attribute-for-margin-not-work-vertically-while-it-works-horizontal – Jawad Asghar Ali Apr 18 '15 at 11:00
  • 1
    `margin: auto 0` centers vertically in flexbox. – Oriol Apr 18 '15 at 11:39