Creating a table with both a fixed header and a fixed column using only CSS

I'm looking to create an HTML table with a fixed header and fixed first column, but I want to avoid using JavaScript or jQuery to set scrollTop/scrollLeft for smooth functionality on mobile browsers. I found a solution that fixes the first column here: jsfiddle.net/C8Dtf/20/, but I need help enhancing it to also fix the header. Ideally, I'd like it to work on WebKit browsers or utilize CSS3 features without relying on JavaScript for scrolling. EDIT: Here's an example of the desired behavior: https://web.archive.org/web/20130829032141/http://datatables.net/release-datatables/extras/FixedColumns/css_size.html

Answer №1

The position: sticky CSS property offers support for both sticking elements to the top and the side in modern versions of Chrome, Firefox, and Edge browsers. You can leverage this feature by pairing it with a div that has the overflow: scroll property, allowing you to create a table with fixed headers that can be positioned anywhere on your webpage.

To implement this, start by enclosing your table within a container:

<div class="container">
  <table></table>
</div>

Add the overflow: scroll style to your container element to enable scrolling functionality:

div.container {
  overflow: scroll;
}

An important note from Dagmar: make sure to set a max-width and a max-height for your container as well.

You can utilize the position: sticky property to have certain table cells stick either to the edge or specific sides like top, right, or left:

thead th {
  position: -webkit-sticky; /* needed for Safari */
  position: sticky;
  top: 0;
}

tbody th {
  position: -webkit-sticky; /* needed for Safari */
  position: sticky;
  left: 0;
}

In case your first column contains <td> elements instead of <th> elements, consider using tbody td:first-child in your CSS per MarredCheese's suggestion.

To ensure the header in the first column remains stuck to the left, here's what you should adjust in your styles:

thead th:first-child {
  left: 0;
  z-index: 1;
}

/* Use overflow:scroll on your container to enable scrolling: */

div {
  max-width: 400px;
  max-height: 150px;
  overflow: scroll;
}


/* Use position: sticky to have it stick to the edge
 * and top, right, or left to choose which edge to stick to: */

thead th {
  position: -webkit-sticky; /* for Safari */
  position: sticky;
  top: 0;
}

tbody th {
  position: -webkit-sticky; /* for Safari */
  position: sticky;
  left: 0;
}


/* To have the header in the first column stick to the left: */

thead th:first-child {
  left: 0;
  z-index: 2;
}


/* Just to display it nicely: */

thead th {
  background: #000;
  color: #FFF;
  /* Ensure this stays above the emulated border right in tbody th {}: */
  z-index: 1;
}

tbody th {
  background: #FFF;
  border-right: 1px solid #CCC;
  /* Browsers tend to drop borders on sticky elements, so we emulate the border-right using a box-shadow to ensure it stays: */
  box-shadow: 1px 0 0 0 #ccc;
}

table {
  border-collapse: collapse;
}

td,
th {
  padding: 0.5em;
}
<div>
  <table>
    <thead>
      <tr>
        <th></th>
        <th>headheadhead</th>
        <th>headheadhead</th>
        <th>headheadhead</th>
        <th>headheadhead</th>
        <th>headheadhead</th>
        <th>headheadhead</th>
        <th>headheadhead</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th>head</th>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
      </tr>
      <tr>
        <th>head</th>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
      </tr>
      <tr>
        <th>head</th>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
      </tr>
      <tr>
        <th>head</th>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
      </tr>
      <tr>
        <th>head</th>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
      </tr>
      <tr>
        <th>head</th>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
      </tr>
    </tbody>
  </table>
</div>

Visit this link to see the result in action!

Answer №2

Currently, it is now achievable through CSS alone utilizing the position: sticky property.

Below is a brief snippet:

