Scrollable tbody in a responsive Bootstrap 4 table

Utilizing Bootstrap 4, I have implemented a responsive table that I want to have a scrollable body. The table is structured as follows:

<div class="table-responsive">
  <table class="table table-striped table-hover custom-table text-nowrap">
    <thead>
      <tr>
        <th>row1</th>
        <th>row2</th>
        <th>row2</th>
      </tr>
    </thead>
    <tbody>
      <!-- some rows -->
    </tbody>
  </table>

To enable scrolling in the tbody, I applied a scss class .custom-table to the table, like this:

.custom-table {
  tbody {
     height: 50px;
     overflow-y: auto;
     width: 100%;
     display: block;
  }
}

Unfortunately, the applied styling is not functioning as expected. The content in the tbody is shifting to the left. You can view the issue in this JSFiddle example. What could be causing this problem?

Answer №1

Kindly review below, I personally opt for utilizing flexbox. I have also integrated -webkit and -ms prefixes to ensure better cross-browser compatibility.

  tbody , thead {
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
  }

  tr {
    -ms-flex-preferred-size: 100%;
    flex-basis: 100%;
    display: -webkit-box;
    display: -ms-flexbox;
    display: flex;
  }

  th,td {
    -webkit-box-flex: 1;
    -ms-flex: 1;
    flex: 1;
    text-align: center;
  }

  tbody {
     height: 50px;
     overflow-y: auto;
  }

  thead {
    padding-right: 20px;
  }

I have applied padding-right to thead specifically due to the scrollbar present on tbody.

Answer №2

Have you attempted to implement this?

Adjust your CSS code to look like this

