Advanced CSS Printing — Using CSS Page Breaks

Written by David Walsh on Friday, January 4, 2008


I have one customer that absolutely insists his web pages print perfectly. Why? Because he refuses to look at his pages on the screen — he tells his employees to print the website for him to look at. And since he looks at pages that way, he believes most of his customers do just this.

Needless to say, I’ve learned quite a few tricks to making a website print properly. I’ve already shared methods for making your website content printer-friendly, as well as making your website structure printer-friendly. One important aspect of making your pages printer-friendly is by using CSS/XHTML page breaks.

There are numerous spots that are good for page breaks:

  • Between page sections (h2 or h3 tags, depending on your site format)
  • Between the end of an article and subsequent comments / trackbacks
  • Between longs blocks of content

Luckily, using page breaks in CSS is quite easy.

The CSS Code

@media all
{
	.page-break	{ display:none; }
}

@media print
{
	.page-break	{ display:block; page-break-before:always; }
}

The XHTML Code

<div class="page-break"></div>

The Usage

<h1>Page Title</h1>
<!-- content block -->
<!-- content block -->
<div class="page-break"></div>
<!-- content block -->
<!-- content block -->
<div class="page-break"></div>
<!-- content block -->
<!-- content -->

Epic Discussion

Commenter Avatar January 04 / #
Shawn says:

I like this tip, but I just can’t wrap my head around the fact that a client would print out webpages. I know there are some quirky clients out there, but this one shakes my head. :)

Commenter Avatar January 04 / #
Kyle says:

Cool. Didn’t know that. For added semantics, you could apply the class to a horizontal rule tag, thus:

Commenter Avatar January 05 / #
Charles says:

Great info — thanks for posting.

As for why someone would print a webpage… reports, quotes, sale papers, etc.

The majority of the reports going out to our employees is via our intranet and sometimes you need to print them.

Commenter Avatar January 06 / #
Huckle says:

Excellent tip. I need to print out web pages all the time for referencing coursework in my A levels and so often it prints out crap.

Commenter Avatar January 07 / #
Muizz says:

Nice tip. Could someone please explain how to introduce page breaks in a web page containing Database generated rows? I’m having that problem now, as IE6 and FF2 do not render my report well. IE breaks a row in half, FF does not print the second page.

@Charles: I’ll appreciate if you share.

Commenter Avatar January 07 / #
Muizz says:

Hello All. I have added the following definition to my separate Print style sheet:

height: 80%;

This ensures that the database content is not broken at any row. Works well in IE6 but not FF2.

Ideas from here – http://davidwalsh.name/optimizing-your-website-structure-for-print-using-css/

Commenter Avatar January 08 / #
Tim says:

Hey, I printed this article out for use in my webdev cheatsheet binder…printed great (once I removed background images).

Thanks!

Commenter Avatar January 10 / #
James says:

Smells a bit like the gmail postal service…. people will use technology the way that best suits them – still hate to think of the paper and ink costs.

Commenter Avatar January 12 / #
John says:

@ Kyle: I would’ve thought an ordinary line break (<br>) would be a good candidate for this sort of thing too.

Commenter Avatar January 13 / #
Martin says:

@john
Yeah or a tag ? It can be used to split content in parts… so why not use it for print page breaks.

Commenter Avatar January 13 / #
Martin says:

grom why is he filtering tags… and not just translating them to normal text !!

Commenter Avatar January 13 / #
Martin says:

ok this is very anyoing!! HR TAG is should be! (HR/)

Commenter Avatar January 14 / #
Siegfried says:

Right. But introducing a .pagebreak class is simply presentational markup. I think that should be avoided. In my print stylesheet i have this:

h1, h2, h3, h4,
h5, h6 {
page-break-after: avoid;
}
table tr, ul, ol {
page-break-inside: avoid;
}

This seems to work reasonable well without enforcing me to add anything what is only presentational markup. If i’d understand this better i could add the “orphans” and “widows” to the stylesheet, but for now it works just acceptable.

And, btw., print paper does not have the same size all over the world. The US “letter” format is different to the European “Din A4″. Fixing the paper size to US letter caused many problems in the past. Those problems should not be repeated nowadays.

Commenter Avatar January 15 / #

I wonder where the flaws are. I was aware of the page-break attribute, but the last time I tested it, it didn’t work in any browsers. Well, time has passed, how is crossbrowser support today?

Commenter Avatar January 16 / #
Siegfried says:

I tested it in IE7, FF2, Konqueror and Epiphany and it seems to work.

Commenter Avatar January 28 / #
sauravh says:

good idea

Commenter Avatar February 10 / #

These are great ideas and I would advise actually printing out your sites when you use this just to make sure. It seems like an easy thing to skip and not test but it does matter

Commenter Avatar June 18 / #

