我创建了一个包含HTML的表格,我希望显示其标题是固定的(滚动只会影响正文)。
我找到了一个解决方案,它几乎可以随意使用。
tbody {
display:block;
height:500px;
overflow:auto;
}
thead, tbody tr {
display:table;
width:100%;
table-layout:fixed;/* even columns width , fix width of table too*/
}
thead {
width: calc( 100% - 1em )/* scrollbar is average 1em/16px width, remove it from thead width */
}
table {
width:400px;
}
问题是,使用属性table-layout: fixed
,它会修复列的宽度,因此我的列未正确显示(它们重叠):
我该如何解决? 我已经尝试了table-layout
的四个可能值答案 0 :(得分:0)
使用td {word-wrap: break-word;}
tbody {
display:block;
height:500px;
overflow:auto;
}
thead, tbody tr {
display:table;
width:100%;
table-layout:fixed;/* even columns width , fix width of table too*/
}
td{
word-wrap: break-word;
}
thead {
width: calc( 100% - 1em )/* scrollbar is average 1em/16px width, remove it from thead width */
}
table {
width:400px;
}
<table class="a">
<thead>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</thead>
<tbody>
<tr>
<td>AlfredsFutterkisteghrtrh</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</tbody>
</table>