Table with a fixed header and scrolling body in Bootstrap 4

https://i.sstatic.net/YasoT.pnghttps://i.sstatic.net/ubmHZ.png I'm new to the world of front-end development and I'm currently working on a table layout. Here's the code snippet of the table I've created:

  <div className="table-responsive">
    <table className="table table-hover" id="job-table">
      <thead>
        <tr className="text-center">
          <th scope="col">Sr.No.</th>
          <th scope="col">Company Name</th>
          <th scope="col">Technology</th>
          <th scope="col">Total Resumes</th>
          <th scope="col">Job Title</th>
          <th scope="col">Total Score</th>
          <th scope="col">Average Score</th>
        </tr>
      </thead>
      <tbody className="text-center">
        {this.props.list && this.props.list.content && this.props.list.content.length > 0 && this.props.list.content.map((item, key) => {
          return (
            <tr key={key}>
              <td className="font-weight-bold">1</td>
              <td className="font-weight-bold">ABCDED</td>
              <td>Software Developer</td>
              <td className="font-weight-bold">17</td>
              <td>5 years experience</td>
              <td className="font-weight-bold">30</td>
              <td className="font-weight-bold">30</td>
            </tr>
          )
        })}
      </tbody>
    </table>
  </div>

I have implemented the table-responsive class to make the table scrollable, but it's not functioning as expected.

tbody { height: 400px; overflow-y: scroll }

Another issue I'm facing is with large content in the td cells affecting other rows. How can I prevent this from happening?

https://i.sstatic.net/ubmHZ.png

Answer №1

To create a fixed header, you can utilize the CSS property position: sticky.

Take a look at the example code.

In this case, adjust the height of the main container.

.table-responsive{height:400px;overflow:scroll;} 

Apply the position sticky to the table row's heading cells.

thead tr:nth-child(1) th{background: white; position: sticky;top: 0;z-index: 10;}

Answer №2

Give flex a shot, it should work well with the table-responsive class:

table {
    display: flex;
    flex-flow: column;
    width: 100%;
}

thead {
    flex: 0 0 auto;
}

tbody {
    flex: 1 1 auto;
    display: block;
    overflow-y: auto;
    overflow-x: hidden;
}

tr {
    width: 100%;
    display: table;
    table-layout: fixed;
}

Hopefully this solution proves helpful

Answer №3

Include display:block in the table to resolve this issue

tbody{height: 400px; overflow-y: scroll;display:block;}
th { position: sticky; top: 0; }

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

Utilizing typeahead.js for optimal performance with an extensive database

With a vast database containing 10,000 addresses and 5,000 individuals, I aim to provide users with the ability to search for either an address or a user. My goal is to utilize Twitter's typeahead feature to suggest results as they input text. To see ...

Utilizing a single variable across different JavaScript scripts - where one script includes a .ready function while the other does not

