tbody 内带有垂直滚动的 html 表格

标签 html css css-tables

我正在尝试在 tbody 内设置一个带有固定标题的垂直滚动条。我尝试了链接中提供的解决方案。

HTML table with 100% width, with vertical scroll inside tbody

table {
width: 100%;
border-spacing: 0;
}

thead, tbody, tr, th, td { display: block; }

thead tr {
/* fallback */
width: 97%;
/* minus scroll bar width */
width: -webkit-calc(100% - 16px);
width:    -moz-calc(100% - 16px);
width:         calc(100% - 16px);
}

tr:after {  /* clearing float */
content: ' ';
display: block;
visibility: hidden;
clear: both;
}

tbody {
height: 100px;
overflow-y: auto;
overflow-x: hidden;
}

tbody td, thead th {
width: 19%;  /* 19% is less than (100% / 5 cols) = 20% */
float: left;
}

如果出现滚动条,效果很好。但是,如果行数很少,并且没有出现滚动条,则说明 thead 与 tbody 不对齐。如何解决 CSS 问题?

enter image description here

最佳答案

一旦您的 body 数据移出指定高度,您的 y 轴就会被激活。

 tbody {
        height: 50px;
        display: inline-block;
        width: 100%;
        overflow-y:scroll;
    }

关于tbody 内带有垂直滚动的 html 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38074727/

相关文章:

jquery - 无法处理 JQuery 形式的 Spring MVC 中的 JSON 数据

javascript - 如何将点击事件从一个 div 传递到另一个?

css - 垂直居中对齐 col-* bootstrap 3 的内容

css - 在按钮内垂直堆叠跨度

html - 如何让流体表与省略号一起使用?

html - 如何在垂直导航栏旁边移动表格(或其他内容)

HTML 表格尺寸在不同系统上受到干扰

node.js - 如何以 HTML5 输入类型日期格式(dd/mm/yyyy)显示 Mongoose 日期,

html - CSS - 防止 div 内容调整父元素的大小

javascript - jQuery 切换 - 图标切换点击?