Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid page break between <thead> and <tbody>

I'm trying to make a printable document with some quite long tables. And sometimes page ends right between table headers and the data, which makes it harder to read.

Example

How could I prevent that?

I've tried to use the following CSS but it didn't help.

@media print {
        h1, h2, h3, thead, thead>tr {
            page-break-after: avoid;
            page-break-inside: avoid;
        }

        tbody {
            page-break-before: avoid;
        }

        /* I would also like to not have page breaks within first five rows */
        tbody:nth-child(-n+5) {    
            page-break-before: avoid;
        }
}

Table structure:

<table border="1" cellspacing="0" cellpadding="3">
    <thead>
    <tr>
        <th rowspan="2">Metric</th>
        <th colspan="3">Type 1</th>
        <th colspan="3">Type 2<th>
    </tr>
    <tr>
        <th>Initial</th>
        <th>Final</th>
        <th>Difference</th>
        <th>Initial</th>
        <th>Final</th>
        <th>Difference</th>
    </tr>
    </thead>
    <tbody>
    <tr>
        <td>Dataset1</td>
        <td>*DATA*</td>
        ...
    </tr>
    </tbody>
</table>
like image 796
Drath Vedro Avatar asked Nov 26 '22 14:11

Drath Vedro


1 Answers

I found a workaround for this issue, based on the solution suggested here: How do I avoid a page break immediately after a heading

Add a wrapper to your table and add a before pseudo-element to it. This element won't actually take up any space (due to the negative margin-bottom), but its height will be used when calculating where to put the page break, forcing the browser to push the table to the next page if it's too close to the bottom.

200px should be replaced with a value that is slightly more than the height of the header + the height of the first row of the body.

/* This is the solution */
.wrapper::before {
    content: "";
    display: block;
    height: 200px;
    margin-bottom: -200px;
    page-break-inside: avoid;
    break-inside: avoid;
}

/* Table styles */
table {
    width: 100%;
}

thead {
    background: green;
}

thead tr {
    page-break-inside: avoid;
    break-inside: avoid;
}

tbody {
    background: yellow;
}

tbody tr {
    height: 80px;
}

td {
    height: 80px;
}
<div class="wrapper">
    <table>
        <thead>
            <tr>
                <td>header</td>
                <td>header</td>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1</td>
                <td>2</td>
            </tr>
            <tr>
                <td>1</td>
                <td>2</td>
            </tr>
            <tr>
                <td>1</td>
                <td>2</td>
            </tr>
            <tr>
                <td>1</td>
                <td>2</td>
            </tr>
            <tr>
                <td>1</td>
                <td>2</td>
            </tr>
        </tbody>
    </table>
</div>
like image 168
UnShame Avatar answered Dec 05 '22 01:12

UnShame