试图使固定的表头在HTML,滚动和可见性问题

Trying to make fixed table header in HTML, scroll and visibility issues?

本文关键字:HTML 滚动 可见性问题 表头      更新时间:2023-09-26

我一直在尝试在HTML中制作一个固定标题的表(必要时还有一个水平滚动条)。这个JSFiddle显示了到目前为止的内容:

html, body {
    margin:0;
    padding:0;
    height:100%;
}
.horizontalscrollcontainer {
    position: relative;
    padding-top: 37px;
    overflow:auto;
}
.verticalscrollcontainer {
    background:transparent;
    overflow-y:auto;
    overflow-x:visible;
    height: 200px;
}
table {
    border-spacing: 0;
    width:120%;
}
td + td {
    border-left:1px solid #eee;
}
td, th {
    border-bottom:1px solid #eee;
    background: #ddd;
    color: black;
    padding: 10px 25px;
}
th {
    height: 0;
    line-height: 0;
    padding-top: 0;
    padding-bottom: 0;
    color: transparent;
    border: none;
    white-space: nowrap;
}
th div {
    position: absolute;
    background: transparent;
    color: black;
    padding: 9px 25px;
    top: 0px;
    margin-left: -25px;
    line-height: normal;
    border-left: 1px solid #800;
}
th:first-child div {
    border: none;
}
<div class="horizontalscrollcontainer">
    <div class="verticalscrollcontainer">
        <table>
            <thead>
                <tr class="header">
                    <th>Table attribute name
                        <div>Table attribute name</div>
                    </th>
                    <th>Value
                        <div>Value</div>
                    </th>
                    <th>Description
                        <div>Description</div>
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>align</td>
                    <td>left, center, right</td>
                    <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the alignment of a table according to surrounding text</td>
                </tr>
                <tr>
                    <td>bgcolor</td>
                    <td>rgb(x,x,x), #xxxxxx, colorname</td>
                    <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the background color for a table</td>
                </tr>
                <tr>
                    <td>border</td>
                    <td>1,""</td>
                    <td>Specifies whether the table cells should have borders or not</td>
                </tr>
                <tr>
                    <td>cellpadding</td>
                    <td>pixels</td>
                    <td>Not supported in HTML5. Specifies the space between the cell wall and the cell content</td>
                </tr>
                <tr>
                    <td>cellspacing</td>
                    <td>pixels</td>
                    <td>Not supported in HTML5. Specifies the space between cells</td>
                </tr>
                <tr>
                    <td>frame</td>
                    <td>void, above, below, hsides, lhs, rhs, vsides, box, border</td>
                    <td>Not supported in HTML5. Specifies which parts of the outside borders that should be visible</td>
                </tr>
                <tr>
                    <td>rules</td>
                    <td>none, groups, rows, cols, all</td>
                    <td>Not supported in HTML5. Specifies which parts of the inside borders that should be visible</td>
                </tr>
                <tr>
                    <td>summary</td>
                    <td>text</td>
                    <td>Not supported in HTML5. Specifies a summary of the content of a table</td>
                </tr>
                <tr>
                    <td>width</td>
                    <td>pixels, %</td>
                    <td>Not supported in HTML5. Specifies the width of a table</td>
                </tr>
            </tbody>
        </table>
    </div>
</div>
https://jsfiddle.net/byB9d/6248/

我的问题是,水平滚动条似乎去垂直scrollcontainer而不是水平scrollcontainer。是否有任何方法来调节这个(溢出-x被自动设置为自动,因为溢出-y被设置为自动)。

谢谢你的建议!

我猜你在css中需要这个:

table thead{position:fixed;}

小提琴

我会分成2个部分,并规定宽度(%如果响应)

<table id="table-top">
<tr>
<th>A</th>
<th>B</th>
</tr>
</table>
<div id="table">
<table id="table_id">
<tr>
<td>A</td>
<td>B</td>
</tr>
<tr>
<td>A</td>
<td>B</td>
</tr>
<tr>
<td>A</td>
<td>B</td>
</tr>
</table>
</div>
CSS....
#table{
    overflow-y: auto; 
    height:150px;
    border-bottom:1px solid grey;
}
table{
    width:100%;
}
table td, table th{
    border-color: grey;
    border: 1px solid;  
    width:50%;
}
https://jsfiddle.net/tonytansley/mvp4u54q/

使用这个可能会有帮助

