Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
255 views
in Technique[技术] by (71.8m points)

css - How to make a container that fill the remaining screen height and is scrollable?

I'm trying to make a fixed div that it's a container and i want to make only this container be scrollable, the rest of the page I want to be fixed. Here is what I have: Stackblitz. Here's a component called "PageDefault" that has the Header and involves each page in application(the content of the page is the prop "children" inside PageDefault's component).

Here is what I would like to do: enter image description here

Minimal Example of what I have(without react):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .header {
        top: 0px;
        background-color: green;
        height: 100px;
      }

      .page-title {
        height: 300px;
        background-color: blue;
      }

      .content {
        position: fixed;
        height: 100%;
        overflow-y: scroll;
      }
    </style>
  </head>
  <body>
    <div class="header">header goes here</div>
    <div class="page-title">page title goes here</div>

    <div class="content">
      blablabla blablabla blabla bla blabla blablabla bla blabla bla blablabla
      blabla bla blabla blablabla blabla bla blabla bla bla blablabla blablabla
      blabla bla blabla bla blabla bla blabla bla blablabla blablabla blabla bla
      blabla blablabla bla blabla bla blablabla blabla bla blabla blablabla
      blabla bla blabla bla bla blablabla blablabla blabla bla blabla bla blabla
      bla blabla bla blablabla blablabla blabla bla blabla blablabla bla blabla
    </div>
  </body>
</html>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Flexbox can do that:

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

::before,
::after {
  box-sizing: inherit;
}

.wrap {
  width: 90%;
  margin: auto;
  height: 100vh;
  max-height: 100vh;
  display: flex;
  flex-direction: column;
}

header {
  background: orange;
  padding: 1em;
}

h1 {
  background: lightblue;
  margin: 0;
}

main {
  flex: 1;
  background: pink;
  overflow: auto;
}

p {
  height: 1000px;
}
<div class="wrap">
  <header>HEADER</header>
  <h1>TITLE</h1>
  <main>
    <p></p>
  </main>
</div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...