Styling CSS Print Page Breaks

By  on  
CSS Page Break

It's important to construct your websites in a fashion that lends well to print. I use a page-break CSS class on my websites to tell the browser to insert a page break at strategic points on the page. During the development of my websites, I style the page-break class for the "screen" media so I can see how pages will print without needing to "print preview" every time I want to find out where I put my page breaks. The following CSS is all I need.

The CSS

@media screen {
	.page-break	{ height:10px; background:url(page-break.gif) 0 center repeat-x; border-top:1px dotted #999; margin-bottom:13px; }
}
@media print {
	.page-break { height:0; page-break-before:always; margin:0; border-top:none; }
}

Realize that this CSS doesn't account for browser-triggered page breaks; it only shows you where you've put your own custom page breaks. Note that we've also hidden all of the page break visuals for the "print" media.

Just another styling tip to make your CSS print development easier!

Recent Features

  • By
    fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

  • By
    9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

Incredible Demos

  • By
    Introducing LazyLoad 2.0

    While improvements in browsers means more cool APIs for us to play with, it also means we need to maintain existing code.  With Firefox 4's release came news that my MooTools LazyLoad plugin was not intercepting image loading -- the images were loading regardless of...

  • By
    Create a Download Package Using MooTools Moousture

    Zohaib Sibt-e-Hassan recently released a great mouse gestures library for MooTools called Moousture. Moousture allows you to trigger functionality by moving your mouse in specified custom patterns. Too illustrate Moousture's value, I've created an image download builder using Mooustures and PHP. The XHTML We provide...

Discussion

  1. Ben

    Never thought of previewing page breaks this way, great idea thanks David!

    A problem I ran into a few weeks ago:

    We needed to email our customers a set of 3 gift vouchers contained in an HTML email, containing barcodes, that they could print off and take into stores.

    Problem is that the HTML email was longer than your average A4 sheet of paper, so the 2nd voucher was inevitably cut in half and spread over 2 pages. This of course would be different for each user, browser/email client and printer combination.

    Unfortunately I’ve found that very few of the popular email clients and webmail services support CSS page breaks.

    Can you think of any way around this? Or know of any means of generating PDFs dynamically that I could then attach to the emails?

    Cheers :)
    Ben

    • Joves

      Consider using Active PDF to generate a PDF dynamically. We use it for our clients and they love it. It is a bit arcane to work with but with a little patience you can create a nice document.

  2. Bart (Belgium)

    Thanks, man…
    This really helped me out!

    Best regards,

    Bart
    Antwerp, Belgium

  3. vignesh

    Its possible to apply dynamically page break . For example my web page is 1200px . i want to apply break @ height of 600px .

  4. Amy

    This won’t work in Chrome. Have you find a way to make it work on Chrome. I’m facing an issue for last 2 days.

  5. Shane

    The page-break demo works for me on Chrome, but doesn’t work on Firefox. Does something different need to be done for Firefox? Thanks

  6. Kostas

    The margin applies perfect in each page break, but what happens to the page title,page number etc. I don’t want to show it, but there is no other way to hide them except setting margin 0;
    Any workaround on this?

  7. Michael

    This would seem to work if you are defining the page breaks and if you have static content. In my case, I need to show the page breaks in a preview page where the user is editing the content on the fly at runtime (in another page). So the content may grow/shrink and the html is dynamic. I do not think it is possible to show page breaks like you are doing in this case, do you agree?

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!