Create a scrolling div between two fixed div elements

后端 未结 2 732
走了就别回头了
走了就别回头了 2021-01-23 09:18

I\'m kind of new to positioning divs how I want them in a website, so I hope someone can help here. What I\'m trying to get is a sandwich-type set up with a scrolling content i

相关标签:
2条回答
  • 2021-01-23 09:50

    If you set your header and footer divs to fixed, you can make a main div for your content which will scroll the way you want it to.

    0 讨论(0)
  • 2021-01-23 09:56

    Simply set the height of the consecutive elements to equal 100% and set your content DIV to scroll on the Y-axis:

    <header>
        <h1>Sandwich Layout</h1>
    </header>
    <div id="main" role="main">
    
    </div>
    <footer>
        Footer stuff here
    </footer>
    
    html,
    body { height: 100%; margin: 0; padding: 0; } /* This is important */
    
    header,
    footer { background: #ccc; height:20%; }
    
    #main { height: 60%; overflow-y:scroll; }
    

    Fiddle: http://jsfiddle.net/kboucher/3E8Gg/

    2020 UPDATE:

    HTML:

    <header>
        <h1>Sandwich Layout</h1>
    </header>
    <div class="main" role="main">
      <div class="fake-height">Content here</div>
    </div>
    <footer>
        Footer stuff here
    </footer>
    

    CSS:

    body,
    html {
      height: 100vh;
      overflow: hidden;
      margin: 0;
      padding: 0;
    }
    
    body {
      display: flex;
      flex-direction: column;
    }
    
    header,
    footer {
      background: #eee;
      padding: 1rem;
    }
    
    .main {
      flex: 1 0 auto;
      height: 0; // prevents flex box expanding out of view-height
      overflow-y: auto;
      padding: 1rem;
    
      .fake-height {
        height: 1000px;
      }
    }
    

    https://codepen.io/kboucher/pen/dyomxWN

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