(Check out this jsFiddle link for a live demo: https://jsfiddle.net/hbqzdzdt/5/)

.grid-container {
  display: grid; /* Using this as a trick to make the .grid element size fit its content */
  overflow: auto;
  height: 300px;
  width: 600px;
}
.grid {
  display: flex;
  flex-wrap: nowrap;
}
.grid-col {
  width: 150px;
  min-width: 150px;
}

.grid-item--header {
  height: 100px;
  min-height: 100px;
  position: sticky;
  position: -webkit-sticky;
  background: white;
  top: 0;
}

.grid-col--fixed-left {
  position: sticky;
  left: 0;
  z-index: 9998;
  background: white;
}
.grid-col--fixed-right {
  position: sticky;
  right: 0;
  z-index: 9998;
  background: white;
}

.grid-item {
  height: 50px;
  border: 1px solid gray;
}
<div class="grid-container">
  <div class="grid">
    <div class="grid-col grid-col--fixed-left">
      <div class="grid-item grid-item--header">
        <p>HEAD</p>
      </div>
      <!-- Additional grid items here -->
    </div>

    <!-- Repeat similar structure for other grid columns -->

</div>
</div>

In terms of compatibility, this technique functions well across major browsers except for IE. A polyfill exists for position: sticky, but personal experience with it is lacking.

Answer №3

While there have been many suggestions proposed to address this issue, they seem to only tackle either the header or a column individually, often requiring the use of JavaScript in the process. The reason being, it is believed that achieving this effect solely through CSS is not possible. Here's why:

If one were to theoretically accomplish this task, it would involve nesting multiple scrollable divs within each other, each with its own scrolling direction. The table would then need to be divided into three distinct sections - the fixed header, the fixed column, and the remaining data.

However, here lies the challenge - while it is feasible to make one element remain static while scrolling, the other element is nested within the scrollable region of the first and thus susceptible to getting scrolled out of view itself, making it impossible to maintain a fixed position on the screen. Some may suggest using absolute or fixed positioning to counter this dilemma, but unfortunately, this approach disables the ability to scroll that particular container. It becomes a paradoxical situation where attempting to solve one problem creates another, rendering it unfeasible to achieve both simultaneously.

In my perspective, the optimal solution involves incorporating JavaScript. By distinctly separating the three elements and synchronizing their positions dynamically through JavaScript, a viable workaround can be achieved. Various helpful examples can be found in different discussions on this topic, including the resource linked below:

http://tympanus.net/codrops/2014/01/09/sticky-table-headers-columns/

Answer №4

Completing this task is quite challenging.

Check out the demo in the link below:

Link has been updated based on feedback from lanoxx

http://jsfiddle.net/C8Dtf/366/

Make sure to include the following additions:

<script type="text/javascript" charset="utf-8" src="http://datatables.net/release-datatables/media/js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="http://datatables.net/release-datatables/media/js/jquery.dataTables.js"></script>
<script type="text/javascript" charset="utf-8" src="http://datatables.net/release-datatables/extras/FixedColumns/media/js/FixedColumns.js"></script>

I can't think of any alternative method for achieving this. Especially not with just CSS.

This requires a thorough understanding. I hope this explanation helps :)

Answer №5

I have updated the code in jsfiddle to reflect the changes you were looking for.

http://jsfiddle.net/X9KjL/27/

Here is an example of the hardcoded titles:

<table id="left_table" class="freeze_table">
    <tr class='tblTitle'>
         <th>New Title 1</th>
         <th>New Title 2</th>
    </tr>
</table>

I also included some additional styling:

.tblTitle{
   position:relative;
    top:10px;
    margin-bottom:20px;
    background:lightgreen;
}
td, th{
    padding:10px;
    height:50px;
    width:50px;
    font-size:16px;
}

I hope this meets your requirements! :)

Answer №6

Here is a sample demonstration using purely CSS:

.table {
  table-layout: fixed;
  width: 500px;
  border-collapse: collapse;
}

.header th {
  font-family: Calibri;
  font-size: small;
  font-weight: lighter;
  border-left: 1px solid #000;
  background: #d0d0d0;
}

.body_panel {
  display: inline-block;
  width: 520px;
  height: 300px;
  overflow-y: scroll;
}

.body tr {
  border-bottom: 1px solid #d0d0d0;
}

