Sticking Table Headers in Bootstrap 4

I'm struggling with making the header of my Bootstrap 4 table fixed/sticky so that it doesn't scroll along with the content. I've tried adding custom CSS but haven't been successful so far.

Here's a snippet of the code for my table:

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table class="table table-striped table-hover table-bordered">
    <thead>
        <th scope="col">Name</th>
        <th scope="col">Title</th>
        <th scope="col">Code</th>
    </thead>
        <tbody>

            <tr id="211140">
                <td><a href="view.php?action=viewDetails&ID=PM5281"> Jaxon Copeley</a></td>
                <td>Senior Designer</td>
                <td>8</td>
            </tr>
            [additional rows from original text]
        </tbody>
</table>

Answer №1

You can easily implement the position: sticky css rule to the th as shown in the example below:

Make sure to also set the top property correctly to prevent any design issues.

th {
  background: white;
  position: sticky;
  top: 0; /* Make sure to include this for proper stickiness */
  box-shadow: 0 2px 2px -1px rgba(0, 0, 0, 0.4);
}
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table class="table table-striped table-hover table-bordered">
    <thead>
        <th scope="col">Name</th>
        <th scope="col">Title</th>
        <th scope="col">Code</th>
    </thead>
    <tbody>
        <tr id="211140">
            <td><a href="staffDetails.php?action=staffLink&contactID=CT5281"> Jaxon Copeley</a></td>
            <td>Senior Designer</td>
            <td>8</td>
        </tr>
        <tr id="212265">
            <td><a href="staffDetails.php?action=staffLink&contactID=CT4102"> Sean Dacey</a></td>
            <td>Associate</td>
            <td>1687</td>
        </tr>
        <tr id="212364">
            <td><a href="staffDetails.php?action=staffLink&contactID=CT5448"> Nathan Giffen</a></td>
            <td>Senior Designer</td>
            <td>273.8</td>
        </tr>
        <tr id="212312">
            <td><a href="staffDetails.php?action=staffLink&contactID=CT6256"> Tristan Godson</a></td>
            <td>Project Designer </td>
            <td>85.75</td>
        </tr>
        <tr id="207542">
            <td><a href="staffDetails.php?action=staffLink&contactID=CT6123"> Anthony McAulay</a></td>
            <td>Designer</td>
            <td>566.2</td>
        </tr>
        <tr id="207990">
            <td><a href="staffDetails.php?action=staffLink&contactID=CT5466"> Gabriella Schofield</a></td>
            <td>Senior Designer</td>
            <td>107</td>
        </tr>
        <tr id="213479">
            <td><a href="staffDetails.php?action=staffLink&contactID=CT6513"> Hayden Giblin</a></td>
            <td>Senior Associate Creative Designer - Head Of Graphic Design</td>
            <td>60</td>
        </tr>
        <tr id="208423">
            <td><a href="staffDetails.php?action=staffLink&contactID=CT5313"> Archer Doolan</a></td>
            <td>Associate</td>
            <td>487.9</td>
        </tr>
        <tr id="208468">
            <td><a href="staffDetails.php?action=staffLink&contactID=CT4330"> Taylah Hutcheon</a></td>
            <td>Senior Associate</td>
            <td>5</td>
        </tr>
        <tr id="212645">
            <td><a href="staffDetails.php?action=staffLink&contactID=CT0038"> Hayley Hodgson</a></td>
            <td>Director</td>
            <td>37</td>
        </tr>
        <tr id="214303">
            <td><a href="staffDetails.php?action=staffLink&contactID=CT4237"> Henry Powell</a></td>
            <td></td>
            <td>9</td>
        </tr>
    </tbody>
</table>

Answer №2

Initially, your thead must have stickiness in mind, and sticky positioning will only take effect once you include either top or bottom

Assign a class name to your thead, such as my-thead

Implement the style like this

.my-thead{
    position: sticky;
    top: 0;
    background: white;
}

Answer №3

To achieve a sticky header for your table, you can use the following CSS:

.header {
position: sticky;
top: 0;
}
thead th {
background-color: #666;
color: white;
}

Make sure to include the necessary Bootstrap and jQuery libraries in your HTML:

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>

Then, apply the "header" class to the <thead> tag of your table:

<table class="table table-striped table-hover table-bordered">
    <thead class="header">
        <th class="header" scope="col">Name</th>
        <th class="header" scope="col">Title</th>
        <th class="header" scope="col">Code</th>
    </thead>
    <tbody>
        <tr>
            <td>John Doe</td>
            <td>Developer</td>
            <td>12345</td>
        </tr>
        <tr>
            <td>Jane Smith</td>
            <td>Designer</td>
            <td>54321</td>
        </tr>
    </tbody>
</table>

Answer №4

To keep your table headers in place, use the CSS property position: sticky; top: 0; on your th elements.

.tableFixHead          { overflow: auto; height: 100px; }
.tableFixHead thead th { position: sticky; top: 0; z-index: 1; }