<style>
    html, body {
        margin:0;
        padding:0;
        height:100%;
    }
    .container {
        position:relative;
        background:transparent;
        top:0px;
        overflow:auto;
        width: 100%;
        height: 200px;
    }
    table {
        border-spacing: 0;
        width:120%;
    }
    td + td {
        border-left:1px solid #eee;
    }
    td, th {
        border-bottom:1px solid #eee;
        background: #ddd;
        color: black;
        padding: 10px 25px;
    }
    th {
        height: 0;
        line-height: 0;
        padding-top: 0;
        padding-bottom: 0;
        color: transparent;
        border: none;
        white-space: nowrap;
    }
    th:first-child div {
        border: none;
    }
    .wrap {
        width: 617px;
    }
    .wrap table {
        width: 600px;
        table-layout: fixed;
    }
      </style>
    </head>
    <body>
    <div class="wrap">
        <table class="head">
            <tr>
                <td>Table attribute name</td>
                <td>Value</td>
                <td>Description</td>
            </tr>
        </table>
    <div class="container">
        <table>
                <tr>
                    <td>align</td>
                    <td>left, center, right</td>
                    <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the alignment of a table according to surrounding text</td>
                </tr>
                <tr>
                    <td>bgcolor</td>
                    <td>rgb(x,x,x), #xxxxxx, colorname</td>
                    <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the background color for a table</td>
                </tr>
                <tr>
                    <td>border</td>
                    <td>1,""</td>
                    <td>Specifies whether the table cells should have borders or not</td>
                </tr>
                <tr>
                    <td>cellpadding</td>
                    <td>pixels</td>
                    <td>Not supported in HTML5. Specifies the space between the cell wall and the cell content</td>
                </tr>
                <tr>
                    <td>cellspacing</td>
                    <td>pixels</td>
                    <td>Not supported in HTML5. Specifies the space between cells</td>
                </tr>
                <tr>
                    <td>frame</td>
                    <td>void, above, below, hsides, lhs, rhs, vsides, box, border</td>
                    <td>Not supported in HTML5. Specifies which parts of the outside borders that should be visible</td>
                </tr>
                <tr>
                    <td>rules</td>
                    <td>none, groups, rows, cols, all</td>
                    <td>Not supported in HTML5. Specifies which parts of the inside borders that should be visible</td>
                </tr>
                <tr>
                    <td>summary</td>
                    <td>text</td>
                    <td>Not supported in HTML5. Specifies a summary of the content of a table</td>
                </tr>
                <tr>
                    <td>width</td>
                    <td>pixels, %</td>
                    <td>Not supported in HTML5. Specifies the width of a table</td>
                </tr>
        </table>
    </div>
    </div>

找到这个小提琴…看起来很适合你:

使用一个section来包装表格[Not my work just提供链接]

<section class="">

小提琴

https://jsfiddle.net/agpq359b/

不确定,这是你想要的

<div class="container">
<table>
    <thead>
        <tr class="header">
            <th>Table attribute name
            </th>
            <th>Value
            </th>
            <th>Description
            </th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>align</td>
            <td>left, center, right</td>
            <td>Not supported in HTML5. Deprecated in HTML 4.01. Specifies the alignment of a table according to surrounding text</td>
        </tr>
        </tbody>
    </table>
</div>
html, body {
    margin:0;
    padding:0;
    height:100%;
}
.container {
    background:transparent;
    overflow:auto;
    width: 100%;
    height: 200px;
}
table {
    border-spacing: 0;
    width:120%;
}
td, th {
    border-bottom:1px solid #eee;
    background: #ddd;
    color: black;
    padding: 9px 25px
}
th:nth-of-type(1), td:nth-of-type(1){
    width:200px;
}
th:nth-of-type(2), td:nth-of-type(2){
    width:100px;
}
th:nth-of-type(3), td:nth-of-type(3){
    width:200px;
}
td + td {
    border-left:1px solid #eee;
}
thead {
    position:absolute;
    border: none;
    top:0px;
}
tbody{
    margin-top:38px;
    display:block;
}

我认为你最好把标题从容器中取出来:

<div class="xclass">
    <table>
        <thead>
            <tr class="header">
                <th>Table attribute name
                    <div class="headerdiv">Table attribute name</div>
                </th>
                <th>Value
                    <div>Value</div>
                </th>
                <th>Description
                    <div>Description</div>
                </th>
            </tr>
        </thead>
   </table>
</div>

检查此提琴- https://jsfiddle.net/byB9d/6241/