.body td {
  border-left: 1px solid #d0d0d0;
  padding-left: 3px;
  font-family: Calibri;
  font-size: small;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
<body>

  <table class="table">

    <thead class="header">

      <tr>
        <th style="width:20%;">test</th>
        <th style="width:30%;">test 2</th>
        <th style="width:50%;">test 3</th>
      </tr>

    </thead>
  </table>

  <div class="body_panel">

    <table class="table">
      <tbody class="body">

        <tr>
          <td style="width:20%;">example text here</td>
          <td style="width:30%;">2</td>
          <td style="width:50%;">3</td>
        </tr>

        <!-- Additional rows can be added as needed -->

      </tbody>
    </table>

  </div>

</body>

Answer №7

Discovering a brilliant solution by Paul O'Brien to address the issue, I wanted to share the link with you: https://codepen.io/paulobrien/pen/LBrMxa

Here is the modified code for removing the style from the footer:

html {
  box-sizing: border-box;
}
*,
*:before,
*:after {
  box-sizing: inherit;
}
.intro {
  max-width: 1280px;
  margin: 1em auto;
}
.table-scroll {
  position: relative;
  width:100%;
  z-index: 1;
  margin: auto;
  overflow: auto;
  height: 350px;
}
.table-scroll table {
  width: 100%;
  min-width: 1280px;
  margin: auto;
  border-collapse: separate;
  border-spacing: 0;
}
.table-wrap {
  position: relative;
}
.table-scroll th,
.table-scroll td {
  padding: 5px 10px;
  border: 1px solid #000;
}
.table-scroll thead th {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
}

th:first-child {
  position: -webkit-sticky;
  position: sticky;
  left: 0;
  z-index: 2;
  background: #ccc;
}
thead th:first-child {
  z-index: 5;
}

Answer №8

If you're looking to solve the issue of fixed Headers and Columns, there is a solution available through a useful plugin:


Updated as of July 2019

Additionally, a new CSS-based solution has emerged for fixing Headers and Columns using the CSS property position: sticky;. This technique involves applying this property directly onto each TH element rather than their parent container. Find more information about it here.

Answer №9

I recently faced a challenge of creating a solution with a fixed header group and a fixed first column. To tackle this, I divided my table into separate div elements and utilized the power of jQuery to monitor window scrolling.

Check out the demo here

var prevTop = 0;
var prevLeft = 0;

$(window).scroll(function(event){
  var currentTop = $(this).scrollTop();
  var currentLeft = $(this).scrollLeft();

  if(prevLeft !== currentLeft) {
    prevLeft = currentLeft;
    $('.header').css({'left': -$(this).scrollLeft()})
  }
  if(prevTop !== currentTop) {
    prevTop = currentTop;
    $('.leftCol').css({'top': -$(this).scrollTop() + 40})
  }
});

Answer №10

My colleague and I are excited to announce the release of our latest GitHub project. We have developed an Angular directive that allows you to enhance your table layouts with fixed headers: https://github.com/objectcomputing/FixedHeader

This solution utilizes CSS and Angular exclusively, incorporating a directive that introduces additional div elements. No jQuery is necessary for its functionality.

While our implementation may not offer as many features as other alternatives, we believe it serves as a reliable option for those looking to implement fixed tables in their projects.

Answer №11

After extensive testing on Internet Explorer 9, Chrome, Firefox (Windows), and Safari (Mac), I have successfully developed a system that is:

  • Compatible with all major browsers
  • Fully functional without the need for JavaScript
  • Utilizes only one div and one table
  • Features fixed header and footer elements (except in IE) with a scrollable body. The header and body have consistent column widths

Here's the result:

HTML:

<thead>
<tr>
  <th class="nombre"><%= f.label :cost_center %></th>
  <th class="cabecera cc">Personal</th>
  <th class="cabecera cc">Dpto</th>
</tr>
</thead>

<tbody>
<% @cost_centers.each do |cc| %>
  <tr>
    <td class="nombre"><%= cc.nombre_corto %></td>
    <td class="cc"><%= cc.cacentrocoste %></td>
    <td class="cc"><%= cc.cacentrocoste_dpto %></td>
  </tr>
<% end %>
</tbody>

<tfoot>
<tr>
  <td colspan="3"><a href="#">Show more users</a></td>
</tr>
</tfoot>
</table>

CSS:

div.cost_center{
width:320px;
font-size:75%;
margin-left:5px;
margin-top:5px;
margin-bottom:2px;
float:right;
display:inline-block;
overflow-y:auto;
overflow-x:hidden;
max-height:300px;  
}

div.cost_center label { 
float:none;
font-size:14px;
}

/* Additional CSS styles omitted for brevity */

Answer №12

For those seeking to enhance their design by adding shadows under a fixed header and to the right of the first (fixed) column, a practical solution in pure CSS is available:

http://jsbin.com/nayifepaxo/1/edit?html,output

The key technique involves using the ::after pseudo-element to apply shadows to the right side of each first td element in every tr:

tr td:first-child:after {
  box-shadow: 15px 0 15px -15px rgba(0, 0, 0, 0.05) inset;
  content: "";
  position:absolute;
  top:0;
  bottom:0;
  right:-15px;
  width:15px;
}

After some trial and error, this method was successfully implemented and shared here for others facing similar challenges.

Answer №13

/* Apply overflow:scroll to the container for scrolling functionality: */

div {
  max-width: 400px;
  max-height: 150px;
  overflow: scroll;
}


/* Use position: sticky to stick elements to edges
 * Specify top, right, or left to choose which edge sticks: */

thead th {
  position: -webkit-sticky; /* for Safari */
  position: sticky;
  top: 0;
}

tbody th {
  position: -webkit-sticky; /* for Safari */
  position: sticky;
  left: 0;
}


/* To make header in first column stick to the left side: */

thead th:first-child {
  left: 0;
  z-index: 1;
}


/* Styling for better presentation: */

thead th {
  background: #000;
  color: #FFF;
}

tbody th {
  background: #FFF;
  border-right: 1px solid #CCC;
}

table {
  border-collapse: collapse;
}

td,
th {
  padding: 0.5em;
}
<div>
  <table>
    <thead>
      <tr>
        <th></th>
        <th>headheadhead</th>
        <th>headheadhead</th>
        <th>headheadhead</th>
        <th>headheadhead</th>
        <th>headheadhead</th>
        <th>headheadhead</th>
        <th>headheadhead</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th>head</th>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
      </tr>
      <tr>
        <th>head</th>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
      </tr>
      <tr>
        <th>head</th>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
      </tr>
      <tr>
        <th>head</th>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
      </tr>
      <tr>
        <th>head</th>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
      </tr>
      <tr>
        <th>head</th>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
        <td>body</td>
      </tr>
    </tbody>
  </table>
</div>

Header and footer alignment issue needs resolution

Answer №14

After encountering some confusion, I came up with a solution that seems to address the issue. Setting z-index properties is crucial for making it work correctly, although the solution may not be perfect, it suits my needs:

The key part of the CSS (in SCSS) involves defining the properties for the non-sticky elements first to establish the z-index values.

.wrapper {
position: relative;
white-space: nowrap;
overflow: auto;
height: 200px;
table {
    border-collapse: collapse;
    tbody {
        tr {
            td {
                padding: .5em .75em;
                box-shadow: inset 0px -1px 0px 0px #000000;
                position: relative;
                z-index: -9999;
                background: white;
                &:first-of-type {
                    position: -webkit-sticky;
                    position: sticky;
                    left: 0px;
                    background: #eee;
                    z-index: -999;
                    box-shadow: inset -1px -1px 0px 0px #000000;
                }
            }
            &:first-of-type {
                position: sticky;
                top: 0;
                td {
                    background: #eee; 
                    &:first-of-type {
                        left: 0;
                        top: 0;
                        background: #eee;
                        z-index: 999;
                    }
                }
            }
            &:nth-of-type(2) {
                position: sticky;
                top: calc(1em + 1em + 2px);
                background: white;
                td:first-of-type {
                    position: sticky;
                    left: 0;
                    z-index: 998;
                }
            }
        }
    }
}

}

https://codepen.io/the_Northway/pen/VwMmwXG

Answer №15

Perhaps this solution could suit your needs... It may be necessary to define specific column widths for the header row.

thead { 
    position: fixed;
}

Check out this jsfiddle example!

Update:

I'm not entirely convinced that achieving the desired effect solely with CSS is feasible. I invite someone to challenge this notion. Here's my work in progress. While it's not finished, it may serve as a starting point for you. Hopefully, it leads you in a positive direction.

Answer №16

One alternative approach is to leverage AngularJS. The AngularUI module offers a directive known as ng-grid that includes a functionality for column pinning. While it may not rely solely on CSS, this method stands out from the rest.

http://angular-ui.github.io/ng-grid/#columnPinning

Answer №17

Example of Pure CSS:

<div id="container">
    <div class="tableHeader">
        <table class="table-header table table-striped table-bordered">
            <thead>
                <tr>
                    <th>this</th>
                    <th>transmission</th>
                    <th>is</th>
                    <th>coming</th>
                    <th>to</th>
                    <th>you</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>we've got it...</td>
                    <td>alright you are go</td>
                    <td>uh, we see the Earth now</td>
                    <td>we've got it...</td>
                    <td>alright you are go</td>
                    <td>uh, we see the Earth now</td>
                </tr>
            </tbody>
        </table>
    </div>


    <div class="tableBody">
        <table class="table-body table table-striped table-bordered">
            <thead>
                <tr>
                    <th>this</th>
                    <th>transmission</th>
                    <th>is</th>
                    <th>coming</th>
                    <th>to</th>
                    <th>you</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>we've got it...</td>
                    <td>alright you are go</td>
                    <td>uh, we see the Earth now</td>
                    <td>we've got it...</td>
                    <td>alright you are go</td>
                    <td>uh, we see the Earth now</td>
                </tr>
            </tbody>
        </table>
    </div>

</div>

#container {
    width: auto;
    height: 200px;
    border: solid 1px #444;
    overflow: auto;
}

