html - 让滚动在子元素上而不是在可调整大小的父元素上

标签 html css

我有一张大 table ,它位于两个粘性 div(页眉和页脚)之间的可调整大小的 float div 中。

但是,当这个表溢出时,滚动就会位于父 float div 上,而不是 div 本身,这会在我水平滚动时弄乱粘性 div。

示例:

enter image description here

当然,这可以通过将表格设置为固定宽度/高度来实现,但我需要它是自动的,因为它位于一个可调整大小的 div 中。

我怎样才能使这项工作?基本上我希望表格 div 有滚动条,而不是它的父元素,就像这个很棒的绘画编辑:

enter image description here

当然会保持自动调整大小。

页眉/页脚不应水平滚动。

这是 fiddle :https://jsfiddle.net/ppgab/nhy02w3v/18/

HTML :

<div class="floating-middle">
  <div class="sticky sticky-top">
    I'm a sticky header
  </div>
  <div class="table-wrapper">
    <table>
    <thead>
      <th>Column1</th>
      <th>Column2</th>
      <th>Column3</th>
      <th>Column4</th>
      <th>Column5</th>
    </thead>
    <tbody>
     ...
    </tbody>
    </table>
  </div>
  <div class="sticky sticky-bottom">
    I'm a sticky footer
  </div>
</div>

CSS:

body{
  display: flex;
  width : 100vw;
  height: 100vh;
  justify-content: center;
  text-align: center;
  flex-direction: column;
}

.floating-middle{
  align-self : center;
  width : 200px;
  height : 200px;
  border: 1px solid black;
  resize: both;
  overflow: auto;
}
.sticky-top{
  position: sticky;
  top: 0;
  border-bottom: 1px solid black;
}
.sticky-bottom{
  position: sticky;
  bottom: 0;
  border-top: 1px solid black;
}
.sticky{
  background: red;
  color: white;
}
table{
  width: 100%;
  overflow: auto;
}

最佳答案

.table-wrapper { 溢出-x:滚动 }

https://jsfiddle.net/fpqamh45/

也许这就是你想要的?

关于html - 让滚动在子元素上而不是在可调整大小的父元素上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54556722/

相关文章:

javascript - postcss 插件是否应该将 postcss 引用为对等依赖项?

html - 我的 Sidenav 阻止页面中的内容

html - Youtube Iframe 嵌入代码不适用于 IE/Edge

javascript - 给 addClass/removeClass 持续时间

JavaScript-创建一个函数来将字符串的第一个字母大写

HTML:固定表格列宽

html - HTML/CSS 中的平铺布局

jquery - 滚动到具有水平溢出的布局中的元素(使用 onclick 或 anchor )

javascript - 显示悬停效果但忽略点击事件?

python - BeautifulSoup 在每个开始和结束标签之前和之后添加额外的空白