.custom-table> tbody {
    display:block;
    height:50px;
    overflow:auto;
}
.custom-table> thead, tbody tr {
    display:table;
    width:100%;         
}
.custom-table> thead {
    width:   100%   
}
.custom-table {
    width:100%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="table-responsive">
  <table class="table table-striped table-hover custom-table text-nowrap">
    <thead>
      <tr>
        <th>row1</th>
        <th>row2</th>
        <th>row2</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>text1</td>
        <td>text2</td>
        <td>text3</td>
      </tr>
      <tr>
        <td>text4</td>
        <td>text5</td>
        <td>text6</td>
      </tr>
      <tr>
        <td>text7</td>
        <td>text8</td>
        <td>text9</td>
      </tr>
    </tbody>
  </table>
</div>

Answer №3

Implementing Udara Kasun's approach with adjustments for better column alignment:

.custom-table>tbody {
  display: block;
  height: 50px;
  overflow: auto;
}
.custom-table>thead,
tr {
  display: block;
  width: 100%;
}

.custom-table>thead {
  width: 100%
}

.custom-table {
  width: 100%;
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="187a77776c6b6c6a7968582d3628362a">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="294b46465d5a5d5b4859691c0719071b">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>

<div class="container-fluid">
  <div class="flex-group-1" style="overflow-y: auto">
    <table class="table table-striped table-hover custom-table table-bordered table-responsive ">
      <thead>
        <tr>
          <th width="100">col1</th>
          <th width="100">col2</th>
          <th width="100">col3</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td width="100">text1</td>
          <td width="100">text2</td>
          <td width="100">text3</td>
        </tr>
        <tr>
          <td width="100">text4</td>
          <td width="100">text5</td>
          <td width="100">text6</td>
        </tr>
        <tr>
          <td width="100">text7</td>
          <td width="100">text8</td>
          <td width="100">text9</td>
        </tr>
      </tbody>
    </table>
  </div>
</div>

Answer №4

How to Create a Fixed Heading Row and Vertically Scrollable Table in Bootstrap 5 Using CSS Only

To achieve this effect, you need to wrap the table in a div element.

Here is the HTML code:

<!-- Table  -->
  <div class="container shadow-sm alarm-table">
    <table class="table">
      <thead class="table-primary">
        <tr>
          <th scope="col">ID</th>
          <th scope="col">Name</th>
          <th scope="col">Email</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>John</td>
          <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="701a1f181e30151d11191c5e131f1d">[email protected]</a></td>
        </tr>
        <tr>
          <td>1</td>
          <td>John</td>
          <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d6bcb9beb896b3bbb7bfbaf8b5b9bb">[email protected]</a></td>
        </tr>
        <tr>
          <td>1</td>
          <td>John</td>
          <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e48e8b8c8aa48189858d88ca878b89">[email protected]</a></td>
        </tr>
        <tr>
          <td>1</td>
          <td>John</td>
          <td><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="157f7a7d7b557078747c793b767a78">[email protected]</a></td>
        </tr>
      </tbody>
    </table>
  </div>

And the CSS code:

/* Makes table scrollable */
.alarm-table{
  height: 150px;
  overflow: auto;
}

/* Makes table heading-row stick to top when scrolling */
.container .table thead tr{
  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

What is the best way to save a PDF from within a frame using JavaScript and an HTML5 <embed> tag?

I'm looking for assistance with a script for my website that dynamically generates a PDF after the user makes selections in one of the frames. The website uses the HTML5 tag to display the PDF file. Can anyone provide guidance on a script that can: ...

Interactive feature in Three.js causes page to scroll downwards upon object selection

Exploring Three.js for the first time, I'm looking to integrate it into the landing page of my website. However, I've encountered an issue where clicking on the scene area with my object causes the page to shift downward. I've tried adding o ...

The PointLight feature in Three.js does not support more than 2 lights simultaneously

Check out this demo: http://jsfiddle.net/PRz4G/ You will notice four white points surrounding a blue one. The intention was for each of those points to have a light in the same position, but unfortunately, three of them are not functioning correctly. Ta ...

AJAX code fetching dynamic content without relying on file loading

When I load my program, the dynamic code does not appear on the page until I reload it again. I attempted to use the onload event in the body to load content from an XML file using AJAX, but it only works after closing and reopening the program, not dynam ...

Do not make unnecessary calls to the server when changing the display property of an HTML object

How can I prevent a server request for the pdf file? I've experimented with using visibility=hidden and width=0. <object class='pdfClass' data='"+conventionId+"/showPdf.html' width='100%' height='600'>< ...

Secret message concealed within the depths of my Bootstrap 5 Side Navbar

I have successfully created a side NavBar using Bootstrap 5, but I'm facing an issue where my text is getting hidden behind the navbar. Any suggestions on how I can resolve this? Below is the code for my side Navbar: const Navbar = ({ handleClick, is ...

Struggling to locate a straightforward sidebar solution without relying on Bootstrap. Finding an easy-to-use answer seems

I have a lightweight and easy-to-understand code for a website project with two panels. The first panel on the left is a large navigation (270px width, top to bottom, similar to a wiki) with approximately 30 unordered list items. Next to it is the second ...

"After clicking the button for the second time, the jQuery on-click function begins functioning properly

(Snippet: http://jsfiddle.net/qdP3j/) Looking at this HTML structure: <div id="addContactList"></div> There's an AJAX call that updates its content like so: <div id="<%= data[i].id %>"> <img src="<%= picture %&g ...

Removing Borders from HTML Tables

I am struggling to eliminate the border of a table within a gridview. The table is located inside the itemtemplate, and I can't figure out how to remove the border. Can someone please assist me with this? Thank you. Shown below is the code I am worki ...

Converting plain text to HTML in emails using Outlook

Trying to figure out how to maintain the formatting of plain text email while displaying as virtual plain text in C sharp, specifically when receiving in Outlook 2007 with VSTO. The current code is not preserving the original formatting, instead it changes ...

Preserve marked checkboxes upon page refresh

So I created a search engine with a filter using checkboxes in a form. The filter function is working fine when I submit the form (I'm using Flask for this). However, once the results are displayed, the checkboxes that were used as filters become unch ...

Angular material: issue with alignment of table inside mat-expansion compared to other table

Hey there, I've encountered an issue on an Angular website where the columns in a table are not aligning properly. Does anyone have a solution for this problem? Thanks! <div class="table-responsive "> <table class="table" style=" ...

How to troubleshoot a Django template where the Bootstrap modal dialog fails to appear upon clicking a button or

How can I implement a button in a Django view to trigger a model dialog for confirming the deletion of a list item? I have tried to display the modal dialog when the button is clicked, but it's not working. Any suggestions on how to make it work? Dja ...

Transferring data from JavaScript to PHP with the help of AJAX

I am encountering issues with my code, as I cannot seem to successfully send coordinates from JavaScript to PHP using AJAX. Although I can retrieve values from JavaScript into a textbox, the values are not being transferred to PHP. Any assistance on resolv ...

What steps should I take to ensure my navigation bar spans across the entire page?

I'm new to this and looking for help. I want my navigation bar to span the entire width of the page without any white spaces on the sides or top. Below is my CSS: nav ul {list-style-type: none; margin: 0; padding: 0; overflow: hidden; backgr ...

Utilize the display property with grid to effortlessly expand a block to occupy the entire width

I am currently exploring the grid system and I have a specific challenge. I would like the third block to expand to its full width without the need for extra classes. Can this be achieved using only internal CSS? .grid { margin: 36px auto; height: ...

How come when you add ({}+{}) it equals to "[object Object][object Object]"?

I ran the following code: {}+{} = NaN; ({}+{}) = "[object Object][object Object]"; What is the reason behind the difference in result when adding ()? ...

Combining TypeScript into HTML resulted in an error: Uncaught ReferenceError clickbutton is not defined

Attempting to create a basic CRUD frontend without the use of any frameworks. I am encountering an issue when trying to include a TypeScript file (index.ts) in my index.html, as the functions called within it are showing as undefined. I understand that bro ...

Choosing the initial tab to display when the page loads from a selection of 3 tabs

How can I set the "All" tab to be selected by default when the page loads, with the text color black and underlined? .publisher-tab { background-color: rgb(255, 255, 255) !important; color: #B3B3B3; font-family: 'Open Sans'; font-style ...

Create a stylish grid layout with perfectly centered content using CSS

I am in the process of developing a layout for print material on sticker labels. To achieve this, I have created a div element and applied the display:grid; property to customize the space allocation. My goal is to make item1, item2, and item3 overlap eac ...