There isn’t an actual page-break
property in CSS. It is actually a set of 3 properties: page-break-before
, page-break-after
and page-break-inside
. These properties help define how the document is supposed to behave when printed. For example, to make a printed document more book-like.
Properties
page-break-before
The page-break-before
property adds a page-break before the element to which it is applied.
Note: this property is in progress of being replaced by the more generic break-before
property. This new property also handles column and region breaks while being syntactically compatible with page-break-before
. Thus before using page-break-before
, check if you can use break-before
instead.
A common use case for this is to apply it to the selector #comments
so a user printing a page with comments could easily choose to print the whole document but stop before the comments cleanly.
page-break-after
The page-break-after
property adds a page-break after the element to which it is applied.
Note: this property is in progress of being replaced by the more generic break-after
property. This new property also handles column and region breaks while being syntactically compatible with page-break-after
. Thus before using page-break-after
, check if you can use break-after
instead.
page-break-inside
The page-break-inside
property adds a page-break inside the element to which it is applied.
Syntax
page-break-after : auto | always | avoid | left | right
page-break-before : auto | always | avoid | left | right
page-break-inside : auto | avoid
The left
and right
values for page-break-before
and page-break-after
refer to a spread layout (like a book) where there are distinct left and right pages. They work like this:
left
forces one or two page breaks after the element so that the next page is formatted as a left page.right
forces one or two page breaks after the element so that the next page is formatted as a right page.
Consider always
as a mix of both. The specification says:
A conforming user agent may interpret the values ‘left’ and ‘right’ as ‘always’.
Example
@media print {
h2 {
page-break-before: always;
}
h3, h4 {
page-break-after: avoid;
}
pre, blockquote {
page-break-inside: avoid;
}
}
This code snippet does 3 things:
- it forces a page-break before all
h2
headings (perhaps h2 tags in your document are chapter titles that deserve a fresh page) - it prevents page-breaks right after sub-headings because that looks odd
- it prevents page-breaks inside
pre
tags and block-level quotes
Related properties
Other resources
- page related properties in the CSS specifications
- break-before and page-break-before at MDN
- break-after and page-break-after at MDN
- page-break-inside at MDN
Browser Support
Chrome | Safari | Firefox | Opera | IE | Android | iOS |
---|---|---|---|---|---|---|
Any | Any | Any | 7+ | 4+ | TBD | TBD |
You can print from mobile devices, like AirPrint on iOS, but we haven’t tested this much. If anyone has data on support, let us know.
Awesome. I’ve been wanting to do something like this for a while. Never liked breaking in the middle of particular sections, especially when it orphans data on the next page!
Very helpful! Thanks!
you are awesome, thanks
I don’t see “page-break-inside” working on any browser, let alone most major browsers, would you please provide a demo page?
Thank you!
Safari 7 on OS X sadly doesn’t appear to be supporting
page-break-after: avoid
. I’ve tested using a header with paragraph, but they got separated at page boundary.Something has changed. page-break-before: always; no longer works in Chrome or Safari. Firefox respects it though.
page-break-inside:avoid;
This works. in Chrome, Firefox and Safari.
Not only does page-break-inside:avoid; work, but it seems the most useful of the options!
I’ve seen the “auto” option listed several places, but no one ever explains what it’s supposed to do.
page-break-inside:avoid is a lifesaver!
Hi,
I wonder if it would be possible to decorate the page breaks as follows:
Say I have a paragraph that I want to avoid page-break within. So, I set
page-break-inside : avoid
as you mentioned. But, for some devices, it’s not possible to avoid the break. Would it be possible, in that case use some sort of decoration to indicate that the page-break happened within such a paragraph? Say, if the break happens, I want to add a right aligned ➜ to indicate the continuation of the paragraph.Para Chrome recomiendo utilizar:
-webkit-column-break-after
-webkit-column-break-after also not working in chrome
-webkit-column-break-after does not exist for Chrome.
Very awesome, very helpful – thank you!
I have a table with many columns and need to export it to pdf. I want to the table column wise. So, that, first page displays first 4 cols, then next page next 4.Once all the cols are displayed more rows should be displayed starting with the first cols
page-break-inside:avoid
Very helpful on Firefox! Thanks!
but some troubles with chrome and IE
same problem with
-webkit-column-break-inside: avoid
break-inside: avoid
Extremely helpful script – it got me out of a hole. Thank you.
None of these seem to work anymore in Chrome and Safari.
left and right behave as always in most browsers.
This works great but I have a scenario where content bleeds into a second page and the page break is ignored. The subsequent breaks all work…just not the break where the content bleeds into the next page. Thoughts?
So, the example with always-pagebreak-before H2,
that would leave a H1-header all alone on a page by itself ?
How to avoid that ?
You could target first H2 after H1 with h2:first-child.
That’s great but i have one issue when i use “page-break-after:always” it adds a empty page will you please help me to remove that empty page
I’m trying to get some working example of this. Has it changed at all? http://s.codepen.io/sheriffderek/debug/AXkNWq So far, no luck.
Does anyone know whether these properties work with HTML emails? Or does support depend on the client in which a message is being viewed?
In my tests, page-break-inside: avoid works well in Gmail, Yahoo and even outlook office. But neither page-break-before:avoid nor page-break-after:avoid works in these clients. page-break-before:always works sometimes.
Hi i want to make a page break after some table in pdf ,,i am using this page breaker concept but not it not supporting for me,
plz help me to do…
Hey Preeti,
Have you tried asking around in our forums? Lots of folks from the community hang out and often answer questions there.
Cheers!
I have a requirement wherein a NO PAGE BREAK PDF has to be generated. Is there is a way in CSS to handle this?
My page has lots of content including charts/table with scrollbar for entire page.
Personally, I use FPDF http://www.fpdf.org/ to generate pdf pages dynamically. You can use it to set the page height for the appropriate amount of content, which would give the effect of a “NO PAGE BREAK PDF”.
#betterlatethannever
page-break-inside: avoid
This means that inside the element with this property page-breaks will be avoided. So if you have a wrapper element and add this property there will be no page breaks inside.
Hope this helped.
Which elements support this attribute? I see it working on paragraphs and headers, but not divs (in Chrome).
I have the div to print together with it’s parent div as block, float set none !important, page-break-before and page-break-after set always. – I still don’t get the page break when printing. What can I do yet? – Every help very appreciated!
page-break doesn’t work with floated elements. You need to have an element that’s not floated and apply the property to that element. Then it will work fine.
Is there a method to have page-breaks only when printing to
landscape
in browser print prompt? I have a<table>
stacking/overlaping some lines only when in landscape :/Yes, if you have a class for printing or pdf then having a different class for page breaks under this one will only be applied when printing.
For example. Let’s say you have a .PDF class that you apply to the body when you are printing something. You can also have a .landscape class and a .pageBreak class that you apply to the elements where you want a page-break. If you place this class under the .PDF one it will only apply when printing. Do the same with the .landscape.
// will only apply when printing (generating a pdf)
.PDF .pageBreak {
page-break-after: always;
}
// will only apply when landscape
.landscape .pageBreak {
page-break-after: always;
}