title: Vertically centered nested flex box
body: <p>I am trying to achieve a simple layout with three vertical sections:</p>
  
  <ol>
  <li>Small header on top of the page.</li>
  <li>Main content, expanded if necessary so that the footer remains at the bottom of the page.</li>
  <li>Footer at the bottom of the page.</li>
  </ol>
  
  <p>The main content section should be further divided in two sections: a very small form with an input box and a button and a 5-row-max table showing some tabular data. The table should take up as much vertical space as needed, an the small form should be centered in the remaining vertical space.</p>
  
  <p>I've tried using flex boxes but there is something I am doing wrong; I cannot manage to vertically center the tiny form in the available space. This is a simplified version of what I've done so far (best viewed in full screen):</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>body {
    margin:0;
    min-height:100vh;
    display:flex;
    flex-direction:column;
    align-items:center;
  }
  
  main {
    flex:1;
    display:flex;
    flex-direction:column;
  }
  
  main&gt;form {
    flex:1;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;body&gt;
    &lt;header&gt;header&lt;/header&gt;
    &lt;main&gt;
      &lt;form&gt;
        &lt;h1&gt;Form with inputs&lt;/h1&gt;
        &lt;label&gt;Inputs&lt;/label&gt;
        &lt;input type="text"&gt;
        &lt;button&gt;Button&lt;/button&gt;
      &lt;/form&gt;
      &lt;div&gt;
        &lt;h1&gt;Other content&lt;/h1&gt;
        &lt;ul&gt;
          &lt;li&gt;Item 1&lt;/li&gt;
          &lt;li&gt;Item 2&lt;/li&gt;
          &lt;li&gt;Item 3&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;/main&gt;
    &lt;footer&gt;footer&lt;/footer&gt;
  &lt;/body&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I have tried multiple combinations of <code>justify-content</code>, <code>align-content</code>, <code>align-items</code> and <code>align-self</code> but the more I try, the less I seem to understand what I am doing :-(</p>
  
  <p>Can anyone who does, provide me with any hints as to what I am doing wrong?</p>
  
  <p>EDIT: If I could use fixed heights for some elements, I might do something like this (which also illustrates what I am after, better viewed in full screen):</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-css lang-css prettyprint-override"><code>* {
    box-sizing:border-box;
  }
        
  html {
    height:100%;
  }
    
  body {
    margin:0;
    height:100%;
    position:relative;
  }
  
  header {
    height:40px;
    background-color:#E6ECC1;
  }
  
  main {
    height:calc(100% - 80px);
    background-color:#dfd;
  }
  
  h1 {
    margin:0;
    padding:10px 0;
  }
  
  main&gt;form {
    height:calc(100% - 150px);
    background-color:#C1E4EC;
  }
  
  main&gt;form:before {
    content:"";
    display:inline-block;
    height:100%;
    vertical-align:middle;
  }
  
  main&gt;form&gt;div {
    display: inline-block;
    vertical-align: middle;
    width: calc(100% - 4px);
    text-align: center;
  }
  
  main&gt;div {
    padding:0 20px;
    height:150px;
    position:absolute;
    bottom:40px;
  }
  
  footer {
    position:absolute;
    height:40px;
    width:100%;
    bottom:0;
    background-color:#fdd;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;body&gt;
    &lt;header&gt;header&lt;/header&gt;
    &lt;main&gt;
      &lt;form&gt;
        &lt;div&gt;
          &lt;h1&gt;Form with inputs&lt;/h1&gt;
          &lt;label&gt;Inputs&lt;/label&gt;
          &lt;input type="text"&gt;
          &lt;button&gt;Button&lt;/button&gt;
        &lt;/div&gt;
      &lt;/form&gt;
      &lt;div&gt;
        &lt;h1&gt;Other content&lt;/h1&gt;
        &lt;ul&gt;
          &lt;li&gt;Item 1&lt;/li&gt;
          &lt;li&gt;Item 2&lt;/li&gt;
          &lt;li&gt;Item 3&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/div&gt;
    &lt;/main&gt;
    &lt;footer&gt;footer&lt;/footer&gt;
  &lt;/body&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I thought I would be able to implement this more cleanly using two nested flex boxes; an outer one providing room for the header at the top, the footer at the bottom and a mid section that can grow to fill all available space in between, and an inner one dividing the outer mid section into two; a bottom one with a constant but unknown height and a top one taking up all remaining space in the mid section. The content of this block, much smaller than the available area when the page is viewed in a large display, should be centered both horizontally and vertically in the available area.</p>
  
  <p>When I only use an outer flex box (the <code>&lt;body&gt;</code> element in my original snippet) the content is centered vertically between the header and the footer. But when I try to split the mid section (the <code>&lt;main&gt;</code> element in my original snippet), the form is pushed upwards. It is as if the height of the <code>flex:1</code> element in the outer box were not available to its children elements.</p>
  
  <p>Thank you very much, have a good day.</p>