I have a Table with a fixed header as below. The problem is that the card elements scroll on top of the fixed header. I have tried increasing z-index
of thead
and decreasing z-index
of the card element. Nothing seems to be working.
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>
<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>
<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>
<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>
<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>
<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>
<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>
</tbody>
</table>
</div>
I want to make the thead
be on top when the div
is scrolled.
Any kind of help is much appreciated. Thank you.
2
Answers
You should change the position property on scroll and also use z-index:
try changing table to div and add .sticky-top to the parent div.