最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How do I stop my headers from disappearing when scrolling through the table? - Stack Overflow

programmeradmin1浏览0评论

I have the following html code for my table

  <div class="table-responsive">
        <table class="table table-striped table-hover">
            <thead class="thead-dark">
                <tr>
                    <th>Expense Type</th>
                    <th>Description</th>
                    <th>Price</th>
                    <th>Entry Date</th>
                    <th>Next Monitoring Date</th>
                </tr>
            </thead>

The associate css is shown below for the table above I have already attempted position sticky on the values however, this does not seem to work?

.table-container {
    padding: 20px;
    flex: 1;
    overflow-y: auto;
}

.table {
    background-color: #FFFFFF;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    margin-bottom: 0;
}

.table-responsive {
    max-height: 650px;
}

th styling where I have attempted to use position sticky however, this has not worked?

.table thead th {
    position: sticky;
    top: 0;
    background-color: #F3F4F6; /* Matches the table container background */
    z-index: 10; /* Ensures the header is above table rows */
    border-bottom: 2px solid #E5E7EB;
    text-align: center;
    font-weight: bold;
    color: #374151;
}

.table tbody tr:hover {
    background-color: #F9FAFB;
    cursor: pointer;
}

Styling for the table body td

.table tbody td {
    vertical-align: middle;
    text-align: center;
}

I have the following html code for my table

  <div class="table-responsive">
        <table class="table table-striped table-hover">
            <thead class="thead-dark">
                <tr>
                    <th>Expense Type</th>
                    <th>Description</th>
                    <th>Price</th>
                    <th>Entry Date</th>
                    <th>Next Monitoring Date</th>
                </tr>
            </thead>

The associate css is shown below for the table above I have already attempted position sticky on the values however, this does not seem to work?

.table-container {
    padding: 20px;
    flex: 1;
    overflow-y: auto;
}

.table {
    background-color: #FFFFFF;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    margin-bottom: 0;
}

.table-responsive {
    max-height: 650px;
}

th styling where I have attempted to use position sticky however, this has not worked?

.table thead th {
    position: sticky;
    top: 0;
    background-color: #F3F4F6; /* Matches the table container background */
    z-index: 10; /* Ensures the header is above table rows */
    border-bottom: 2px solid #E5E7EB;
    text-align: center;
    font-weight: bold;
    color: #374151;
}

.table tbody tr:hover {
    background-color: #F9FAFB;
    cursor: pointer;
}

Styling for the table body td

.table tbody td {
    vertical-align: middle;
    text-align: center;
}
Share Improve this question asked Jan 19 at 21:33 foodsrfoodsr 114 bronze badges 2
  • You may need to provide a more complete example in a snippet. I just tested your code and the header sticks to the top while scrolling as expected. – abney317 Commented Jan 20 at 3:48
  • Hi @abney317 thanks for confirming I have identified the root cause being: .table { background-color: #FFFFFF; border-radius: 8px; overflow: hidden; box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); margin-bottom: 0; } specifically overflow: hidden; – foodsr Commented Jan 20 at 8:50
Add a comment  | 

1 Answer 1

Reset to default 0

I have identified the root cause being:

.table {
    background-color: #FFFFFF;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
    margin-bottom: 0;
}

specifically overflow: hidden;

发布评论

评论列表(0)

  1. 暂无评论