/* Standard styling for tables */
table  { border-collapse: collapse; width: 100%; }
th, td { padding: 8px 16px; }
th     { background:#eee; }
<div class="tableFixHead">
  <table>
    <thead>
      <tr><th>TH 1</th><th>TH 2</th></tr>
    </thead>
    <tbody>
      <tr><td>A1</td><td>A2</td></tr>
      <tr><td>B1</td><td>B2</td></tr>
      <tr><td>C1</td><td>C2</td></tr>
      <tr><td>D1</td><td>D2</td></tr>
      <tr><td>E1</td><td>E2</td></tr>
    </tbody>
  </table>
</div>

Answer №5

If you are utilizing a Bootstrap 4 Table, you can take advantage of Bootstrap's pre-existing CSS that includes a .sticky-top predefined class. This means there is no need to create a new class in your own CSS for the desired effect; all you have to do is incorporate the existing Bootstrap class into your code:

<!-- [your own code here, with Bootstrap's CSS as well] -->

<table class="table table-striped table-hover table-bordered">
  <thead>
    <th class="sticky-top" scope="col">Name</th>
    <th class="sticky-top" scope="col">Title</th>
    <th class="sticky-top" scope="col">Code</th>
  </thead>
  <tbody>

  <!-- [additional code goes here] -->

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

Flashing white when transitioning background images

My website features a div with a blurred image that I want to gradually unblur upon hovering, using transition-property and transition-duration. I have two versions of the image prepared - one blurred and one unblurred. However, when viewing online (desp ...

Removing the Yellow Highlight on Input Field Following Email Autocomplete in Chrome

My username-password form is styled and working perfectly, but there's an issue that arises when I log in multiple times. Chrome automatically fills in my email, turning the username textbox yellow. It doesn't seem to happen with Firefox or Safar ...

Learn how to efficiently import scripts and styles from WordPress plugins with the help of the wp_enqueue_script function

After creating my own custom Wordpress theme, everything seemed to be running smoothly. However, I soon encountered an issue where certain plugins were not functioning properly with the theme. It seems that the theme is unable to import the necessary style ...

Having a Jquery resizing problem? No worries! When the width is less than 768, simply enable the click option. And when the width is

HTML <div class="profile"> <a href="#" class="hoverdropdown" onclick="return false;">Profile</a> <ul class="dropdown" style="display: none;"> <li><a href="#">Dashboard&l ...

Is it possible to establish a connection between Firebase Storage and HTML using TypeScript without Angular or React in IntelliJ?

My project goal is to create a functional login and register page using TypeScript. Currently, my code operates without a database, but I aim to implement Firebase for registering user credentials for easy login. I have only come across tutorials using F ...

Designing HTML columns to adapt to different screen resolutions

Looking for a clever jQuery or CSS trick to dynamically adjust the number of columns in a website layout based on the size of the viewport. For instance, an iPhone display would have 1 column, while an 800x600 screen would have 2, and a 1024x768 screen wou ...

Adjusting image size as you scroll

Hello everyone, I am currently working on coding a website logo that decreases in size as the user scrolls down, similar to the effect seen on this website: . I am relatively new to HTML and was wondering if there are any specific commands or techniques th ...

Developing a custom mixin to alert a banner at the top of the webpage

I currently have a feature on my website that triggers a pop-up notification whenever a message is received from another user. The pop-up consists of a div displaying the content of the message along with a close button. After exploring various methods to ...

Datetimepicker initialization with default start date

I need to disable all dates for the specific date stored in a session. My attempt: The session contains the following date format: dd-mm-yyyy $_SESSION['beschikbaar']; This is the input field <input class="form-control" type="text" place ...

Ways to automatically insert an icon within a textarea

While attempting to insert an image inside a textarea, I discovered that it seems impossible without using the contenteditable ="true" attribute of a textarea. However, upon visiting the diaspora site, I found out that this can indeed be achieved. I am uns ...

Is there a way to move the bootstrap carousel item captions outside of the image without having to set a specific height for the carousel manually?

<div id="carouselExampleIndicators" className="carousel slide" data-bs-ride="true"> <div className="carousel-indicators"> <button type="button" data-bs-target="#carouselExampleIndicators" data-bs-slide-to="0" className="active" ...

Ensuring XHTML W3C Compliance with multiple details in src attribute

We are currently working on a unique image carousel feature for our website. This carousel will display clickable thumbnails that, when clicked, will show the full-size image. In order to make this work, we need to include both URLs in the HTML code. Howev ...

JavaScript: table is not defined

When creating a table using Django models and JavaScript, I encountered an issue where the cells values of the table could not be accessed from another JavaScript function. The error message indicated that the table was "undefined". Below is the HTML code ...

Displaying the value of a jquery variable in an HTML document

I'm tackling a problem differently today compared to yesterday, but my knowledge of jQuery and JavaScript is quite basic. My goal is to increment the transform value of a div every 5 seconds: <div style="transform: translateX(0px);" id="slide_ima ...

Navigating between pages using the ExpressJS and Angular 1 routing system

Can someone help me troubleshoot an issue I'm having with my Express API and Angular front-end? Whenever I try to access the /about route, it keeps defaulting back to index.html and displaying a 404 error message. Can you take a look at my code and pi ...

The process of manipulating the content of an HTML textarea using Selenium with Python

http://neomx.iwedding.co.kr/roundcube I attempted to change the content of a textarea, specifically the tracking number. My goal was to replace "9400111206213835724810" with "487289527385922090". However, I encountered an error message stating "ElementNot ...

Cross-Browser Compatibility of CSS

I have noticed that lists styled with the following CSS code appear differently in Internet Explorer 8 and Firefox 4. In IE8, the rounded corners are missing, while FF4 does not change color when hovering over it. Which browser should I trust for accurate ...

displaying the description of a Wordpress category

How can I display the Wordpress category description and name separately in two different locations? I want to show just the description without including the category name or slug. Currently, I have been using the following code snippet to display only t ...

Manipulating contextual selectors

Have you ever tried reverting or overriding attributes from contextual selectors? .card { padding-top: 20px; ... } .card .card-header { padding: 0 20px; } .card .card-header.news { padding-top: 0px; } Do you think it's possible to elimina ...

How to perfectly position an image within a fixed full screen container

When you click on a thumbnail image, a full-screen overlay with a larger version of the image will be triggered using JavaScript. To ensure that the image is centered and resized inside the black overlay when the browser window size changes, I attempted t ...