1

I have several tables on one page, and when I open the print preview in a browser, I've got some issue as on the screenshot. It happens when one of the tables is not fit to a paper. Is there a valid CSS way to prevent this problem.

Html page to reproduce behavior:

<html>
<head>
<title>Page Title</title>
<style>

table {
        border-collapse: collapse;
        margin-bottom: 30px;
}

th, td {
    border: 1px solid #777;
}
</style>
</head>
<body>
    <table>
        <thead>
            <tr>
                <th>Header cell</th>
                <th>Header cell</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Body cell</td>
                <td>Body cell</td>
            </tr>
            <tr>
                <td>Body cell</td>
                <td>Body cell</td>
            </tr>
            <tr>
                <td>Body cell</td>
                <td>Body cell</td>
            </tr>
            <tr>
                <td>Body cell</td>
                <td>Body cell</td>
            </tr>
        </tbody>
    </table>
        <table>
        <thead>
            <tr>
                <th>Header cell</th>
                <th>Header cell</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>Body cell</td>
                <td>Body cell</td>
            </tr>
            <tr>
                <td>Body cell</td>
                <td>Body cell</td>
            </tr>
            <tr>
                <td>Body cell</td>
                <td>Body cell</td>
            </tr>
            <tr>
                <td>Body cell</td>
                <td>Body cell</td>
            </tr>
        </tbody>
    </table>
</body>
</html>

Screenshot with issue

1 Answers1

1

Use the page-break-inside property, it sets whether a page-break should be avoided inside a specified element.

<style>
@media print {
   table {page-break-inside: avoid;}
}
</style>
Mike V.
  • 2,077
  • 8
  • 19