Create a scrolling div between two fixed div elements

后端 未结 2 768
走了就别回头了
走了就别回头了 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:56

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

    Sandwich Layout

    Footer stuff here
    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:

    Sandwich Layout

    Content here
    Footer stuff here

    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

提交回复
热议问题