HTML:表头与表体不同步

时间:2015-10-16 04:10:32

标签: html css

我创建了一个带有固定标题和水平/垂直滚动条的表,但是主体不同步。 这是它的jfiddle。 请告诉我我做错了什么

http://jsfiddle.net/nyCKE/7033/

    <table cellspacing="0" cellpadding="0" border="0" width="200">
   <tr>
   <td>
   <table cellspacing="0" cellpadding="1" border="1" width="200" >
     <tr style="color:white;background-color:grey">
        <th>Header 1</th>
        <th>Header 2</th>
     </tr>
   </table>
</td>
  </tr>
 <tr>
<td>
   <div style="width:300px; height:100px; overflow:auto;">
     <table cellspacing="0" cellpadding="1" border="1" width="400" >
       <tr>
         <td>new itemmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm</td>
         <td>new item</td>
       </tr>
       <tr>
         <td>new item</td>
         <td>new item</td>
       </tr>
          <tr>
         <td>new item</td>
         <td>new item</td>
       </tr>
   </table>  
   </div>
</td>

3 个答案:

答案 0 :(得分:0)

添加

th, td {
    width: 49px;
}
table table
{
    border: none;
}

可能对您有所帮助,如果您可以从第一行删除BG Image并使用普通颜色,那么行的高度也将被管理。

Fiddle

答案 1 :(得分:0)

创建该问题的 DOM ,通过更改它,您可以轻松实现您所寻求的目标。

您无需使用额外的<table>并使用单<table>来执行此操作:

<强> HTML

<table cellspacing="0" cellpadding="0" border="0" width="200">
<thead>
    <tr>
      <th>Header 1</th>
      <th>Header 2</th>
    </tr>
</thead>    
<tbody>
    <tr>
        <td>new itemmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm</td>
        <td>new item</td>
      </tr>
      <tr>
        <td>new item</td>
        <td>new item</td>
      </tr>
      <tr>
        <td>new item</td>
        <td>new item</td>
      </tr>
      <tr>
        <td>new item</td>
        <td>new item</td>
      </tr>
      <tr>
        <td>new item</td>
        <td>new item</td>
      </tr>
      <tr>
        <td>new item</td>
        <td>new item</td>
      </tr>
      <tr>
        <td>new item</td>
        <td>new item</td>
      </tr>
      <tr>
        <td>new item</td>
        <td>new item</td>
      </tr>
      <tr>
        <td>new item</td>
        <td>new item</td>
      </tr>
      <tr>
        <td>new item</td>
        <td>new item</td>
      </tr>
</tbody>

<强> CSS

.table-scroll {
    max-height: 200px;
    width:400px;
    overflow: auto;
    font-family: Arial;
}
table {
    background: #f5f5f5;
    border-collapse: separate;
    box-shadow: inset 0 1px 0 #fff;
    font-size: 12px;
    line-height: 24px;
    margin: 0;
    text-align: left;
    width: 700px;
}
th {
    background: url(http://jackrugile.com/images/misc/noise-diagonal.png),  linear-gradient(#777, #444);
    border-left: 1px solid #555;
    border-right: 1px solid #777;
    border-top: 1px solid #555;
    border-bottom: 1px solid #333;
    box-shadow: inset 0 1px 0 #999;
    color: #fff;
    font-weight: bold;
    padding: 10px 15px;
    position: relative;
    text-shadow: 0 1px 0 #000;
}
th:after {
    background: linear-gradient(rgba(255, 255, 255, 0),  rgba(255, 255, 255, .08));
    content: '';
    display: block;
    height: 25%;
    left: 0;
    margin: 1px 0 0 0;
    position: absolute;
    top: 25%;
    width: 100%;
}
th:first-child {
    border-left: 1px solid #777;
    box-shadow: inset 1px 1px 0 #999;
}
th:last-child {
    box-shadow: inset -1px 1px 0 #999;
}
td {
    border-right: 1px solid #fff;
    border-left: 1px solid #e8e8e8;
    border-top: 1px solid #fff;
    border-bottom: 1px solid #e8e8e8;
    padding: 10px 15px;
    position: relative;
    transition: all 300ms;
    word-break: break-all;
}
td:first-child {
    box-shadow: inset 1px 0 0 #fff;
}
td:last-child {
    border-right: 1px solid #e8e8e8;
    box-shadow: inset -1px 0 0 #fff;
}
tr {
    background: url(http://jackrugile.com/images/misc/noise-diagonal.png);
}
tr:nth-child(odd) td {
    background: #f1f1f1  url(http://jackrugile.com/images/misc/noise-diagonal.png);
}
tr:last-of-type td {
    box-shadow: inset 0 -1px 0 #fff;
}
tr:last-of-type td:first-child {
    box-shadow: inset 1px -1px 0 #fff;

tr:last-of-type td:last-child {
    box-shadow: inset -1px -1px 0 #fff;
}

请查看我的JSFiddle

  

请注意我在这里使用了 jQuery插件

floatthead

答案 2 :(得分:0)

请参阅此jsfiddle

我将你的html改为单表

<table>
        <tr>
            <th>Header 1</th>
            <th>Header 2</th>
        </tr>
        <tr>
            <td>new itemmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm</td>
            <td>new item</td>
        </tr>
        <tr>
            <td>new item</td>
            <td>new item</td>
        </tr>
        <tr>
            <td>new item</td>
            <td>new item</td>
        </tr>
        <tr>
            <td>new item</td>
            <td>new item</td>
        </tr>
        <tr>
            <td>new item</td>
            <td>new item</td>
        </tr>
        <tr>
            <td>new item</td>
            <td>new item</td>
        </tr>
        <tr>
            <td>new item</td>
            <td>new item</td>
        </tr>
        <tr>
            <td>new item</td>
            <td>new item</td>
        </tr>
        <tr>
            <td>new item</td>
            <td>new item</td>
        </tr>
        <tr>
            <td>new item</td>
            <td>new item</td>
        </tr>
</table>

并添加了此css

tbody {
     display: block;
     height: 200px;
     width: 300px;
     overflow-y: auto;
     overflow-x: auto;
 }