WWW FAQs: How do I stop users from printing out my web pages?

2004-12-23: "How do I prevent users from printing a web page?"

The best available technique is a very simple one. The cascading style sheet specification offers a way to assign particular styles to particular media -- in other words, to treat printers and monitors differently. This is typically used to provide more attractive printed output, but we can use it to prevent easy printing of a web page.

Notice that I said "easy printing." The user could still save a copy of your web page, edit the HTML, and remove the special style attributes. And if the user doesn't find a way to do that, they can still use the "print screen" button or otherwise capture screen displays and then print them out with any good graphics program. This technique is useful as a reminder that you have not given the user permission to print out the page in question; it is a way of keeping honest people honest, in other words. But nothing can stop a skilled and determined user from printing out a web page.

Here is a simple example of a web page that appears normally on the screen, but prints out as a blank page:

<style type="text/css" media="print">
body { visibility: hidden; display: none }
You can see me, but you can't print me... conveniently.

The key, of course, is the <style> element inside the <head> element. This inline style element first hides the contents of the <body> element (that is, the entire contents of your page), which prevents them from being printed by web browsers all the way back to Netscape 4.0 but still generates blank pages occupying the proper amount of space, and then disables "display" (in this case, printing) of that content entirely so that no blank pages are generated. As a result, modern web browsers will not print unnecessary blank pages, and the generation of blank pages serves as an acceptable fallback for a small number of users with very old browsers.

Legal Note: yes, you may use sample HTML, Javascript, PHP and other code presented above in your own projects. You may not reproduce large portions of the text of the article without our express permission.

Got a LiveJournal account? Keep up with the latest articles in this FAQ by adding our syndicated feed to your friends list!