.tableHeader {
    position: fixed;
    height: 40px;
    overflow: hidden;
    margin-right: 18px;
    background: white;
}

.table-header tbody {
    height: 0;
    visibility: hidden;
}

.table-body thead {
    height: 0;
    visibility: hidden;
}

http://jsfiddle.net/cCarlson/L98m854d/

Limitation: The implementation of the fixed header relies heavily on specific dimensions, making abstraction not a feasible solution.

Answer №18

Sticky positioning may not function properly for certain elements like (thead) in Chrome and other WebKit browsers such as Safari.

However, it works correctly with (th)

body {
  background-color: rgba(0, 255, 200, 0.1);
}

.intro {
  color: rgb(228, 23, 23);
}

.wrapper {
  overflow-y: scroll;
  height: 300px;
}

.sticky {
  position: sticky;
  top: 0;
  color: black;
  background-color: white;
}
<div class="container intro">
  <h1>Sticky Table Header</h1>
  <p>Position : sticky doesn't work for some elements like (thead) in Chrome and other webkit browsers like Safari.</p>
  <p>But it works fine with (th)</p>
</div>
<div class="container wrapper">
  <table class="table table-striped">
    <thead>
      <tr>
        <th class="sticky">Firstname</th>
        <th class="sticky">Lastname</th>
        <th class="sticky">Email</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>James</td>
        <td>Vince</td>
        <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="90faf1fdf5e3d0f5e8f1fde0fcf5bef3fffd">[email protected]</a></td>
      </tr>
      <tr>
        <td>Jonny</td>
        <td>Bairstow</td>
        <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9b3b6b7b7a099bca1b8b...       
      <tr>
        <td>Pat</td>
        <td>Cummins</td>
        <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="7a0a1b0e3a1f021b170a161f54191517">[email protected]</a></td>
      </tr>
      <tr>
        <td>Adam</td>
        <td>Zampa</td>
        <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98e2f9f5e8f9d8fde0f9f5e8f4fdb6fbf7f5">[email protected]</a></td>
      </tr>
    </tbody>
  </table>
