Decrease the z-index of the Bootstrap 4 card to be lower than that of the table

I'm struggling with a Table that has a fixed header. The issue is that the card elements keep scrolling on top of the fixed header. I've attempted to adjust the z-index of thead and decrease the z-index of the card element, but nothing seems to be resolving the problem.

document.getElementById("scrollarea").addEventListener("scroll", function() {
  var translate = "translate(0," + this.scrollTop + "px)";
  $(this).find("thead")[0].style.transform = translate;
});
#scrollarea {
  max-height: 200px;
  overflow: auto;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/js/bootstrap.min.js"></script>
<div id="scrollarea">
  <table class="table table-striped">
    <thead>
      <tr>
        <th>Row 1</th>
        <th>Row 2</th>
        <th>Row 3</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td colspan="3">
          <div class="card">
            <div class="card-body">
              <h5 class="card-title">Card title</h5>
              <p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
            </div>
          </div>
        </td>
      </tr>
      ... <!-- More card elements -->
    </tbody>
  </table>
</div>

I need assistance in ensuring that the thead remains on top when the div is scrolled.

Any help would be greatly appreciated. Thank you.

Answer №1

Experiment with replacing tables with divs, and make sure to include the .sticky-top class on the parent div element.

Answer №2

To achieve the desired effect, adjust the position property on scroll and apply z-index as well:

// Trigger myFunction when the page is scrolled
window.onscroll = function() {myFunction()};

// Select the navbar element
var navbar = document.getElementById("navbar");

// Get the offset position of the navbar
var sticky = navbar.offsetTop;

// Add the "sticky" class to the navbar when it reaches its scroll position. Remove "sticky" when it leaves that position.
function myFunction() {
  if (window.pageYOffset >= sticky) {
    navbar.classList.add("sticky")
  } else {
    navbar.classList.remove("sticky");
  }
}
/* Style the navbar */
#navbar {
  overflow: hidden;
}

/* Apply style changes with the "sticky" class */
.sticky {
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 1;
  background: #FFF;
}

/* Add top padding to prevent sudden jumps in content layout */
.sticky + .tbody {
  padding-top: 60px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/js/bootstrap.min.js"></script>
<div id="scrollarea">
  <table class="table table-striped">
    <thead id="navbar">
      <tr>
        <th>Row 1</th>
        <th>Row 2</th>
        <th>Row 3</th>
      </tr>
    </thead>
    <tbody>
      <!-- Content rows -->
    </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

Respond.js appears to be without functionality

Can anyone lend a hand? I'm feeling incredibly frustrated trying to get respond.js to cooperate. Typically, we have no issues with using it on our mobile sites. However, I am currently facing challenges trying to achieve mobile compatibility on one o ...

Is a persistent left border / divider only on the even-numbered items in the list?

I have a layout of list items arranged in 2 columns, separated by a bottom margin after every 'row' of 2 items. While it's simple to add a left border to every even list item... I am curious if it's achievable to extend the border so ...

Validate the Ajax form upon submission

Is there a way to incorporate code within an AJAX block to validate form fields? Before sending the AJAX request page described below, I need to ensure that the fields for firstname, lastname, and email are filled out. If any of these fields are empty, th ...

The jQuery method serializeArray does not include the values of HTML textareas and checkboxes

Hello, I'm looking to send data to the server from an HTML form. The server I'm using is CodeIgniter and I'm utilizing the AJAX method. Here's the HTML form: <div id="fileupload"> <form id="upload-media-form" class="uploa ...

How can I stop the setinterval function in JavaScript?

My issue revolves around intervals. Upon declaring a function with setInterval, I find that even after clearing the interval, the function continues to execute. Here is my code: if (score == 1) { leftBlinkTimer(0) } else if (score == 0) { leftBlin ...

The links located beneath the iframe/span element are unclickable

My latest creation is a static advert ticker positioned at the bottom of the window. It's contained within an <iframe> for easy placement on other websites. I added a <span> around the <iframe> to keep it fixed at the bottom of the s ...

locate and interact with a dynamically created button using JavaScript through ajax

Hey there, I'm trying to display an image along with a button using AJAX whenever a visitor uploads an image. for (var i = 0; i < data.length; i = i + 1) { imageThump += '<img src="' + data[i].path + '" />'; image ...

"Node.js and Webpack - the dynamic duo of

Have you ever wondered why we need to use webpack and npm to install node for JavaScript, when we can simply run JavaScript code directly in our browser? And how exactly do we go about implementing webpack into our project files? ...

A guide on verifying discrepancies between selected record data in two tables

How can I validate if the data of selected records is mismatching in two tables? I need to select a few records in each table. Upon clicking the Match button, if the currencies are not the same, we need to display an alert saying "Data mismatched". I have ...

Spacing between letters on a canvas element

This question is straightforward - I am struggling to find a way to adjust the letter spacing when drawing text on a canvas element. I want to achieve a similar effect as the CSS letter-spacing attribute, by increasing the space between each letter in the ...

How to modify a variable in the Config.json using a Discord.js command

Lately, I enhanced my bot's functionality by allowing it to retrieve the color for embeds from a file specified in my config.json. All I need to do is modify something like: "embedcolor": "00A950" to "embedcolor": "0 ...

Revolutionize iPad user experience with seamless HTML5 video integration

What is the most effective method for dynamically embedding HTML5 video to ensure compatibility with the iPad? (using pure Javascript) I'm having difficulty getting this approach to work: <div id="placeholder"><script type="text/javascript" ...

Safari on iOS 11.4 ignoring the 'touch-action: manipulation' property

I ran into an issue while developing a React web app that I want to work seamlessly across different platforms. My goal was to allow users to interact with a div element by double-clicking on desktop and double-tapping on mobile devices. However, when tes ...

Testing a service resolution in a controller through unit testing

As I attempt to create a test for my home module, I keep encountering an error that reads "unknown provider: service." Interestingly, when I modify resolveSomething in my home module to output a string, the app functions as expected, indicating that the re ...

The first modal becomes jammed upon closing the second modal

I'm in need of some assistance, as I am facing an issue that I can't seem to figure out. It's baffling me why my first modal window gets stuck after I close my second modal window. The setup involves a button that opens a "login" modal, whic ...

What is the best way to ensure uniform container sizes and properly align images within them?

Customize Image Sizes: Is there a way to set a standard container size and adjust image sizes properly without compromising clarity or aesthetics? The images vary in dimensions, so should they be standardized or is there another solution? Code snippet: ...

What could be causing the sudden spikes to 100% on the node server that result in crashes? How can

As I work on developing an online browser game using websockets and a node server, I have noticed that with 20-30 players, the CPU hovers around 2% and RAM at 10-15%. All of this is being hosted on a budget-friendly Digital Ocean droplet. However, every 2 ...

Ways to combine multiple CSS styles for an element using .css()

I'm struggling to apply styles to a deeply nested element using jQuery and JavaScript. I understand how to style nested elements, but I'm having trouble targeting a specific deeply nested element. My website is built on WordPress, and it has ins ...

Tips on how to properly format a date retrieved from a database using the JavaScript function new Date()

I've been grappling with the best method for inserting dates into the database, and currently I'm utilizing new Date(). However, when I query from the database, it returns a date format like this: 2021-09-24T12:38:54.656Z It struck me that this ...

Update the style of elements on each iteration in Symfony2

I have implemented a collapsible CSS page for FAQs. While static text works fine, I am fetching questions and answers from the database which is causing an issue. In its standard CSS version, the collapsible FAQ appears as follows: <div clas ...