Home  >  Article  >  Web Front-end  >  How to Create a Fixed Header Table Inside a Scrollable Div?

How to Create a Fixed Header Table Inside a Scrollable Div?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-17 01:58:03133browse

How to Create a Fixed Header Table Inside a Scrollable Div?

How to Create a Fixed Header Table Inside a Scrollable Div

To make a header fixed while scrolling, it's recommended to use two tables. One for the header, which remains static, and another for the content cells, wrapped in a fixed-height div with overflow enabled.

HTML:

<div class="wrap">
    <table class="head">
        <thead>
            <tr>
                <th>Head 1</th>
                <th>Head 2</th>
                <th>Head 3</th>
            </tr>
        </thead>
    </table>
    <div class="inner_table">
        <table>
        <tbody>
            <tr>
                <td>Body 1</td>
                <td>Body 2</td>
                <td>Body 3</td>
            </tr>
            <!-- More table rows here -->
        </tbody>
    </table>
    </div>
</div>

CSS:

.wrap {
    width: 352px;
}

.wrap table {
    width: 300px;
    table-layout: fixed;
}

table tr td {
    padding: 5px;
    border: 1px solid #eee;
    width: 100px;
    word-wrap: break-word;
}

table.head tr td {
    background: #eee;
}

.inner_table {
    height: 100px;
    overflow-y: auto;
}

This approach ensures a static header when the table's content is scrolled, preventing the content from obscuring the important column names.

The above is the detailed content of How to Create a Fixed Header Table Inside a Scrollable Div?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn