Just about everybody knows that you can specify what media a CSS style sheet applies to – whether it’s for print, screen or both. When users want to print pages on your site, this allows you to deliver up a specially constructed style sheet that hides things that you’d rather not have print (ads, navigation, etc.) and adds things that you’d rather not have on screen (maybe some kind of letterhead or a page footer) so that it’s print-friendly. In this respect, CSS has done wonders for making content on the web that much more ubiquitous and practically sounded the death knoll for the old fashioned technique of having a “Printer Friendly” button that links to a pop-up or new window containing an unformatted version of the original page.
I’d like to share a technique that I’ve found useful when setting up sites to be print-friendly. First off, declare your respective stylesheets in the document head:
<link media="screen" href="default.css" type="text/css" rel="stylesheet" />
<link media="print" href="print.css" type="text/css" rel="stylesheet" />
As you can see, default.css is for the screen and print.css is for printing. Now, in the actual markup what I’ll do is add classes called “print” and “noprint” to various elements:
<h1 class="print">This is a Printed Headline</h1><p>This will show up on screen and when printed.</p>
<img src="photo.jpg"Â class="noprint" alt="This photo will be shown on screen only" />
In case it’s not evident at this point, what I’m doing here is indicating which elements will be for print only and which are for screen only. Everything else will be displayed on both.
Now in my screen stylesheet, default.css, I’ll add the following styles:
.print { display: none; }
.noprint { display: block; }
And in the print stylesheet, print.css, I’ll reverse it:
.print { display: block; }
.noprint { display: none; }
Now it’s easy for me to globally show (or hide) elements depending on the medium we’re in – screen or print. Nothing groundbreaking here, but this a lot cleaner than addressing each element one by one in your stylesheet.