我有一个表,最初有7列和4行。我希望固定宽度为td和th。挑战是我在表中动态添加列和行,我成功了,因为我面临的问题是我无法控制td,th的宽度。最初,我给table,td和tbody 100%的宽度,给td,th 15%的宽度。
这是我的CSS:
table {
width: 100%;
overflow:scroll;
}
thead {
width: 100%;
overflow:scroll;
}
tbody {
width: 100%;
overflow:scroll;
}
td{
width: 15%;
}
th{
width: 15%;
}下面是我的表格:
<section class="panel">
<div class="panel-body">
<div class="row">
<div class="table-responsive">
<table border="1px" class="fixed">
<thead>
<tr>
<th rowspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
</tr>
<tr>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
</tr>
<tr>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
</tr>
<tr>
<th rowspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
</tr>
<tr>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
</tr>
<tr>
<th rowspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
</tr>
<tr>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
</tr>
</tbody>
</table>
</div>
</div>
</div>

我想要的是给td,th 15%的宽度,如果td,th的总宽度超过100,那么应该有一个滚动条来,而th,td的宽度没有变化。
请帮帮忙。
发布于 2018-04-07 17:00:12
CSS
table {
display: block;
overflow: scroll;
white-space: nowrap;
}
th div
{
width:150px; //give here your approximate width
}将html更改为
<section class="panel">
<div class="panel-body">
<div class="row">
<div class="table-responsive">
<table border="1px" class="fixed">
<thead>
<tr>
<th rowspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
</tr>
<tr>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
</tr>
<tr>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
<th><div>Amount</div></th>
<th><div>Interest Rate</div></th>
</tr>
<tr>
<th rowspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
</tr>
<tr>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
</tr>
<tr>
<th rowspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
<th colspan="2">Banking Details</th>
</tr>
<tr>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
<th>Amount</th>
<th>Interest Rate</th>
</tr>
</tbody>
</table>
</div>
</div>
</div>https://stackoverflow.com/questions/49705372
复制相似问题