Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save janjouketjalsma/ca5449d68caa8014029f5a2dd536f74d to your computer and use it in GitHub Desktop.

Select an option

Save janjouketjalsma/ca5449d68caa8014029f5a2dd536f74d to your computer and use it in GitHub Desktop.
Pure CSS3 Horizontally scrolling table with N fixed columns at the start.
/*
* Horizontal scrolling table
*/
table.horizontal-scrolling {
display: block; /* to enable scrolling within the boundaries of the table */
width: 100%; /* to make sure the element will resize to it's bounding box */
overflow-x: scroll; /* to enable scrolling horizontally */
text-align: left; /* optional: left aligned content */
background-color: white; /* We need a background color because other cells scroll behind these cells */
border-collapse: separate;
table-layout: fixed;
}
table.horizontal-scrolling .fixed-column {
position: sticky; /* to enable fixed column behaviour */
min-width: 150px; /* More predictable column widths, fixes several layout issues */
max-width: 300px; /* Twice the min-width to make sure the second to last column doesn't show up after the last one when scrolling horizontally */
overflow-x: scroll;
background-color: white;
}
table.horizontal-scrolling tr:hover .fixed-column {
background: var(--color-juri-lightest);
}
/* Required to make position sticky work up to 4 adjacent cells, offset by min-width of preceding cell */
table.horizontal-scrolling .fixed-column:nth-child(1) {
left: 0px;
}
table.horizontal-scrolling .fixed-column:nth-child(2) {
left: 150px;
}
table.horizontal-scrolling .fixed-column:nth-child(3) {
left: 300px;
}
table.horizontal-scrolling .fixed-column:nth-child(4) {
left: 453px;
}
table.horizontal-scrolling th,
table.horizontal-scrolling td {
border: 0px;
}
table.horizontal-scrolling th:not(:first-child),
table.horizontal-scrolling td:not(:first-child) {
border-left: 1px solid var(--color-juri-lighter);
}
table.horizontal-scrolling .fixed-column-last {
border-right: 2px solid var(--color-juri-lighter);
}
table.horizontal-scrolling tr:not(:last-child) td,
table.horizontal-scrolling th {
border-bottom: 1px solid var(--color-juri-lighter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment