我面临的问题,而设置高度宽度溢出滚动。

<style> 
     tbody{
       height:50px;display:block;overflow:scroll
     }
   </style>

       <h3>Table B</h3>
    <table style="border: 1px solid red;width:300px;display:block">
        <thead>
            <tr>
                <td>Name</td>
                <td>phone</td>
            </tr>
        </thead>
        <tbody style='height:50px;display:block;overflow:scroll'>
            <tr>
                <td>AAAA</td>
                <td>323232</td>
            </tr>
            <tr>
                <td>BBBBB</td>
                <td>323232</td>
            </tr>
            <tr>
                <td>CCCCC</td>
                <td>3435656</td>
            </tr>
        </tbody>
    </table>

在这里参观我的小提琴

我想要表B像表A一样溢出滚动。

任何帮助都将不胜感激。

非常感谢, M。


当前回答

Webkit似乎在内部使用display: table-row-group作为tbody标记。 目前有一个设置高度的错误:https://github.com/w3c/csswg-drafts/issues/476

希望这个问题能很快得到解决。

其他回答

更改第二个表代码,如下所示。

<table style="border: 1px solid red;width:300px;display:block;">
<thead>
    <tr>
        <td width=150>Name</td>
        <td width=150>phone</td>
    </tr>
</thead>
<tbody style='height:50px;overflow:auto;display:block;width:317px;'>
    <tr>
        <td width=150>AAAA</td>
        <td width=150>323232</td>
    </tr>
    <tr>
        <td>BBBBB</td>
        <td>323232</td>
    </tr>
    <tr>
        <td>CCCCC</td>
        <td>3435656</td>
    </tr>
</tbody>
</table>

JSFIDDLE演示

如果你想让body显示滚动条,设置它的display: block;。

设置显示:表格;这样它就能保持表的行为。

为了均匀分布单元格,使用table-layout: fixed;。

演示


CSS:

table, tr td {
    border: 1px solid red
}
tbody {
    display: block;
    height: 50px;
    overflow: auto;
}
thead, tbody tr {
    display: table;
    width: 100%;
    table-layout: fixed;/* even columns width , fix width of table too*/
}
thead {
    width: calc( 100% - 1em )/* scrollbar is average 1em/16px width, remove it from thead width */
}
table {
    width: 400px;
}

如果tbody不显示滚动,因为content小于height或max-height,可以随时使用:overflow-y: scroll;设置滚动。演示2


<编辑S/updateS> 2019 - 04/2021


重要提示:这种使表可滚动的方法在某些情况下有缺陷。(见下面的评论)顺便说一下,这个帖子中的一些重复的答案应该得到同样的警告

警告:此解决方案断开头部和身体细胞网格;这意味着在大多数实际情况下,您将无法从表中获得所期望的单元格对齐。注意这个解决方案使用了一个hack来保持它们的对齐:

Anyhow, to set a scrollbar, a display reset is needed to get rid of the table-layout (which will never show scrollbar). Turning the <table> into a grid via display:grid/contents will also leave a gap in between header and scrollable part, to mind about. (idem if built from divs) overflow:overlay; has not yet shown up in Firefox ( keep watching it) position:sticky will require a parent container which can be the scrolling one. make sure your thead can be sticky if you have a few rows and rowspan/colspan headers in it (it does not with chrome).

到目前为止,仅通过CSS还没有完美的解决方案。有一些平均的方法来选择,所以它适合你自己的表格(表格布局:固定;是. .修复表和列的宽度,但javascript可能会被用来重置这些值=>退出纯CSS)

默认情况下,溢出不适用于表组元素,除非你给了一个display:block到<tbody>,你也必须给一个position:relative和display:block到<thead>。检查DEMO。

.fixed {
  width:350px;
  table-layout: fixed;
  border-collapse: collapse;
}
.fixed th {
  text-decoration: underline;
}
.fixed th,
.fixed td {
  padding: 5px;
  text-align: left;
  min-width: 200px;
}


.fixed thead {
  background-color: red;
  color: #fdfdfd;
}
.fixed thead tr {
  display: block;
  position: relative;
}
.fixed tbody {
  display: block;
  overflow: auto;
  width: 100%;
  height: 100px;
  overflow-y: scroll;
    overflow-x: hidden;
}

最简单的解决方案:

在CSS中添加以下代码:

.tableClassName tbody {
  display: block;
  max-height: 200px;
  overflow-y: scroll;
}

.tableClassName thead, .tableClassName tbody tr {
  display: table;
  width: 100%;
  table-layout: fixed;
}
.tableClassName thead {
  width: calc( 100% - 1.1em );
}

1.1 em是滚动条的平均宽度,如有需要请修改。

我猜你要做的是,保持标题固定,滚动正文内容。 你可以滚动内容到2个方向:

水平:你不能水平滚动内容,除非你使用滑块(例如jQuery滑块)。我建议在这种情况下避免使用表格。 vertical:使用tbody标记将无法实现这一点,因为分配display:block或display:inline-block将破坏表的布局。

下面是一个使用divs: JSFiddle的解决方案

HTML:

<div class="wrap_header">
    <div class="column">
        Name
    </div>
    <div class="column">
        Phone
    </div>
    <div class="clearfix"></div>
</div>
<div class="wrap_body">
    <div class="sliding_wrapper">
        <div class="serie">
            <div class="cell">
                AAAAAA
            </div>
            <div class="cell">
                323232
            </div>
            <div class="clearfix"></div>
        </div>
        <div class="serie">
            <div class="cell">
                BBBBBB
            </div>
            <div class="cell">
                323232
            </div>
            <div class="clearfix"></div>
        </div>
        <div class="serie">
            <div class="cell">
                CCCCCC
            </div>
            <div class="cell">
                3435656
            </div>
            <div class="clearfix"></div>
        </div>
    </div>
</div>

CSS:

.wrap_header{width:204px;}
.sliding_wrapper,
.wrap_body {width:221px;}
.sliding_wrapper {overflow-y:scroll; overflow-x:none;}
.sliding_wrapper,
.wrap_body {height:45px;}
.wrap_header,
.wrap_body {overflow:hidden;}
.column {width:100px; float:left; border:1px solid red;}
.cell {width:100px; float:left; border:1px solid red;}

/**
 * @info Clearfix: clear all the floated elements
 */
.clearfix:after {
    visibility:hidden;
    display:block;
    font-size:0;
    content:" ";
    clear:both;
    height:0;
}
.clearfix {display:inline-table;}

/**
 * @hack Display the Clearfix as a block element
 * @hackfor Every browser except IE for Macintosh
 */
    /* Hides from IE-mac \*/
    * html .clearfix {height:1%;}
    .clearfix {display:block;}
    /* End hide from IE-mac */

解释:

您有一个包含所有数据的滑动包装器。

注意事项:

.wrap_header{width:204px;}
.sliding_wrapper,
.wrap_body {width:221px;}

有17px的差异,因为我们需要考虑滚动条的宽度。