如何让只有“内容”区域出现滚动条?

前端开发 2026-07-07

我已经尝试了到处看到的所有方法,但仍没能让它起作用。

我只想让内容区域有滚动条。

我尝试过使用Flexbox,也尝试过calc(),但都没有按预期工作。

body {
  align-items: center;
  height: 100vh;
  overflow: hidden;
}

html,
body {
  margin: 0px;
  padding: 0px;

}

table {
  width: 100%;
  height: 100%;
  background-color: #c0c0c0;
}

tr {
  size: 100%;
}

td {
  padding: 2px;
}

h {
  font-size: 64px;
}

.header {
  font-size: 24px;
  height: 32px;
  box-shadow: none;
  background-color: #0000ff;
  color: #ffffff;
}

.footer {
  height: 32px;
  box-shadow: none;
}

.content {
  height: -moz-calc(100% - (64px));
  height: -moz-calc(100% - (64px));
  text-align: center;
  background-color: #008080;
  overflow-y: scroll;
  scrollbar-gutter: stable;
  color: #fff;
}

.clock {
  text-align: center;
  box-shadow: solid -1px -1px #0a0a0a, inset 1px 1px #0a0a0a, inset -2px -2px #0a0a0a, inset 2px 2px #0a0a0a;
}

button {
  background-color: #c0c0c0;
  width: 32px;
  height: 24px;
}

button:active {
  outline: 1px dotted #000;
  outline-offset: -4px
}

button:hover {
  cursor: help;
}
<table>
  <tr class="header">
    <td colspan="3">
      <marquee>!! work in progress !!</marquee>
    </td>
  </tr>
  <tr class="content">
    <td colspan="3">
      <h>work in progress</h><br>
      <p>overflow</p><br>
      <p>overflow</p><br>
      <p>overflow</p><br>
      <p>overflow</p><br>
      <p>overflow</p><br>
      <p>overflow</p><br>
      <p>overflow</p><br>
      <p>overflow</p><br>
      <p>overflow</p><br>
      <p>overflow</p><br>
      <p>overflow</p><br>
      <p>overflow</p><br>
      <p>overflow</p><br>
      <p>overflow</p>
    </td>
  </tr>
  <tr class="footer">
    <td style="width:46.5%; text-align: left;"><button>c</button></td>
    <td class="clock">d</td>
    <td style="width:46.5%; text-align: right;">e</td>
  </tr>
</table>

解决方案

只需把内容包裹在一个能够可靠滚动的容器中。

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
}

table {
  width: 100%;
  height: 100%;
  border-collapse: collapse;
  background: #c0c0c0;
}

td {
  padding: 2px;
}

.header {
  height: 32px;
  font-size: 24px;
  background: #0000ff;
  color: #ffffff;
}

.footer {
  height: 32px;
}

.content-row {
  height: calc(100% - 64px);
}

.content-row>td {
  height: 100%;
  padding: 0;
}

.content {
  height: 100%;
  overflow-y: auto;
  scrollbar-gutter: stable;
  text-align: center;
  background: #008080;
  color: #fff;
  padding: 2px;
  box-sizing: border-box;
}

h1 {
  font-size: 64px;
  margin: 0.3em 0;
  font-weight: normal;
}

p {
  margin: 1em 0;
}

.left {
  width: 46.5%;
  text-align: left;
}

.right {
  width: 46.5%;
  text-align: right;
}

.clock {
  text-align: center;
}

button {
  width: 32px;
  height: 24px;
  background: #c0c0c0;
}

button:active {
  outline: 1px dotted #000;
  outline-offset: -4px;
}

button:hover {
  cursor: help;
}
<table>
  <tr class="header">
    <td colspan="3">
      <marquee>!! work in progress !!</marquee>
    </td>
  </tr>

  <tr class="content-row">
    <td colspan="3">
      <div class="content">
        <h1>work in progress</h1>

        <p>overflow</p>
        <p>overflow</p>
        <p>overflow</p>
        <p>overflow</p>
        <p>overflow</p>
        <p>overflow</p>
        <p>overflow</p>
        <p>overflow</p>
        <p>overflow</p>
        <p>overflow</p>
        <p>overflow</p>
        <p>overflow</p>
        <p>overflow</p>
        <p>overflow</p>
      </div>
    </td>
  </tr>

  <tr class="footer">
    <td class="left"><button>c</button></td>
    <td class="clock">d</td>
    <td class="right">e</td>
  </tr>
</table>
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章