</div>

Alternatively, you can check out my CodePen example :

Answer №19

Presented below is an alternative approach I developed utilizing CSS grids, inspired by the insights shared within this discussion:

Discover the solution on CodeSandbox

Answer №20

This is my attempt at creating a CSS-only solution:

#wrapper{
    height: 120px;
    width: 220px;
    overflow: auto;
}
header{
    position: sticky;
    top: 0;
    background-color: lightgrey;
    z-index: 1;
}
td:first-child{
    position: sticky;
    left: 0;
    background-color: darkgrey;
}
th:first-child{
    position: sticky;
    top: 0;
    left: 0;
    z-index: 2;
    background-color: darkgrey;
}
<div id="wrapper">
    <table>
        <header>
            <th>Header A</th>
            <th>Header B</th>
            <th>Header C</th>
            <th>Header D</th>
            <th>Header E</th>
            <th>Header F</th>
            <th>Header G</th>
        </header>
        <tbody>
            <tr>
                <td>1</td>
                <td>2</td>
                <td>3</td>
                <td>4</td>
                <td>5</td>
                <td>6</td>
                <td>7</td>
            </tr>
            <tr>
                <td>1</td>
                <td>2</td>
                <td>3</td>
                <td>4</td>
                <td>5</td>
                <td>6</td>
                <td>7</td>
            </tr>
            <tr>
                <td>1</td>
                <td>2</td>
                <td>3</td>
                <td>4</td>
                <td>5</td>
                <td>6</td>
                <td>7</td>
            </tr><tr>
                <td>1</td>
                <td>2</td>
                <td>3</td>
                <td>4</td>
                <td>5</td>
                <td>6</td>
                <td>7</td>
            </tr><tr>
                <td>1</td>
                <td>2</td>
                <td>3</td>
                <td>4</td>
                <td>5</td>
                <td>6</td>
                <td>7</td>
            </tr><tr>
                <td>1</td>
                <td>2</td>
                <td>3</td>
                <td>4</td>
                <td>5</td>
                <td>6</td>
                <td>7</td>
            </tr>
        </tbody>
    </table>
    </div>