Can anyone please provide a simple sample HTML that can be copy and pasted in one block to try the page-break-inside CSS? I have tried several forms of this including the example web page at http://www.richinstyle.com/test/paged/pagebreakinside.html

None of them seem to work? I tried in IE 7 and Firefox 2.

Any help greatly appreciated guys! ;-)

Commenter Avatar June 18 / #

Stupid! i forgot to mention that it is actually ‘page-break-inside: avoid’ i am having a problem with. Sorry guys.

Commenter Avatar June 18 / #
cliff says:

thanks, great advice. I was having a hard time with this

Commenter Avatar June 30 / #

Smells a bit like the gmail postal service…. people will use technology the way that best suits them – still hate to think of the paper and ink costs.very nice……..

Commenter Avatar July 22 / #

I like this tip, but I just can’t wrap my head around the fact that a client would print out webpages.

Commenter Avatar July 25 / #

I tested it in IE7, FF2, Konqueror and Epiphany and it seems to work.

Commenter Avatar August 09 / #
Chris says:

This is very interesting as I often have this problem, thanks

Commenter Avatar August 27 / #
Awestruck says:

Excellent tips, thanks. Now how do I prevent a side panel with navigation buttons from printing?

Commenter Avatar September 19 / #
Tong says:

Thank you so much! I’m trying to solve this problem for a while, and finally find your article! It’s indeed a life saver.

Commenter Avatar October 02 / #
Awestruck says:

Interesting. But what does the word ‘avoid’ do? I have made page breaks work using the tips given, however I have a problem in that I don’t want the banners on the pages to print.
How can I make the printer start from just below the banner?
Awestruck

Commenter Avatar October 06 / #

This is very interesting.thanks

Commenter Avatar October 09 / #
izokon says:

Excellent tips, thanks a lot.

Commenter Avatar October 13 / #

hi,
My web page is too long to print in one A4 size page.I want to print my site’s specification page in to one A4 size page By CSS .Can any one tell me how can I do this?

——-bongoneshian
http://www.gsmyard.com

Commenter Avatar October 31 / #
güvenlik says:

Gre at tips.thanks

Commenter Avatar November 19 / #
david says:

…well and another way to do this is to not do it by yourself. Let the user have it his way by linking your pages directly to a online service like PrintWhatYouLike. This article for instance would look like this

Commenter Avatar November 28 / #

Ok, I have an inkling (pun not intended) to modify that book image to the right to say “Schneier on Squid.”

“The closest the squid industry has to a rock star.”

Commenter Avatar December 12 / #
bayanlar says:

There are working fine now! Thanks.

Commenter Avatar December 15 / #
branda says:

This is very interesting as I often have this problem

Commenter Avatar December 17 / #
huzurevi says:

Thank you so much

Commenter Avatar January 05 / #
Keith Wolf says:

Yeah I had to use this for a sales quote project for the retail company I work for. I allowed there to be an unlimited number of items on the printed quote and when it reached a certain number in the PHP loop it would insert a pagebreak line, worked like a charm.

Muizz said: FF does not print the second page

Yeah I ran into this issue on a content site, I found that in the print stylesheet set all items that are to be printed as display: inline. This doesn’t work for every situation but often in FireFox, when it tries to print a very large block level (display: block) item (say the main content portion is to be printed and it has been set to block for screen display purpose), it tries to print it all on one page (one big block). Inline will allow each line to be printed on its own merit and usually splits pages properly.

Commenter Avatar January 05 / #
Keith Wolf says:

@myself – That last comment of mine, for getting FF to print properly, it also helps to set that main content as float: none. I honestly don’t remember why but it really helped for me.

So in the stylesheet:

#mainContent { float: none; display: inline; }

Commenter Avatar March 04 / #
ARTI says:

I have one customer that absolutely insists his web pages print perfectly. Why? Because he refuses to look at his pages on the screen he tells his employees to print the website for him to look at. And since he looks at pages that way, he believes most of his customers do just this. Needless to say, I’ve learned quite a few tricks to making a website print properly. I’ve already shared methods for making your website content printer-friendly, as well as making your website structure printer-friendly. One important aspect of making your pages printer-friendly is by using CSS/XHTML page breaks.

Commenter Avatar March 18 / #
Güvenlik says:

thank you…

Commenter Avatar April 28 / #

Gre at tips.thanks

Commenter Avatar April 28 / #

Yeah I ran into this issue on a content site, I found that in the print stylesheet set all items that are to be printed as display: inline. This doesn’t work for every situation but often in FireFox, when it tries to print a very large block level (display: block) item (say the main content portion is to be printed and it has been set to block for screen display purpose), it tries to print it all on one page (one big block). Inline will allow each line to be printed on its own merit and usually splits pages properly.

Commenter Avatar April 28 / #
sac ekimi says:

