我创建了带有固定标题和水平/垂直滚动条的表,但是主体不同步。
这是它的小提琴。
请指教我在做什么错

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>

最佳答案

正是您的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

关于html - HTML:表头与表主体不同步,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33162441/

10-12 12:54
查看更多