Answer №21

Here is a helpful resource that I believe will assist you: https://datatables.net/release-datatables/extensions/FixedHeader/examples/header_footer.html

To summarize, if you are familiar with creating a dataTable, all you need to do is include this jQuery code at the bottom of your script:

$(document).ready(function() {
    var table = $('#example').DataTable();

    new $.fn.dataTable.FixedHeader( table, {
        bottom: true
    } );
} );

The 'bottom: true' option is used to keep the bottom header fixed in place.

Answer №22

For those looking to utilize just HTML and CSS, here is a solution for your needs:
Check out this jsFiddle showcasing a script-free approach to creating a table with a fixed header. Adapting the markup for a fixed first column should be straightforward as well. Simply establish an absolutely-positioned table for the first column within the hWrapper-div and readjust the positioning of the vWrapper-div.
This solution can easily accommodate dynamic content using server-side or client-side templating engines, and performs effectively across modern browsers including older versions of IE starting from IE8.

Answer №23

Just looking to update the design like so

<table style="position: relative;">
   <thead>
      <thead>
        <tr>
           <th></th>
        </tr>
      </thead>
   </thead>
   <tbody style="position: absolute;height: 300px;overflow:auto;">
      <tr>
         <td></td>
      </tr>
   </tbody>
</table>

Demo: https://plnkr.co/edit/Qxy5RMJBXmkaJAOjBtQn?p=preview

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for effectively redirecting traffic to another website

Is there a way to redirect someone from a page URL like example.com/2458/233 to example2.com/2458/233? The first URL contains only a file that redirects to the other domain. Can anybody provide instructions on how to achieve this process? To clarify furt ...

Creating tooltips using React and Tailwind CSS

I'm currently working on creating tooltips for icons in my header. I have created a component that combines the icon and tooltip into one div, with Tailwind CSS styles applied. JSX Component: const HeaderIcon = ({ icon, redirect = window.location.pat ...

The width of the Div is set to 100%, yet there is still some empty space visible on

I am having trouble setting the background of my code to display a picture that is 300px in height and 100% in width. However, when I set the width to 100%, there are about 15px gaps on each side. I could adjust the width to 1000px, but that presents issue ...

Fixed width for the last column in DataTables

