Header and Column Fixed Table

I'm struggling with a fixed header and fixed column table issue. The header stays in place, but the column doesn't. How can I solve this problem? I've attempted to use the position: fixed property on the first column, but it's not working correctly. Is there a solution that doesn't involve Javascript? (I've looked at previous questions on the same topic, but none of them have helped me). Here is my Codepen.

body {
  background-image: url(http://absfreepic.com/absolutely_free_photos/small_photos/green-grass-football-lawn-4020x2261_98957.jpg);
}

h3 {
  margin: auto;
  text-align: center;
  padding: 10px;
  color: white;
}

.table {
  background-color: white;
  margin: auto;
  width: 600px;
  border-collapse: separate;
  display: block;
  overflow-x: scroll;
}

thead,
tbody {
  display: inline-block;
}

tbody {
  overflow-y: scroll;
  overflow-x: hidden;
  max-height: 70px;
  position: relative;
}

th {
  background-color: lightgrey;
}

td,
th {
  min-width: 100px;
  max-width: 100px;
  word-wrap: break-word;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
  <h3>World Cup 2018</h3>

  <table class="table table-bordered">
    <thead>
      <tr>
        <th></th>
        <th colspan="6">Europe</th>
        <th colspan="3">South-America</th>
        <th colspan="2">Asia</th>
      </tr>
      <tr>
        <th>Countries</th>
        <th>Germany</th>
        <th>Spain</th>
        <th>Portugal</th>
        <th>France</th>
        <th>England</th>
        <th>Croatia</th>
        <th>Argentina</th>
        <th>Brazil</th>
        <th>Colombia</th>
        <th>Japan</th>
        <th>Russia</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th>Goalkeeper</th>
        <td>Neuer</td>
        <td>De Gea</td>
        <td>Beto</td>
        <td>Lloris</td>
        <td>Butland</td>
        <td>Subasic</td>
        <td>Caballero</td>
        <td>Alisson</td>
        <td>Ospina</td>
        <td>Kawashima</td>
        <td>Lunev</td>
      </tr>
      <tr>
        <th>Defender</th>
        <td>Boateng</td>
        <td>Ramos</td>
        <td>Pepe</td>
        <td>Varane</td>
        <td>Walker</td>
        <td>Lovren</td>
        <td>Otamendi</td>
        <td>Marcelo</td>
        <td>Sanchez</td>
        <td>Makino</td>
        <td>Granat</td>
      </tr>
      <tr>
        <th>Midfielder</th>
        <td>Kroos</td>
        <td>Iniesta</td>
        <td>Ronaldo</td>
        <td>Tolisso</td>
        <td>Lingard</td>
        <td>Modric</td>
        <td>Messi</td>
        <td>Paulinho</td>
        <td>Rodriguez</td>
        <td>Honda</td>
        <td>Golovin</td>
      </tr>
      <tr>
        <th>Forward</th>
        <td>Gomez</td>
        <td>Asensio</td>
        <td>Quaresma</td>
        <td>Griezmann</td>
        <td>Welbeck</td>
        <td>Perisic</td>
        <td>Dybala</td>
        <td>Neymar</td>
        <td>Bacca</td>
        <td>Osako</td>
        <td>Smolov</td>
      </tr>
    </tbody>
  </table>

Answer №1

body {
  background-image: url(http://absfreepic.com/absolutely_free_photos/small_photos/green-grass-football-lawn-4020x2261_98957.jpg);
}

h3 {
  margin: auto;
  text-align: center;
  padding: 10px;
  color: white;
}

.table {
  background-color: white;
  margin: auto;
  width: 600px;
  border-collapse: separate;
  display: block;
  overflow-x: scroll;
}

thead,
tbody {
  display: inline-block;
}

thead {
  position: sticky;
  top: 1px;
  z-index: 9999;
}

tbody {
  height: 80px;
}

th {
  background-color: lightgrey;
}

td,
th {
  min-width: 100px;
  max-width: 100px;
  word-wrap: break-word;
}

.fixed {
  position: sticky;
  width: 5em;
  left: 0;
  top: auto;
  z-index: 999;
}

td:not(.fixed) {
  z-index: 0;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="conatiner">
  <table class="table table-bordered">
    <thead>
      <tr>
        <th></th>
        <th colspan="6">Europe</th>
        <th colspan="3">South-America</th>
        <th colspan="2">Asia</th>
      </tr>
      <tr>
        <th class="fixed">Countries</th>
        <th>Germany</th>
        <th>Spain</th>
        <th>Portugal</th>
        <th>France</th>
        <th>England</th>
        <th>Croatia</th>
        <th>Argentina</th>
        <th>Brazil</th>
        <th>Colombia</th>
        <th>Japan</th>
        <th>Russia</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th class="fixed">Goalkeeper</th>
        <td>Neuer</td>
        <td>De Gea</td>
        <td>Beto</td>
        <td>Lloris</td>
        <td>Butland</td>
        <td>Subasic</td>
        <td>Caballero</td>
        <td>Alisson</td>
        <td>Ospina</td>
        <td>Kawashima</td>
        <td>Lunev</td>
      </tr>
      <tr>
        <th class="fixed">Defender</th>
        <td>Boateng</td>
        <td>Ramos</td>
        <td>Pepe</td>
        <td>Varane</td>
        <td>Walker</td>
        <td>Lovren</td>
        <td>Otamendi</td>
        <td>Marcelo</td>
        <td>Sanchez</td>
        <td>Makino</td>
        <td>Granat</td>
      </tr>
      <tr>
        <th class="fixed">Midfielder</th>
        <td>Kroos</td>
        <td>Iniesta</td>
        <td>Ronaldo</td>
        <td>Tolisso</td>
        <td>Lingard</td>
        <td>Modric</td>
        <td>Messi</td>
        <td>Paulinho</td>
        <td>Rodriguez</td>
        <td>Honda</td>
        <td>Golovin</td>
      </tr>
      <tr>
        <th class="fixed">Forward</th>
        <td>Gomez</td>
        <td>Asensio</td>
        <td>Quaresma</td>
        <td>Griezmann</td>
        <td>Welbeck</td>
        <td>Perisic</td>
        <td>Dybala</td>
        <td>Neymar</td>
        <td>Bacca</td>
        <td>Osako</td>
        <td>Smolov</td>
      </tr>
    </tbody>
  </table>
</div>

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 center my error message in Simple_form for Ruby on Rails?

Currently, my form is displaying an error message (Name is a required field) in this format: However, I want to position the 'can't be blank' error next to the 'Name', like so: To achieve this layout, I made modifications to the ...

How to dynamically alter a PHP variable using a JavaScript button click on the existing webpage

I've spent the last hour scouring stackoverflow for answers to my problem, but trying out solutions has only resulted in errors - perhaps because I'm attempting to implement it on the same page. I've experimented with saving the value to a ...

AngularJS function does not send back a value to the caller

Currently, I am diving into the world of AngularJS and decided to create a small playground just for fun. The issue I am facing is that for some reason, the return value from getMessage() function is not being passed back to the caller. Below is the code s ...

Fluidity of Containers on Mobile Devices

I currently have a list structured like this: https://i.sstatic.net/ei4Xt.png In order to display more details about each list item, I've added a hidden div that can be toggled open like so: https://i.sstatic.net/61k5Z.png While this works fine on ...

Tips for modifying Capybara selectors on a website with css-modules

When writing our automated tests, a typical line of code we use looks like this: find('.edit-icon').click As we move towards incorporating css-modules in our project, I've been informed that class names could undergo significant changes. A ...

Taking pictures on my website with the help of c#, HTML (excluding HTML 5), and javascript

My current project requires me to capture an image from a webcam, and I have been brainstorming solutions. However, there are specific conditions that I must adhere to in order to achieve the desired result: I am unable to utilize HTML5 due to compatibil ...

What is the process for daily web scraping of news articles using Python?

As I work on developing an application, I require daily news feeds from multiple websites. Utilizing Python's BeautifulSoup library seems like a viable option, but it may not be suitable for websites that display news across various pages which requir ...

How can I create an element in Javascript that does not inherit any CSS styles?

I'm creating a basic userscript with Greasemonkey for a school project that inserts a bright yellow box containing links onto all websites. Currently, my approach is straightforward: var guesses_div = document.createElement("div"); guesses_div.style. ...

Script tag causing alignment issues for centering div on page

Currently, I am in the process of creating a web application using Bootstrap 4.3.1 and integrating the Spotify API. My main goal right now is to center a div element on the screen both vertically and horizontally, which will display the authenticated user& ...

Save room for text that shows up on its own

I am currently dealing with a situation where text appears conditionally and when it does, it causes the rest of the page to be pushed down. Does anyone know the best way to reserve the space for this text even when it's not visible so that I can pre ...

Basic AJAX request not functioning properly

I am encountering an issue with a very simple AJAX call on my localhost. I am using a Windows 10 Machine with XAMPP running. I have checked the packages, and it seems that the AJAX request is not being sent to handle.php. Can someone help me figure out wha ...

What is the best way to prevent or highlight a particular row in a table depending on the line number values using Angular 2 and NgFor?

My work involves using angular2 and dealing with a table of games. I have a list of games in the database that have been clicked on before (each game can be clicked once). I want to add a class (whether it's a block or painted it doesn't matter) ...

Ensure the navbar remains visible by avoiding the need to include "bg-dark" class

As I work on creating a website using Laravel and Bootstrap, I encountered an issue with the navbar element. Originally set to navbar-light, I modified it to navbar-dark in order to achieve a dark and transparent navbar. Additionally, I removed the class b ...

I would like to create a vertical line similar to the ones seen on Twitter

I'm currently working on a project to create a social media platform similar to Twitter. One thing I'm trying to accomplish is to draw a vertical line between two images, like the one in this tweet from PewDiePie, and have it centered between two ...

Angular 13: Problems arise with custom theme in Angular Material version 13

I've set up a custom theme palette for my project that works perfectly with version ^12.2.13 of Angular Material, but not with ^13.2.3. Here's the SCSS code for my custom theming: my-custom-theme.scss @import '~@angular/material/theming&apo ...

Tips for avoiding accidentally selecting nearby text while holding down a button on your mobile device

My application requires the user to press and hold a button to record audio. However, on mobile devices, when the user holds the button, the browser attempts to select nearby text due to finger pressure. While this behavior is understandable, I want to pre ...

How to retrieve the index of a table row in Vue with Element UI?

Is there a way to add a button only to the first row of a table column (labeled as 'Option' in the example code) and check the row index using v-if="scope.row.index === 0"? The current method with scope.row.index is not working. <el- ...

CSS - The Failure of Implementing Multiple Segmented Controls

Recently, I came across a fantastic segmented control on and I am looking to customize it to fit my requirements. However, I encountered an issue where adding multiple controls to the page only affects the first segmented control. Instead of duplicating ...

Struggling to understand why my React Component is failing to render properly

Could there be an issue with how I imported react or am I simply overlooking something small? Any feedback would be greatly appreciated. As a beginner in programming, I may be missing a simple solution that I'm not experienced enough to identify. Than ...

Child element failing to resize along with parent container

Here is the HTML structure I have for a template.php page within my website. <!DOCTYPE html> <html lang="en"> <head> <title></title> ...... </head> <body> <div id="container"> ...