I am facing a challenge with 2 javascript scripts that I have: <script type="text/javascript"> function capture(){ var canvas = document.getElementById('canvas'); var video = document.getElementById('video'); canvas.getContext ...

Convert JSON objects within an array into HTML format

Is there a way to reformat an array of JSON objects that has the following structure? [{"amount":3,"name":"Coca-Cola"},{"amount":3,"name":"Rib Eye"}] The desired output in plain HTML text would be: 3 - Coca-Cola 3 - Rib Eye What is the best approach to ...

Converting text/plain form data to JSON using Node.js - step by step guide

I am currently working on a Node.js application to execute a POST call to an API for placing an order. However, the data I receive in our app is in text/plain format and not JSON. This is the current format of the data: TypeOrder=buy Coin=BTC AmountCoin= ...

Tips on utilizing browser.getCurrentUrl() within a Protractor examination

I’ve been wrestling with these lines of Protractor code today: element(by.linkText("People")).click(); browser.waitForAngular(); var url = browser.getCurrentUrl(); ... It seems that getCurrentUrl always throws an error when placed after a waitF ...

The form data should persist even if the user switches screens or logs out of the session, ensuring that the information remains intact

As a user fills out data in form input fields, ensuring that the entered values persist even if the user navigates to other pages or logs out of the session can be a challenge. How can we achieve this functionality in vue.js? Despite my attempts, I have no ...

Tips for Preventing Ant Design from Overriding Existing CSS Styles

Currently, I am working on a ReactJS project. Initially, the entire project was designed using pure CSS. However, I decided to incorporate a small Antd component into my project. Following the provided instructions, I imported the component like this in on ...

What is the process for including points to the scoreboard?

I am currently working on a simple JavaScript game as my very first project. The game involves clicking a button to generate random numbers for both the user and computer, and the winner is determined by the larger number. I have set up a scoreboard to kee ...

Updates to $scope are not reflecting in the application

My input includes a datalist that is populated by an angular get request as the page loads. <input list="data" /> <datalist id="data"> <option ng-repeat="item in items" value="{{item.data}}"> </datalist> The $http call is straig ...

Building multi-dimensional array checkboxes in AngularJS - a step-by-step guide

I am working with a multi-dimensional array and I need to display all the data using checkboxes. The array below contains dietary requirements, and I want to create checkboxes for each entry where the value is true. How can I use ng-repeat to iterate over ...

Reorganizing MongoDB arrays with Prisma and NextJS

I'm dealing with data stored in MongoDB that looks like this: { items: [ { id: '6614005475802af9ae05b6b2', title: 'title', createdAt: '2024-04-08T14:33:56.734Z', }, { id: '6613f ...

The concept of CSS div wrapping and managing vertical whitespace in web design

My goal is to make multiple div elements wrap to the next line when the width of their container is changed. The wrapping part is working fine, as the boxes drop to the next line when there isn't enough width for them to fit. However, I am facing an i ...

Trouble accessing Strapi CMS data on Next.js frontend using React.js

Currently, I am in the process of developing a website using Strapi as the CMS and Next.js(React) for the Frontend. The website features an image slider that includes an image, a headline, and a description. I am trying to retrieve these elements from my S ...

My code fails to recognize the top property when the window size is 1300px

Error at the Top Not Being Recognized: Hello, I am facing an issue where the top part of the webpage does not behave correctly when the window size is less than 1300px. The condition set for 100% top only applies after refreshing the page; otherwise, it d ...

The anticipated data type is derived from the attribute 'value' defined within the 'IntrinsicAttributes & ProviderProps<IProductContext>' type

During the creation of an e-commerce website, I decided to use React Typescript. However, I encountered an issue with Contexts while trying to export my state in the Provider value. Thank you for any assistance provided. ERROR Type '{ products: IP ...

Utilize the 'document / redirect' API within an express controller function

Here is how I have an API call using document / redirect: https://i.sstatic.net/HIrbc.png This call is made within HTML Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transit ...

req.body is not defined or contains no data

I am facing an issue with my controllers and routers. bookController.js is functioning perfectly, but when I try to use userControllers for registration and login logic, req.body always appears empty. I tried logging the form data using console.log, but it ...

Manage a large quantity of inputs effortlessly within an accordion using React-Native with just one click from the parent component

I recently started exploring the world of React. In my current project, I have multiple custom accordion components with lists of text inputs. I'm looking for a way to handle these input systems with just one button click from the parent component of ...

Retrieving and displaying data either one by one or in groups, without knowing the exact number of records

Currently, I am developing a Ruby on Rails + React application where I fetch and display records using axios and useEffect. Everything works perfectly as expected, with all the data being displayed after fetching. However, I have a new requirement to fetc ...

Can you provide guidance on integrating this jQuery script into my Next.Js application effectively?

I have been using a script on a plain HTML website, importing it via the <script> tag: (function($) { var wa_time_out, wa_time_in; $(document).ready(function() { $(".wa__btn_popup").on("click", function() { if ($(&qu ...