x
 
1
<!DOCTYPE html>
2
<title>My Example</title>
3
4
<style>
5
/* --- This is the most important part of the setup --- */
6
/* 1. The container MUST have a defined height and overflow property. */
7
.sticky-header-container {
8
    height: 400px; /* Example height. Adjust as needed. */
9
    overflow-y: auto; /* Enable vertical scrolling */
10
    border: 1px solid #ccc;
11
    border-radius: 8px;
12
}
13
14
.sticky-header-table {
15
    width: 100%;
16
    border-collapse: collapse;
17
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
18
    /* This allows the th to stick relative to the table */
19
    position: relative;
20
}
21
22
/* 2. The header cells (th) are what become sticky */
23
.sticky-header-table thead th {
24
    position: -webkit-sticky; /* For Safari */
25
    position: sticky;
26
    top: 0; /* Stick to the top of the container */
27
    z-index: 10; /* Ensure the header stays on top of the scrolling content */
28
29
    /* -- Standard header styling -- */
30
    padding: 12px 15px;
31
    background-color: #ffffff; /* Give header a solid background to hide content scrolling underneath */
32
    border-bottom: 2px solid #dee2e6;
33
    text-align: left;
34
    font-weight: 600;
35
}
36
37
.sticky-header-table tbody td {
38
    padding: 12px 15px;
39
    border-bottom: 1px solid #e9ecef;
40
}
41
42
.sticky-header-table tbody tr:nth-of-type(even) {
43
    background-color: #f8f9fa;
44
}
45
46
</style>
47
48
<!-- 
49
    INSTRUCTIONS: To see the effect, scroll vertically inside this table. 
50
    The header will remain at the top of this container.
51
-->
52
<div class="sticky-header-container">
53
    <table class="sticky-header-table">
54
        <thead>
55
            <tr>
56
                <th scope="col">Log ID</th>
57
                <th scope="col">Timestamp</th>
58
                <th scope="col">Level</th>
59
                <th scope="col">Message</th>
60
            </tr>
61
        </thead>
62
        <tbody>
63
            <tr><td>1001</td><td>2024-08-22 11:00:01</td><td>INFO</td><td>User logged in.</td></tr>
64
            <tr><td>1002</td><td>2024-08-22 11:00:05</td><td>INFO</td><td>Data sync started.</td></tr>
65
            <tr><td>1003</td><td>2024-08-22 11:00:15</td><td>DEBUG</td><td>Payload received from API.</td></tr>
66
            <tr><td>1004</td><td>2024-08-22 11:00:25</td><td>WARN</td><td>API response time > 500ms.</td></tr>
67
            <tr><td>1005</td><td>2024-08-22 11:00:30</td><td>INFO</td><td>Data sync completed.</td></tr>
68
            <tr><td>1006</td><td>2024-08-22 11:01:00</td><td>ERROR</td><td>Failed to connect to database.</td></tr>
69
            <tr><td>1007</td><td>2024-08-22 11:01:05</td><td>INFO</td><td>Retrying database connection...</td></tr>
70
            <tr><td>1008</td><td>2024-08-22 11:01:10</td><td>INFO</td><td>Database connection successful.</td></tr>
71
            <tr><td>1009</td><td>2024-08-22 11:02:00</td><td>INFO</td><td>Processing batch job #123.</td></tr>
72
            <tr><td>1010</td><td>2024-08-22 11:02:30</td><td>INFO</td><td>Batch job #123 complete.</td></tr>
73
            <tr><td>1011</td><td>2024-08-22 11:03:00</td><td>DEBUG</td><td>Checking for new tasks.</td></tr>
74
            <tr><td>1012</td><td>2024-08-22 11:04:00</td><td>INFO</td><td>No new tasks found. Idling.</td></tr>
75
            <tr><td>1013</td><td>2024-08-22 11:05:00</td><td>INFO</td><td>User logged out.</td></tr>
76
            <!-- Add more rows to ensure vertical scrolling -->
77
            <tr><td>1014</td><td>2024-08-22 11:05:10</td><td>INFO</td><td>Session terminated.</td></tr>
78
            <tr><td>1015</td><td>2024-08-22 11:05:20</td><td>DEBUG</td><td>Cleanup complete.</td></tr>
79
        </tbody>
80
    </table>
81
</div>