Home > Web Front-end > CSS Tutorial > How Can I Prevent HTML Table Rows from Splitting Across Pages When Printing?

How Can I Prevent HTML Table Rows from Splitting Across Pages When Printing?

Patricia Arquette
Release: 2024-12-14 22:56:14
Original
432 people have browsed it

How Can I Prevent HTML Table Rows from Splitting Across Pages When Printing?

Addressing Page Breaks in HTML Table Printing

When printing extensive HTML tables, managing page breaks is crucial to maintain readability. To prevent rows from being split across pages, explore the following solution:

You can employ CSS styles to control page breaks in your HTML table:

table { page-break-inside:auto }
tr    { page-break-inside:avoid; page-break-after:auto }
thead { display:table-header-group }
tfoot { display:table-footer-group }
Copy after login

Here's an example HTML table with these CSS styles applied:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Test</title>
<style type="text/css">
    table { page-break-inside:auto }
    tr    { page-break-inside:avoid; page-break-after:auto }
    thead { display:table-header-group }
    tfoot { display:table-footer-group }
</style>
</head>
<body>
    <table>
        <thead>
            <tr><th>heading</th></tr>
        </thead>
        <tfoot>
            <tr><td>notes</td></tr>
        </tfoot>
        <tbody>
        <tr>
            <td>x</td>
        </tr>
        <tr>
            <td>x</td>
        </tr>
        <!-- 500 more rows -->
        <tr>
            <td>x</td>
        </tr>
    </tbody>
    </table>
</body>
</html>
Copy after login

Implementing these CSS rules ensures that:

  • page-break-inside:avoid on tr elements prevents row breaks within a page.
  • page-break-after:auto on tr elements allows a page break after each row, ensuring they are not split.
  • display:table-header-group and display:table-footer-group on thead and tfoot elements keep the table header and footer on their respective pages.

The above is the detailed content of How Can I Prevent HTML Table Rows from Splitting Across Pages When Printing?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template