如何防止我的td中的文字破坏我的表格对齐?

时间:2016-03-09 14:03:19

标签: javascript jquery html

我一直在使用只有jQuery的可滚动tbody表,唯一的问题是:每当文本比其他文本更大或更小时,我的thead和tbody就搞砸了,我怎么能阻止它这样做?

可滚动的tbody只能正常工作:

从右:

+-----------+
| h | h | h | -> Header is okay if all THs are about the same size
|¨¨¨¨¨¨¨¨¨¨¨|
| c | c | c | -> Same thing applies to the tbody 

问题:

+-----------+
|hasd| h | h| -> if the texts are different sizes the fields get screwed up
|¨¨¨¨¨¨¨¨¨¨¨|
| c |casd |c| -> Same thing applies to the tbody 

我的代码:



$(function() {
  makeItScrollable();
});
$(window).resize(function() {
  makeItScrollable();
});


function makeItScrollable() {
  $('table').css({
    "border-collapse": "collapse"
  });
  $('thead').css({
    "position": "absolute",
    "width": "100%"
  });
  $('tbody').css({
    "position": "absolute",
    "overflow-y": "scroll",
    "overflow-x": "hidden",
    "height": "100%"
  });

  $('tbody').css({
    'width': Math.ceil($('thead').width() + 17)
  });
  $('tbody > tr > td').css({
    'width': Math.ceil($('thead').width())
  });
  $('thead > tr > th').css({
    'width': $('tbody').width() / $('thead > tr > th').length
  });
  $('tbody').css({
    'margin-top': $('thead').height() - 1
  });
}

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

<style>
  .table {
    position: absolute;
    width: calc(100% - 30px);
    height: calc(100% - 36px);
  }
  td,
  th {
    border: 1px solid #000;
    text-align: center;
    margin: 0;
    padding: 0;
  }
</style>
<div class="table">
  <table>
    <thead>
      <tr>
        <th>HEADERererASDASDASDSA</th>
        <th>HEADER</th>
        <th>HEADER</th>
      </tr>
    </thead>

    <tbody>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
    </tbody>
  </table>
</div>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

根据@Marcos评论,您可以使用javascript / jQuery将第一个td中每个tbody > tr的宽度设置为等于相应thead > tr > th的宽度。

在这个例子中,我已经将样式更改从JS中删除,以使事情更清晰。除了Chrome上的其他任何东西外,这些都没有经过测试,但它应该足以让你入门。

$(function() {
  makeItScrollable();
});
$(window).resize(function() {
  makeItScrollable();
});


function makeItScrollable(){
  //add a class as a hook for our styles
  $('table').addClass("scrollable");
  //set the width of the first row of tds to match the td above (wont work is you have colspans)
  $('tbody > tr > td').each(function(index, value) {
      $(this).css("width",$("thead > tr > th:eq("+index+")").width()+1);
  });

}
/* default tables */
  .table {
    position: absolute;
    width: calc(100% - 30px);
    height: calc(100% - 36px);
  }
  table {width:100%;}
  td,
  th {
    border: 1px solid #000;
    text-align: center;
    margin: 0;
    padding: 0;
  }
/* scrollable tables */
  table.scrollable {
    border-collapse: collapse;
  }
  table.scrollable > tbody {
    position: absolute;
    overflow-y: scroll;
    overflow-x: hidden;
    height: 100%;
    margin-top: -1px;
  }
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>

<div class="table">
  <table>
    <thead>
      <tr>
        <th>HEADERererASDASDASDSA</th>
        <th>HEADER</th>
        <th>HEADER</th>
      </tr>
    </thead>

    <tbody>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
      <tr>
        <td>CELL</td>
        <td>CELL</td>
        <td>CELL</td>
      </tr>
    </tbody>
  </table>
</div>

答案 1 :(得分:1)

供您参考,以下想法是使用jquery .it根据特定列中的最大内容大小调整标题和数据单元格。

通过将标题移动到不同的表格来调整标记以方便我。

&#13;
&#13;
$(function() {
  makeItScrollable();
});
$(window).resize(function() {
  makeItScrollable();
});


function makeItScrollable() {


  var tableWidth = 0;

  $('thead tr td').each(function(tdIndex, td) {

    var maxWidth = 0;
    $('tr').find('td:nth-child(' + (tdIndex + 1) + ')').each(function(tdIndex, td) {

      var width = $(this).width();
      if (width > maxWidth) {
        maxWidth = width;
      }

    });
    tableWidth += maxWidth;
    $('tr').find('td:nth-child(' + (tdIndex + 1) + ')').width(maxWidth);


  });
  tableWidth = tableWidth + 20;
  $('.headers').width(tableWidth);
  $('.data_table').width(tableWidth);
  $('.data').width(tableWidth + 20);
}
&#13;
<!DOCTYPE html>
<html>

<head>
  <script src="https://code.jquery.com/jquery-2.1.4.js"></script>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>

<body>


  <style>
    td,
    th {
      border: 1px solid #000;
      text-align: center;
      margin: 0;
      padding: 0;
    }
    .data {
      height: 300px;
      overflow-y: scroll;
    }
  </style>
  <div class="table">
    <table class="headers">
      <thead>
        <tr>
          <td>HEADERererASDASDASDSAdfdsafdsf</th>
            <td>HEADER</th>
              <td>HEADER</th>
        </tr>
      </thead>
    </table>
    <div class='data'>
      <table class="data_table">


        <tbody>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELLCELLCELLCELLCELLCELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
          <tr>
            <td>CELL</td>
            <td>CELL</td>
            <td>CELL</td>
          </tr>
        </tbody>
      </table>
    </div>
  </div>
</body>

</html>
&#13;
&#13;
&#13;