如何将 <th> 标题与表中的 <td> 对齐?
Posted
技术标签:
【中文标题】如何将 <th> 标题与表中的 <td> 对齐?【英文标题】:How to align <th> Header with <td> in Table? 【发布时间】:2020-11-30 10:16:55 【问题描述】:在 DataTable 中启用滚动条 (ScrollY) 后,无法将表头与表数据对齐。 我尝试了许多在线可用的解决方案,但它似乎不起作用。 我试过 display:block 但没有运气。
查看 jsfiddle:https://jsfiddle.net/VM001/dzukjbh3/
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.js"> </script>
<script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap.min.js"></script>
Datatable:
$(document).ready(function()
$("#TableName").Datatable(
language: "search": "Filter",
paging: false,
info: true,
scrollY:'200px',
scrollCollapse: true,
scrollX: false,
);
);
<table id="TableName" class="table table-hover table-bordered table-striped>
<thead style="display:block">
<tr>
<th>Item1</th>
<th>Item2</th>
<th>Item3</th>
<th>Item4</th>
</tr>
</thead>
<tbody>
% for item in items_json %
<tr>
<td> item.a </td>
<td> item.b </td>
<td> item.c </td>
<td> item.d </td>
</tr>
</tbody>
</table>
【问题讨论】:
您能分享用于创建数据表的部分代码吗?如果没有代码示例,很难弄清楚发生了什么。 嘿,我已经用代码更新了问题...如果您需要任何其他详细信息,请告诉我。 我认为您还需要添加 CSS,也许还需要添加屏幕截图或 jsfiddle jsfiddle.net/VM001/dzukjbh3 这里添加了jsfiddle。不知何故滚动条没有出现在这里,但它是我的网页 【参考方案1】:我必须删除你的一些 css 规则,但它似乎对我有用:
$(document).ready(function()
$("#P1SxTable").DataTable(
language: "search": "Filter",
paging: false,
info: true,
scrollY:'50px',
scrollCollapse: true,
scrollX: false,
);
);
/*table
width: 100%;
table-layout: fixed;
*/
th
font-size: 15px;
font-weight: bolder;
text-align: center;
td
font-size: 15px;
text-align: center;
table.table-bordered
border: 2px solid black;
table.table-bordered > thead > tr > th
border: 1px solid black;
background-color: lightgrey;
table.table-bordered > tbody > tr > td
border: 1px solid black;
border-top: 1px solid;
/*thead
display: table;
width: 100%;
tbody
display: block;
height: 200px;
*/
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap.min.js"></script>
<div class="container-fluid">
<h2>Data</h2>
<table id="P1SxTable" class ="table table-hover table-bordered table-striped" style="width: 100%;">
<!--caption><b>P1Sx YTD data</b></caption-->
<thead>
<tr>
<th>Number</th>
<th>Severity</th>
<th>Description </th>
<th>Node Name</th>
<th>Assignee</th>
<th>Impact minutes</th>
</tr>
</thead>
<tbody>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
</tbody>
</table>
</div>
【讨论】:
以上是关于如何将 <th> 标题与表中的 <td> 对齐?的主要内容,如果未能解决你的问题,请参考以下文章