如何使内容滚动而不是页面

时间:2020-10-30 13:28:12

标签: html css twitter-bootstrap overflow

我想滚动显示内容区域而不是页面。

根据documentation,我应该可以通过应用引导程序overflow-auto类来做到这一点,但是我无法正常工作,例如,此处的黄色div;

* {
  padding: 1em;
}

html,
body {
  margin: 0;
  padding: 0;
  background-color: #0F0;
}

nav {
  background-color: #F00;
}

main {
  background-color: #FFF;
}

.header {
  background-color: #07f;
}

.content {
  background-color: #f7f;
}

.scroll {
  background-color: #FD0;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<body>
  <div class="container p-0">
    <div class="row p-0">
      <nav class="col-md-2">Nav bar</nav>
      <main class="col-md-10">
        <div class="header">
          <b>Header</b>
        </div>

        <div class="content">
          <b>Title</b>
          <div class="scroll overflow-auto">
            <div class="E">1</div>
            <div class="E">2</div>
            <div class="E">3</div>
            <div class="E">4</div>
            <div class="E">5</div>
            <div class="E">6</div>
            <div class="E">7</div>
            <div class="E">8</div>
            <div class="E">9</div>
            <div class="E">10</div>
            <div class="E">11</div>
            <div class="E">12</div>
            <div class="E">10</div>
            <div class="E">11</div>
            <div class="E">12</div>
            <div class="E">10</div>
            <div class="E">11</div>
            <!--<div class="E">12</div>-->

          </div>
        </div>
      </main>
    </div>
  </div>

</body>

1 个答案:

答案 0 :(得分:2)

要添加滚动,必须定义内容的高度(如注释中所述)。您可以使用overflow定义overflow-y y轴,并使用overflow定义overflow-x x轴。或者,您可以使用overflow:auto将两个轴都设置为溢出。

工作代码:

* {
  padding: 1em;
}

html,
body {
  margin: 0;
  padding: 0;
  background-color: #0F0;
}

nav {
  background-color: #F00;
}

main {
  background-color: #FFF;
}

.header {
  background-color: #07f;
}

.content {
  background-color: #f7f;
}

.scroll {
  background-color: #FD0;
  height: 200px;
  overflow-y: auto;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet"/>
<body>
  <div class="container p-0">
    <div class="row p-0">
      <nav class="col-md-2">Nav bar</nav>
      <main class="col-md-10">
        <div class="header">
          <b>Header</b>
        </div>

        <div class="content">
          <b>Title</b>
          <div class="scroll overflow-auto">
            <div class="E">1</div>
            <div class="E">2</div>
            <div class="E">3</div>
            <div class="E">4</div>
            <div class="E">5</div>
            <div class="E">6</div>
            <div class="E">7</div>
            <div class="E">8</div>
            <div class="E">9</div>
            <div class="E">10</div>
            <div class="E">11</div>
            <div class="E">12</div>
            <div class="E">10</div>
            <div class="E">11</div>
            <div class="E">12</div>
            <div class="E">10</div>
            <div class="E">11</div>
            <!--<div class="E">12</div>-->

          </div>
        </div>
      </main>
    </div>
  </div>

</body>