Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying "page-break-before" to a table row (tr)

Tags:

css

page-break

According to W3.org, the style page-break-after applies to block level elements (http://www.w3.org/TR/2004/CR-CSS21-20040225/page.html#page-break-props)

<tr> is a block level element (according to this: http://www.htmlhelp.com/reference/html40/block.html, it is)

I'm doing this, but the page break is not creating an actual page break when printing:

  <table>
    <tr><td>blah</td></tr>
    <tr><td>blah</td></tr>
    <tr style="page-break-after: always"><td>blah</td></tr>
    <tr><td>blah</td></tr>
  </table>

Am I doing this the correct way?

If <tr> wasn't a block level element: how am I suppose to achieve this page break?

Note: the before code is just an example, but what I'm trying to do is to put a page-break every 5 rows of the table, so if you know any tips for that case, will be appreciated

like image 209
sports Avatar asked Dec 09 '13 21:12

sports


People also ask

How do I prevent page breaks within a table row?

Make sure all parent elements are display: block . Also consider overriding table , tr , td 's display styles with CSS grid for the print layout if you keep having issues with the table.

What is insert page-break before?

What are Page Breaks? Insert a page break when you want to move to the beginning of the next page in your document. Many users, unaware of the page break feature, will simply mash the Enter key when they want to start a new page. That works too – until you want to edit your document later.

How does page-break before work?

Page breaks are avoided before the element. Page breaks are forced before the element resulting in the browser formatting the next page as a left page. Page breaks are forced before the element resulting in the browser formatting the next page as a right page.


2 Answers

Inside <head>, set this style in your CSS stylesheet

<head>
    <style>
        @media print {
            tr.page-break  { display: block; page-break-before: always; }
        }   
    </style>
</head>

That way, it will produce a page break during printing right before this table row.

<tr class="page-break">
</tr>
like image 138
guest Avatar answered Sep 29 '22 01:09

guest


The site you referenced states that <tr> "may also be considered a block-level element since it may contain block-level elements." Neither the W3.org or Mozilla docs state that <tr> is a block-level element.

Some Possible Solutions

  1. Based on the wording and your example, I would ensure that the cell contains a true block-level element. Here are two examples using <h1> and <p> which are block-level text elements.

    <tr style="page-break-after: always"><td><h1>Next Section</h1></td></tr>
    <tr style="page-break-after: always"><td><p>This will be a new page.</p></td></tr>
    
  2. Others have reported similar problems and one of the solutions might work for you.

    • How to apply CSS page-break to print a table with lots of rows?
    • Google Chrome Printing Page Breaks
    • Printing a Gridview - how to print n rows on each page using page break
    • page-break-inside doesn't work in Chrome?
  3. As mentioned by My Lister, you could attempt to catch the printing action or generate a print version of the page that would separate the table out so you can obtain the desired page break after every five rows.

like image 25
JSuar Avatar answered Sep 29 '22 00:09

JSuar