Flexbox with fixed header and footer and scrollable content

后端 未结 2 1218
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-05 15:52

I am trying to make a flexbox design work. I am not an HTML/css expert. :(

I have a flexbox design with fixed header and footer and content that occupies the rest of

相关标签:
2条回答
  • 2020-12-05 16:41

    You can do something like this:

    html, body {
      margin: 0;
      height: 100%; /* can also use viewport units (height: 100vh) */
    }
    
    #container {
      display: flex; /* displays flex-items (children) inline */
      flex-direction: column; /* stacks them vertically */
      height: 100%; /* needs to take the parents height, alternative: body {display: flex} */
    }
    
    main {
      flex: 1; /* takes the remaining height of the "container" div */
      overflow: auto; /* to scroll just the "main" div */
    }
    
    section {
      height: 100%; /* takes the visible area of the "main" div */
      overflow: auto; /* recommended */
      border-bottom: 1px solid;
      background: lightgreen;
    }
    
    header {background: #f88}
    section:last-child {border: none}
    footer {background: lightblue}
    <div id="container">
      <header>top</header>
      <main>
        <section>1st</section>
        <section>2nd</section>
        <section>3rd<br>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<br>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<br>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<br>
        </section>
        <section>4th</section>
        <section>5th</section>
      </main>
      <footer>bottom</footer>
    </div>

    0 讨论(0)
  • 2020-12-05 16:41

    It sounds like what you want are multiple elements in the content section that basically take up the entire page in size, but that you can scroll through.

    Let's call these elements in the content section "slide"s.

    The easiest way to achieve this would be to set the minimum dimensions of these elements, something like -

    .content .slide {
      min-width:100vw;
      min-height:100vh;
    }
    

    This will make sure, even if the content within the slide's doesn't take up the entire space, it will remain at full size.

    0 讨论(0)
提交回复
热议问题