
使用 position: sticky 结合语义化 结构,可高效实现滚动时表头(top)与首列(left)始终可见的类 Excel 布局,无需 JavaScript,兼容现代浏览器。
使用 `position: sticky` 结合语义化 `
<div class="relative h-96 w-full overflow-auto border border-gray-300">
<table class="min-w-full border-collapse table-fixed">
<thead class="bg-gray-700 text-white"><tr>
<th class="p-4 sticky top-0 left-0 z-20 w-48">Task</th>
<th class="p-4 sticky top-0 z-10">Mon</th>
<th class="p-4 sticky top-0 z-10">Tue</th>
<th class="p-4 sticky top-0 z-10">Wed</th>
<th class="p-4 sticky top-0 z-10">Thu</th>
</tr></thead>
<tbody>
<tr>
<td class="bg-gray-200 font-medium p-4 sticky left-0 z-10">Design</td>
<td class="p-4 border-b border-gray-200">✓</td>
<td class="p-4 border-b border-gray-200">✓</td>
<td class="p-4 border-b border-gray-200">—</td>
<td class="p-4 border-b border-gray-200">✓</td>
</tr>
<tr>
<td class="bg-gray-200 font-medium p-4 sticky left-0 z-10">Dev</td>
<td class="p-4 border-b border-gray-200">—</td>
<td class="p-4 border-b border-gray-200">✓</td>
<td class="p-4 border-b border-gray-200">✓</td>
<td class="p-4 border-b border-gray-200">✓</td>
</tr>
<tr>
<td class="bg-gray-200 font-medium p-4 sticky left-0 z-10">Test</td>
<td class="p-4">✓</td>
<td class="p-4">—</td>
<td class="p-4">✓</td>
<td class="p-4">✓</td>
</tr>
</tbody>
</table>
</div>











