skip to Main Content

I am curious if anyone can get 100% of these requirements working together, I can only get 80% (#4 is a pain):

  1. The header always stays at the top of the page.
  2. The footer always stays at the bottom of the page.
  3. The main takes up the remaining middle of the page, regardless of whether it overflows or not.
  4. When main overflows, there is a scrollbar that takes up the entire height of the browser (i.e. next to the header, main, and footer). Optional bonus: when main is small, there is no scrollbar.
  5. Only main experiences overscroll. Header and footer are do not experience overscroll.

The best I have done so far does not have the full scrollbar height:

<div className="flex flex-col h-full overflow-hidden">
  <header />
  <main className="flex-1 overflow-auto" /> 
  <footer />
</div>

I am using Tailwind, but feel free to use plain CSS. Hope it doesn’t crush your soul like it has mine.

3

Answers


  1. You can try this

    html code

    <div class="container">
      <header>Header</header>
      <main>
        <div style="width: 300px;">Main Content</div>
      </main>
      <footer>Footer</footer>
    </div>
    

    and css

    <style>
      html, body, #root {
        height: 100%;
        margin: 0;
      }
      
      .container {
        position: relative;
        height: 100%;
        display: flex;
        flex-direction: column;
      }
    
      header {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        height: 60px;
        background-color: #333;
        color: white;
        text-align: center;
        line-height: 60px;
      }
    
      main {
        flex: 1;
        overflow-y: auto;
        padding-top: 60px;
        padding-bottom: 60px;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    
      footer {
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        height: 60px;
        background-color: #333;
        color: white;
        text-align: center;
        line-height: 60px;
      }
    </style>
    
    Login or Signup to reply.
  2. You said base css is ok, so here i give you another approach, with some minor change as not use properties like your css class or change position: absolute to position: fixed.

    * {margin: 0; padding: 0; box-sizing: border-box;}
    div {position: relative; min-height: 100vh;}
    header {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        background: lightblue;
        padding: 15px;
    }
    footer {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        background: lightgray;
        padding: 15px;
    }
    main {
        background: lightgreen;
        min-height: 100vh;
        padding: 63px 15px;
    }
    <div>
        <header>Header</header>
        <main>
            <h3>Main</h3>
            <p>What is Lorem Ipsum?</p>
            <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
            <p>Why do we use it?</p>
            <p>It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).</p>
            <p>Where does it come from?</p>
            <p>Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.</p>
            <p>The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.</p>
            <p>Where can I get some?</p>
            <p>There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.</p>
        </main>
        <footer>Footer</footer>
    </div>
    Login or Signup to reply.
  3. Does this meet your expectation?

    body {
      margin: 0;
    }
    
    header {
      position: sticky;
      top: 0;
      left: 0;
      width: 100%;
      background: lightblue;
      padding: 15px;
      z-index: 1;
    }
    
    footer {
      position: sticky;
      bottom: 0;
      left: 0;
      width: 100%;
      background: lightgray;
      padding: 15px;
      z-index: 1;
    }
    
    main {
      background: lightgreen;
      min-height: 100vh;
    }
    <div class="container">
      <header>Header</header>
      <main>
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec non neque vel tortor lobortis consequat. Vestibulum condimentum justo purus, ornare malesuada eros sagittis ac. Mauris imperdiet ex quis urna eleifend scelerisque. Suspendisse mi lectus, venenatis
        id fringilla quis, consectetur eget tellus. Nunc malesuada pharetra nibh in porta. In ornare sapien a felis porta feugiat. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Nulla sollicitudin quam metus, vel sagittis
        ex consectetur at. Phasellus ut sapien velit. Suspendisse vitae blandit nisl. Praesent porttitor, nibh vel suscipit pulvinar, diam arcu faucibus tellus, sed tempus odio mi a nibh. Duis mollis, odio ut pulvinar venenatis, est erat eleifend mi, id egestas
        arcu lorem id augue. Integer commodo gravida fermentum. Nullam egestas consequat tellus, vitae dapibus elit lobortis sed. Phasellus vitae justo lorem. In quis ipsum pellentesque, pulvinar turpis ut, ultricies neque. Aliquam nisl leo, laoreet ac malesuada
        non, dignissim sed augue. Phasellus egestas, turpis quis pretium cursus, mi dui lobortis lacus, quis gravida nisl eros nec sapien. Aenean molestie sem ut lorem suscipit posuere. Sed porttitor ex metus, sit amet sodales tellus molestie eleifend. Integer
        aliquam justo sed dolor volutpat, in gravida diam laoreet. Ut at molestie enim. Nam posuere convallis blandit. Cras vestibulum placerat suscipit. Mauris euismod tortor nec justo consectetur tincidunt. Nulla facilisi. Nunc mollis libero nec urna dapibus
        ultrices. Nulla in posuere tortor. Aenean sit amet porttitor arcu, eu efficitur neque. Quisque et mauris eros. Aenean lobortis leo at nunc pellentesque, sed eleifend nisi ornare. Donec in tortor porttitor, fringilla ligula quis, sodales justo. Praesent
        pulvinar efficitur tortor sit amet consequat. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Nulla augue ligula, cursus ac arcu quis, sodales facilisis enim. Nulla venenatis ex nulla, in aliquet massa congue
        id. Aenean volutpat tellus quis mattis rhoncus. Mauris a convallis lectus, eu posuere metus. Phasellus sit amet enim lorem. Maecenas fringilla felis sit amet dolor finibus, eu rhoncus nisi tristique. Aenean in volutpat purus. Nunc posuere felis elit,
        eu viverra neque auctor vitae. Quisque faucibus dapibus quam eget porta. Praesent quis fermentum ante. Aliquam scelerisque lobortis porttitor.
      </main>
      <footer>Footer</footer>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search