This is very interesting as I often have this problem

Commenter Avatar April 28 / #
Keith Wolf says:

Uh, why did yuz estetigi just repeat, word-for-word, exactly what I said a few weeks ago? Dude, at least quote me and add something of value.

It looks like this same person did the same thing to another comment. (yuz estetigi is sac ekimi and also Burun Estetigi) … can you say spam?

Commenter Avatar July 12 / #
Allaa says:

Hello all,
I am a bit confused, can u please provide me with steps 1,2,3 like to make my web page printable?

Commenter Avatar July 17 / #

The easiest thing to do is probably to convert PPT to PDF. You can certainly use Camtasia to make a recording of a presentation using your PPT. A good format for vodcasting that is m4v, which will play on video ipods. Here are tutorials for how to do that.

Commenter Avatar July 17 / #
Allaa says:

Thank u all, i found it very useful
Allaa

Commenter Avatar September 01 / #
Ross says:

This is pretty handy because we often map users experiences by printing the pages of our site and then following their path through our site. Thanx for that!

Commenter Avatar September 14 / #

Could someone please explain how to introduce page breaks in a web page containing Database generated rows? I’m having that problem now,

Commenter Avatar October 23 / #
Corey says:

@Shawn: Easy, for printable documents (Wikis, Instruction guides, or toilet reading material), or when generating PDF’s from webpages with a PDF printer.

Lots of people still like “Dead Tree”

Commenter Avatar November 10 / #
Kabin says:

nice article, thanks for your share..

Commenter Avatar December 18 / #
huzurevi says:

This is pretty handy because we often map users experiences by printing the pages of our site and then following their path through our site. Thanx for that

Commenter Avatar December 25 / #
estetik says:

Thanks for sharing an article …:))) much fun ….

Commenter Avatar January 18 / #
kurye says:

Stupid! i forgot to mention that it is actually ‘page-break-inside: avoid’ i am having a problem with. Sorry guys.

Commenter Avatar January 27 / #
Dek Thai says:

You are the God.

Commenter Avatar February 26 / #
söve says:

Great post.Thanks.

Commenter Avatar March 04 / #
dlibian says:

I just found out that if you are printing out a largish table with more rows than will fit on a single printed page… Firefox will very helpfully print the your table header (TH tag) on each printed page for you.

Commenter Avatar March 08 / #
Estetik says:

I’ve been working on a multi-client research project on redefining employee computing, and I cannot count the number of clients who have locked out social networking sites, even LinkedIn, or simply do not understand the “web2.0 thing.” Even for clients who are a bit more open in their thinking, are looking for “who has done this already…prove it.”

Commenter Avatar March 25 / #
DoZ says:

Clients… :(
One of mine did ask me if he could get the forms from his website to be faxed to him!

Commenter Avatar April 06 / #
kuryetr says:

thank you very much. succesfull article

Commenter Avatar April 19 / #
güvenlik says:

Thank you for articles

Commenter Avatar May 12 / #

Stupid! i forgot to mention that it is actually ‘page-break-inside: avoid’ i am having a problem with. Sorry guys.

Commenter Avatar May 18 / #
check up says:

I just found out that if you are printing out a largish table with more rows than will fit on a single printed page… Firefox will very helpfully print the your table header (TH tag) on each printed page for you.

Commenter Avatar May 25 / #

Stupid! i forgot to mention that it is actually ‘page-break-inside: avoid’ i am having a problem with. Sorry guys.

Commenter Avatar June 19 / #
estetik says:

Stupid! i forgot to mention that it is actually ‘page-break-inside: avoid’ i am having a problem with.

Commenter Avatar June 23 / #
Charlype says:

@Siegfried: Ciao non so chi tu sia ma immagino Maxim, se e’ cosi son contento ……….

@Siegfried:

Commenter Avatar June 29 / #
estetik says:

Why this web site do not have other languages support?

Commenter Avatar July 01 / #
hairdresser, hair, hair style says:

@fibercement: I sell stuff online – I want customers to be able to print out the invoice off the webpage when complete. Page break let’s me separate page 2 for terms and conditions… That’s one instance, I could think of another 10-15 instances where someone would need to print out a webpage…

Commenter Avatar July 22 / #
Estetik says:

This is pretty handy because we often map users experiences by printing the pages of our site and then following their path through our site. Thanx for that!

Commenter Avatar July 22 / #
Güzellik says:

I just found out that if you are printing out a largish table with more rows than will fit on a single printed page… Firefox will very helpfully print the your table header (TH tag) on each printed page for you

Be Heard!

I want to hear what you have to say! Share your comments and questions below.

Name*:
Email*:
Website:  


© David Walsh 2007-2010. Contact David Walsh. Powered by the remarkable MooTools JavaScript framework.