Here's the scenario: I have a jQuery script that loads DataTables, and I know how to set the "aoColumns" : "sWidth" parameter to fix the width of a specific column, which is working fine. However, my issue arises from having multiple tables with var ...

fewer security personnel leads to a higher success rate

I've tried searching for it online, but unfortunately, I couldn't find any helpful results. What I'm attempting to achieve is a mixin that can lighten or darken a color based on a given percentage. If the percentage is less than 0, then I w ...

How to implement CSS styling for a disabled component

In my application, I have a FormControlLabel and Switch component. When the Switch is disabled, the label in FormControlLabel and the button in Switch turn gray. However, I want to maintain the color of both the label (black) and the button (red). To test ...

What strategies can be used to ensure that the page layout adjusts seamlessly to even the smallest shifts in window size?

Of course, I am familiar with media queries and how they allow us to set specific min-width and max-width ranges for CSS changes. However, when I look at the website styledotme.com, I notice that the block/div beneath the navigation bar gradually shrinks ...

Is there a jQuery function that can produce repeated output and append content with each successive click?

Attempting to implement a dynamic searchbar with various parameters has led me to explore using jQuery to load and clone the searchbar file in order to append it to the body dynamically. I have made several attempts to modify selectors without achieving t ...

When the user clicks on an element, my JavaScript code dynamically updates the CSS property by changing the window

When a tag is clicked in HTML triggering an onclick event, the CSS property that was updated in the JavaScript does not persist. The changes appear momentarily and then disappear once the window is refreshed. Javascript: <script type="text/javascript"& ...

Adding CSS stylesheets on-the-fly in JavaFX

I am looking to incorporate a CSS file from the filesystem into my application. The goal is to allow users to dynamically add JavaFX CSS files that can be created by anyone and stored anywhere on their system. I attempted a test scenario to see if adding ...

Collision of CSS styles with a different CSS stylesheet causing conflicts

Currently, I am in the process of developing a website and have encountered a specific issue. Within my page, there are various divs and tables present. I have meticulously crafted classes and styles to customize the appearance of these elements to align ...

The CSS styles are not applied when using self.render("example.html") in Tornado/Python

I'm still learning Python and programming in general. I'm currently using Tornado as my web server to host my websites. However, when I try to generate a dynamic html page using self.render("example.html", variables here), the resulting page does ...

What's the reason for the inability to resize fieldset using both?

Can someone explain why the resize control is not enabled on the fieldset element? The documentation mentions that resize does not work with: Inline elements Block elements that have overflow set to visible fieldset { resize: both; overflow: h ...

Integrating a title field into every p-column with ng-template

I am exploring ng-templates for the first time. I have managed to customize each column to display names accurately, but I am encountering an issue. I would like to incorporate a title field in order to show a tooltip with the full name when hovering over ...

The ongoing calculation consistently yields a result of zero

This content belongs to the index.php file if(isset($_POST['submit'])) { echo header('Location:answer.php'); } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> ...

Encountering a roadblock while trying to work with AngularJS Material radio buttons

In one of my projects, I have implemented a polling system where users can choose a question from a list and then proceed to the options page. On the options page, users can select their answer choices and submit their responses. The results are then displ ...

Curved lines on canvas

I am currently using the stroke and path features in the canvas to create two lines, with the intention of giving them a curved, wave-like effect. I would like to achieve this without having to create an actual image in Photoshop. Is there anyone who can ...

How to switch between classes for elements and return to the original one when none of the elements is chosen

Hello there, I need some assistance. Here's the scenario: I am working with a grid of six items. My goal is to have the first item in the grid become active by default. When the user hovers over any of the remaining five items, I want the active clas ...

Trouble with retrieving data from localStorage

On my webpage, I have set up multiple input fields where users can enter data. When they click a button, the data from these inputs is stored inside <span>...</span> elements (the intention being that the text remains visible in these <span& ...

What is the best way to format a date input field so that when a user enters a year (yyyy), a hyphen (-

Need help with date formatting in the format yyyy-mm-dd. Seeking a way to prompt user input for the year and automatically append "-" to the date as needed. Also utilizing a datepicker tool for selecting dates. ...