Search code examples
cssgoogle-chromeprintingmedia-queries

Chrome (v51) - Media query to insert page breaks between table rows - landscape only


I have a large HTML table that cannot be modified and I'd like to put page breaks after each row when printed, if the orientation is landscape only.

I created a little test page to pursue a solution.

This code works as expected under Firefox 46.0, but not at all under Chrome 51.

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CSS print media query test</title>
<style>
body {
    font-family: arial,sans;
}
table {
    margin: 0 auto;
}
table tr {
    height: 500px;
    vertical-align: top;
}
table td {
    border: 1px solid #23ffff;
    background-color: #edffff;
    padding: 15px;
    cell-spacing: 5px;
}
@media print and (orientation:landscape) {
    table tr {
        page-break-after: always !important;
    }
}
</style>
</head>
<body>
<table>
<tr>
<td>$i is 1 and $j is 1</td>
<td>$i is 1 and $j is 2</td>
<td>$i is 1 and $j is 3</td>
<td>$i is 1 and $j is 4</td>
</tr>
<tr>
<td>$i is 2 and $j is 1</td>
<td>$i is 2 and $j is 2</td>
<td>$i is 2 and $j is 3</td>
<td>$i is 2 and $j is 4</td>
</tr>
<tr>
<td>$i is 3 and $j is 1</td>
<td>$i is 3 and $j is 2</td>
<td>$i is 3 and $j is 3</td>
<td>$i is 3 and $j is 4</td>
</tr>
<tr>
<td>$i is 4 and $j is 1</td>
<td>$i is 4 and $j is 2</td>
<td>$i is 4 and $j is 3</td>
<td>$i is 4 and $j is 4</td>
</tr>
</table>
</body>
</html>

This is a test page, I don't normally put the CSS in tags.


Solution

  • CSS page break attributes only work on block-level elements. Add display: block and adjust spacing if necessary.

    @media print and (orientation:landscape) {
        table tr {
            display: block;
            page-break-after: always;
        }
    }