Skip to content

The HTML table sticky columns and rows stop working when I add css to keep the table within the page boundaries

I have a large table that is scrolling on the horizontal and vertical with the first column sticky and the three header rows sticky. This works to a point. However the table extends past the right side of the screen.

enter image description here

I would like it to remain in the boundaries of the screen. From the image you can see I have two issues:

  1. The table extends past the end of the screen on the right
  2. The Name heading is behind the list of names when scrolled (this does not happen in the attached jfiddle)

When I add this css:

table {
    display: block;
    overflow: scroll;

The table is within the boundary of the screen; however the sticky on the heading rows does not work. The left column sticky is also not as good (please see the image).

enter image description here

Please see my attempt at a jfiddle; however, I can not get the Bootstrap to work (I included the cdns).

My aim is to have the:

  1. Table stay within the boundaries of the screen
  2. Top heading rows and left column stick when I scroll
  3. Name heading remain in front when I scroll

A bit more of an explanation relating to making the “Name” cell in the top right corner sticky. This is the starting position:

enter image description here

When I scroll right the “Name” column is in front, which is correct:

enter image description here

However, when I scroll down the “Name” cell does not stick:

enter image description here

So what I need is for the “Name” cell to stick on both scroll right and down.


You can set/make the thead itself sticky instead of individual rows/trs. As for the names in the row, since they’re first-child of each tbody‘s trs you can select them and apply styles, like so:

  background: red;
  position: sticky;
  left: 0;

You can then wrap the table inside a block container to give it vertical and horizontal scrolling. See the CSS example below:

  margin: 0 auto;
  display: block;
  width: 98vw;
  height: 95vh;
  overflow: auto;
  border: 1px solid black;

I’ve edited your jsfiddle here.