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

What is the best way to customize the MenuProps of Material UI Select component using createTheme?

I'm trying to update the dropdown menu style of my Material UI Select component using createTheme, but I'm having trouble getting it to work. Here is the code I have so far. Can you spot what might be causing the issue? import { createTheme } fr ...

Display or conceal a div based on the size of the screen using HTML and CSS

Hey there, I recently finished my first React Project and I’m wondering if there’s a way to hide the 'side-menu' section on mobile screens using CSS. Any suggestions? <div className='side-menu'> <SiderComponent /> < ...

Analysis of printing functionality across Chrome, Safari, and Firefox browsers

When utilizing the print function of various browsers to save web content in a PDF file, I have observed that each browser behaves differently. This becomes an issue when the content spans multiple pages. For instance, Chrome prints correctly while Safari ...

Conceal the footer using CSS while the slide is in focus

Trying to figure out a way to hide the footer when a specific container is active: For example, how can I hide the footer when the second and third items are selected as active? <div class="owl-stage"> <div class="owl-item"></div> & ...

Generate unique identifiers in HTML

Below is my HTML and JavaScript code: <!DOCTYPE html> <html> <head> <style> .custom-div { border: 1px solid green; padding: 10px; margin: 20px; } </style> <script> ...

Anchor link leading to an incorrect location

Sorry for the potentially silly question, but I'm struggling to figure out what's happening here. I'm currently working on a website and trying to create an anchor link at . I've placed the anchor just before in this code: <a name ...

What could be causing my webpage content to be partially hidden by my CSS menu?

I am currently working on my website which features a CSS menu with my website logo, a text box, and a dropdown. Additionally, I have created a login form using a table like the one shown below. <p>&nbsp;</p><table width="40%" border="0 ...

Steps for designing a stationary footer in HTML and CSS

I have implemented a static footer on my website: HTML <div class="card fixedFooter"> <div class="card-body space-around"> <button type="button" class="buttonFooter" routerLink="/myRouter" > <i class ...

Styling Images with Gradient using CSS

Looking to create a unique effect on an image by adding a radial gradient that seamlessly fades into the background color. Despite my efforts, I haven't been able to achieve this using filters and my current code is a bit messy. Here's what I ha ...

Leveraging tailwind for achieving dynamic height responsiveness

My Tailwind-built page currently has a table in the bottom left corner that is overflowing, and I would like it to adjust its size to fit the available space and scroll from there. I want it to fit snugly in the bottom left corner of the browser viewport ...

The process of retrieving CSS attributes from a control found by XPath using Selenium IDE

During my Selenium IDE script execution, I am looking to identify and handle an error state. This specific error state is visually highlighted on the page with a select control changing its background color to a light shade of red. The xpath for the selec ...

Flask caches JSON files automatically

I am currently working on a visualization app using js and python. The functionality of my app is as follows: There is a textbox in the browser where I input an URL The URL is then sent to Python via Flask In Python, the URL is processed to create ...

Challenges arise when trying to load CSS on an EJS page in conjunction with using res.redirect()

When using Express with EJS, my base route is set as follows: router.get("/", productsData.getProducts); The "productsData" is sourced from my controllers page and the code snippet within that page is as follows: exports.getProducts = (req, res, next) => ...

Python's webdriver for data scraping techniques

codesample I am facing an issue while trying to interact with a button using Firefox, Python, and Webdriver. Every time I encounter the following error message: selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .pull- ...

Differences between HTML viewports and media queries

For simple webpages, my general rule is to utilize viewport units as they provide a lot of flexibility to the website. However, when dealing with complex webpages, I usually opt for media queries. In some cases, especially intermediate ones, I find it ben ...

The "maxlength" attribute does not function with the input type "number" in an HTML textbox

The maxlength attribute does not seem to be functioning properly when using type="number" ...

The appropriate use of spacing after text in the context of HTML and

Why is it that when I add padding or margin, no extra space is created after the text? Here is the output: I have tried adding padding and margin after the Stay Tuned text, but it seems to be not working. Any insights on what might be causing this issue? ...

Responsive full-screen background image display

Why is the background image not appearing as a full image on large screens but fitting fine on small screens? <!DOCTYPE html> <html dir="ltr"> <head> <meta charset="utf-8" dir="ltr" /> <title ...

Issue with uploading files in Internet Explorer using Ajax not resolved

I've been encountering an issue while uploading images using Ajax and PHP. Surprisingly, everything runs smoothly on Firefox but fails to work on Internet Explorer (I.E). Take a look at my HTML code below: <!doctype html> <head> <titl ...

Having a problem with the glitch effect in Javascript - the text is oversized. Any tips on how to resize

I found some interesting code on the internet that creates a glitch text effect. However, when I implement it, the text appears too large for my webpage. I'm struggling to adjust the size. Here is the current display of the text: too big This is how ...