Skip to content
Advertisement

Freeze first row and first column of table

I am trying to freeze/lock the first row and the first column of a table.

I have tried giving thead position: absolute; or position: fixed; but it looks strange.

I have followed some answers but I am still confused how to make it.

My HTML / CSS Code :

JavaScript
JavaScript

Advertisement

Answer

Freeze First Row

Freezing the first row can be done with CSS by setting the table body to overflow: auto, and giving a fixed width to the table cells. (See example 1)

Freeze First Row & First Column

However, to get this behavior for both first row and first column, you need to separate the first row, first column, and first cell from the table, and then continuously set the position of these elements based on the scrolled position of the table body, upon a scroll event. (See example 2)

Example 1: (Freeze first row only)

JavaScript
JavaScript

Example 2: (Freeze first row and first column)

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement