<table style="text-align: left;width: 100%; float:left" cellpadding="0" cellspacing="1" border="1">
<tr>
<th style="width:25%">head</th>
<th style="width:25%">head</th>
<th style="width:25%">head</th>
<th style="width:25%">head</th>
</tr>
</table>
<div style="height:100px; overflow-y:scroll; width:100%; float:left;">
<table style="text-align: left;width: 100%; float:left" cellpadding="0" cellspacing="1" border="1">
<tbody>
<tr>
<td style="width:25%">row</td>
<td style="width:25%">row</td>
<td style="width:25%">row</td>
<td style="width:25%">row</td>
</tr>
<tr>
<td>row</td>
<td>row</td>
<td>row</td>
<td>row</td>
</tr>
<tr>
<td>row</td>
<td>row</td>
<td>row</td>
<td>row</td>
</tr>
<tr>
<td>row</td>
<td>row</td>
<td>row</td>
<td>row</td>
</tr>
<tr>
<td>row</td>
<td>row</td>
<td>row</td>
<td>row</td>
</tr>
<tr>
<td>row</td>
<td>row</td>
<td>row</td>
<td>row</td>
</tr>
<tr>
<td>row</td>
<td>row</td>
<td>row</td>
<td>row</td>
</tr>
<tr>
<td>row</td>
<td>row</td>
<td>row</td>
<td>row</td>
</tr>
</tbody>
</table>
</div>
在上面的代码中,我有2个html表
表1:对于标题
表2:对于身体
对于表2,我保留了用于滚动的div,这是我的要求。
当该div存在时,会出现对齐问题,如果我删除div,则两个表的对齐都是完美的
您能帮我解决这个问题吗?
答案 0 :(得分:0)
您可以在表1中再添加1个div并在右边留一个空白
<div style="margin-right: 15px;">
<table style="text-align: left;width: 100%; float:left;" cellpadding="0" cellspacing="1" border="1">
<tr>
<th style="width:25%">head</th>
<th style="width:25%">head</th>
<th style="width:25%">head</th>
<th style="width:25%">head</th>
</tr>
</table>
</div>
<div style="height:100px; overflow-y:scroll; width:100%; float:left;">
<table style="text-align: left;width: 100%; float:left" cellpadding="0" cellspacing="1" border="1">
<tbody>
<tr>
<td style="width:25%">row</td>
<td style="width:25%">row</td>
<td style="width:25%">row</td>
<td style="width:25%">row</td>
</tr>
<tr>
<td>row</td>
<td>row</td>
<td>row</td>
<td>row</td>
</tr>
<tr>
<td>row</td>
<td>row</td>
<td>row</td>
<td>row</td>
</tr>
<tr>
<td>row</td>
<td>row</td>
<td>row</td>
<td>row</td>
</tr>
<tr>
<td>row</td>
<td>row</td>
<td>row</td>
<td>row</td>
</tr>
<tr>
<td>row</td>
<td>row</td>
<td>row</td>
<td>row</td>
</tr>
<tr>
<td>row</td>
<td>row</td>
<td>row</td>
<td>row</td>
</tr>
<tr>
<td>row</td>
<td>row</td>
<td>row</td>
<td>row</td>
</tr>
</tbody>
</table>
</div>
答案 1 :(得分:0)
// Change the selector if needed
var $table = $('table.scroll'),
$bodyCells = $table.find('tbody tr:first').children(),
colWidth;
// Adjust the width of thead cells when window resizes
$(window).resize(function() {
// Get the tbody columns width array
colWidth = $bodyCells.map(function() {
return $(this).width();
}).get();
// Set the width of thead columns
$table.find('thead tr').children().each(function(i, v) {
$(v).width(colWidth[i]);
});
}).resize(); // Trigger resize handler
table.scroll {
width: 100%; /* Optional */
/* border-collapse: collapse; */
border-spacing: 0;
border: 2px solid black;
}
table.scroll tbody,
table.scroll thead { display: block; }
thead tr th {
height: 30px;
line-height: 30px;
/*text-align: left;*/
}
table.scroll tbody {
height: 100px;
overflow-y: auto;
overflow-x: hidden;
}
tbody { border-top: 2px solid black; }
tbody td, thead th {
width: 20%; /* Optional */
border-right: 1px solid black;
}
tbody td:last-child, thead th:last-child {
border-right: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<table class="scroll">
<thead>
<tr>
<th>Head 1</th>
<th>Head 2</th>
<th>Head 3</th>
<th>Head 4</th>
<th>Head 5</th>
</tr>
</thead>
<tbody>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
</tr>
<tr>
<td>Content 1</td>
<td>Lorem ipsum dolor sit amet.</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
</tr>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
</tr>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
</tr>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
</tr>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
</tr>
<tr>
<td>Content 1</td>
<td>Content 2</td>
<td>Content 3</td>
<td>Content 4</td>
<td>Content 5</td>
</tr>
</tbody>
</table>
来源:
HTML table with 100% width, with vertical scroll inside tbody http://jsfiddle.net/hashem/CrSpu/554/