Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

Kissing Print Versions of Pages Goodbye

Rated 3.82 (Ratings: 9) (Add your rating)

Log in to add a comment
(18 comments so far)

Want more?

 
Picture of luminosity

Lachlan Cannon

Member info | Full bio

User since: October 13, 2001

Last login: October 13, 2001

Articles written: 5

Have you ever gone to a site at which you needed to click on a link inside an article to have a printable version without the site's chrome, and thought to yourself that surely there must be a better way? Welcome to the power of CSS.

One of the many great things about CSS is that it is absolutely trivial to make the same page display across a range of media according to the needs of that particular medium. Among the plethora of media which CSS already covers is print. So how can we use CSS to optimise a page for printing?

I'm going to assume that your site already uses CSS for layout — or at least uses structural layout. Provided that your site uses a well designed table layout, you should also be able to apply a print style sheet to it.

So, how do we do it?

The first thing you need to do is link in your new style sheet and specify it is for print media.

If you're using <link> to include your style sheets, you can add a new link in your documents, pointing to the style sheet for print formatting, and add the attribute media="print". For example:
<link rel="stylesheet" href="print.css" type="text/css" media="print" />

To mark a section of embedded style in your document as print only, add the same attribute to your <style> tag, as in:
<style type="text/css" media="print">

To @import a print style sheet, add "print" after the declaration, for example:
@import url(print.css) print;

Lastly, to include print stylings inside already existing style sheets, surround the print rules with @media print { } for example:
@media print { body { background-color: white; } }

The next thing we need to address is deciding what parts of the page are irrelevant when printed. You'll end up deciding yourself what formatting your print style sheets need, of course, but for the purposes of this article let's assume you have a header bar, a background image, a navigational menu, a breadcrumb and the usual disclaimers (privacy policy, terms of use, etc) in addition to your content. The next thing you have to do is consider the printed page. If someone prints your site to show it to a friend what is going to be irrelevant to them?

Let's start by massacring your carefully designed layout. Firstly, the navigational items, such as the breadcrumb and nav menu have to go. They have no use on a printed page, where the links on them can't be interacted with, so they're cut out by a display: none; declaration in the print stylesheet for those elements. Next in line to go is the background image - there is nowhere logically to put a background image, except behind the text, which it would obscure. In fact, all the items I mentioned except the content have no real relevance to someone reading the paper, so we set them all to not display.

So we're done now, right? Wrong. Although you've adjusted your site structure to better fit the printed page, other things remain to be changed. Take, for instance hyperlinks. Since they can't be interacted with offline, it may make sense to you to remove their underline (via text-decoration: none;) so that they read as normal text. You do write link text so as to make sense when read without the link, don't you? Alternatively, you may wish to keep your underlines there, to indicate to the reader that they are hyperlinks, and in your print style sheet you can also use CSS generated content to print the url of hyperlinks out after them. The code for doing that would appear like this:
a:after{content:"[" attr(href) "]"; }

What else can we do? Well, we can format borders to better suit printed documents, get rid of the help borders that many people use for acronyms, and abbreviations, and a host of other changes in this vein.

Also, don't forget that just because points don't make sense as a font size unit for the screen, doesn't mean they don't work well in print. In fact, you should be using point instead of pixel if you're going to use absolute font sizes at all — pixels come out much smaller on printers than the web, so your nice 12 pixel Verdana that looks great on screen might be so small as to be unreadable in print.

In Summary

CSS offers structural sites an easy way to implement chrome-free printing, without requiring a special print version, as well as all the other benefits it provides. If you're not using this technology yet, you should be considering it.

Advantages of print style sheets

  • Totally transparent to user — they only have to click print, instead of having to select a printable version of the page and then click print.
  • Easy to modify the way your print pages are formatted.
  • Once you get past the basics of formatting pages this way CSS can specify even more page formatting options, that you've never had access to before.

Disadvantages

  • Won't work for users of older browsers.
  • It can be hard making sure your print style sheets are working. Some browsers do not have print preview options, and IE sometimes has trouble displaying them in print preview when they're stored locally.

Final thoughts

Coming from a user standpoint, if nothing else, I believe that using print style sheets makes a lot of sense. No one wants to have to use more effort than necessary, so if you have a choice why make them have to click twice to print something, when all they should have to do is click the print button?

Born in Melbourne, Australia, Lachlan is soon to begin study at University.

Lachlan delights in being sent money, and other small gifts. But if you really have the money to spare ;) give it to something more worthy.

which browsers?

Submitted by fraggle on March 12, 2002 - 09:03.

good thinking (saw this first on AListApart), but it may be a good idea to let us know which browsers this trick will work with? is this only the recent browsers?

login or register to post comments

Re: which browsers

Submitted by luminosity on March 12, 2002 - 23:56.

I know that the media="print" works in the latest versions of Opera, IE, and Netscape, however I'm not sure going back. I believe that Netscape has had support since 6.0, IE has had it since 5.0, and I don't know when Opera started supporting it.

login or register to post comments

Big plus for usability

Submitted by noahlazar on March 13, 2002 - 01:19.

I've been using basic printer styles on my client sites for a while now. In most cases, I simply assign a display:none style for elements such as navigation, fluff images, and some spacing. If a user is going to print an article, do they really want (or even need) to see the sidebar, et al on their prinouts?

I know Netscape 6.x, Opera 6, and IE 5.5 on up handle printer styles. IE also has many JavaScript features to even further control printing (as seen on WebReference).

Another handy feature you can implement is to have alternate logos for the screen and printer. The printer version of your logo and tagline can be aliased to white for better readabiliy. To do this, have your HTML code show both logos, with the printer-only one set to display:none in your screen CSS file. Your printer CSS file should then show that logo and hide the original one. To be compatible with older browsers that cannot handle style sheets (and for whom you do not want to display two logos on the screen), you can even do a browser sniff and then dynamically insert that second invisible logo.

Since adding printer styles only takes a few minutes and degrades without a problem for older browsers, do a favor to your users and clean up their printouts.

login or register to post comments

Another method..

Submitted by djc on March 14, 2002 - 09:40.

I believe this is only supported in NS6/Mozilla and Opera right now, but a trick I use is to use the 'alternate stylesheet' value for the 'rel' attribute of the 'link' tag to define extra styles(like print ones) like so:

<link rel="STYLESHEET" type="text/css" href="/djc/intro.css">
<link rel="alternate stylesheet" title="Blue & White style" type="text/css" href="/djc/stdio/bluewhite.css">
<link rel="alternate stylesheet" title="A different style" type="text/css" href="/djc/stdio/new.css">

Then, from the menu bar of the browser, View -> Use Stylesheet -> whatever style. This loads the different styles on the fly, and without a page reload or any server processing which is kinda nice. The code above is working on my site if anyone wants to see a demo.. Just another option to consider!

login or register to post comments

limitations

Submitted by komi on March 14, 2002 - 10:35.

If you happen to know the limitations of publishing documents for print using CSS when compared to PDF solutions, please let us know. Surely, there must be limitations in either the features of CSS or latest browser implementations when it comes to generating documents like printable accounting reports that have table data that can span multiple pages.

login or register to post comments

Almost transparent

Submitted by carlbeeth on March 16, 2002 - 11:00.

I agree completely with the article with one tiny exception: It is not yet transparent to the user. The problem is expectations, print versions buttons have been used for such a long time now that the visitors will actually need a little time to adjust to the CSS way of doing things. A good example is the huge "miracle print" box they have at a list apart to explain that they don't need css (granted it serves the double purpose of promoting CSS).

login or register to post comments

Another option

Submitted by luminosity on March 31, 2002 - 19:55.

Another option in your print stylesheets, that I think I should have mentioned in the article is that you could also print out the values of acronyms / abbreviations using CSS, assuming you use the title to hold the abbreviation / acronym's full reading. If you want people reading the print to be able to know all the cronyms like people viewing it on the web can, then simply add the following code to your stylesheet.

abbr[title]:after, acronym[title]:after {
    content: "(" attr(title) ")";
}

This code will print out a ( followed by the title of the acronym / abbreviation, followed by a closing ), but only for acronyms and abbreviations that have a title, so that you don't end up with empty brackets in your page if you forgot a title. This could be a little annoying to users if you markup every occurence of the acronym or abbreviation on the page, however, but marking up only the first one to occur on the page would seem to make sense both online and in print. If the user doeswn't know the term they'll try to find out from the first one to occur, won't they?

login or register to post comments

Browser support

Submitted by davespicks on April 21, 2002 - 10:54.

The biggest problem I've found with using printing style sheets is that browser support on the Mac browsers (for 9.x) is spotty at best.

Internet Explorer 5.1.4 doesn't seem to support page-break-inside: avoid; or display:none; visibility: hidden; in all cases (that may be a caching issue, but I haven't found a way around it). Also, displaying a print style-sheet in the browser looks different from viewing the page in "Print Preview", which makes me wonder what's going on.

Netscape 6.2 Mac still has the bug where it won't always correctly load the page, leaving me with an empty page about 80% of the time. When the page does load, pages are often garbled because the browser won't load the style-sheets (same connection problem) or won't load images.

Opera 5 Mac seems to do a the best job of the bunch, but it doesn't seem to honor page-break-before: avoid; (or after).

After two hours of fussing with it, I'm starting to think it would just be easier to write some PHP that would pre-process the page and generate a printable version that way. A very discouraging morning.

login or register to post comments

a:after{content:"[" attr(href) "]"; }

Submitted by cre8d on May 7, 2002 - 20:29.

hiya, i can't seem to get this working.. i have a href="mailto:" link i'd like to be displayed in the print version -- is there any particular position the a:after needs to go? a:link, a:visited, a:hover, a:active ... thanks :)

login or register to post comments

Re: a:after

Submitted by luminosity on May 7, 2002 - 22:49.

What browser are you looking at this in? Currently, the only browser I know of that supports generated content is Opera 5+, unfortunately. Hopefully soon browser support will pick up so that this can become a useful widely-adopted tool. In the meantime if you absolutely have to have the link printed out then you might be able to use JavaScript, or you might want to still use a server generated printer friendly page for now.

login or register to post comments

Re: a:after

Submitted by cre8d on May 7, 2002 - 23:57.

ahh, oops, I read up on a:after and misread browser support being everything *but* opera, rather than *just* opera supporting this... thanks for clearing that up for me!

login or register to post comments

where does the unprinted content go?

Submitted by nickyj on January 22, 2003 - 07:30.

hi! nice article btw.

I'm wondering, I'm playing with the print stylesheet and I'm getting a huge gap at the top of the page after setting my along-the-top navigation to display: none; visibility: hidden. I'd like to "shunt" the printed content up to the top of the page, any ideas? Or is this not really doable yet?

Thanks for any info, if I find anything out I'll post back here.

Cheers
Nicky

login or register to post comments

Re: where does the unprinted content go?

Submitted by luminosity on January 22, 2003 - 20:23.

If your hidden elements have all been given display: none;, then the only thing I can think of is that you've either got margins too big, or you used absolute positioning in your screen stylesheet, but you used media="all" so it's been carried into your print one. Try messing around with the margins and giving your elements position: static;.

login or register to post comments

The @media rule

Submitted by Chiefdnd on January 23, 2003 - 13:30.

A good source of reference is the CSS2 Specifications found at http://www.w3.org.

Section 7.2.1 The @media rule

This section refers to the media types and gives an example for Screen and print. Here we have expanded it to include a page break.

@media print{
BODY{
font-size:10px;
}
table{
font:10px Tahoma,Verdada,"Myriad Web",sans-serif;
}
br.newpage{
page-break-before:always;
}
}
@media screen{
BODY{
font-size:14px;
}
table{
font:13px Tahoma,Verdada,"Myriad Web",sans-serif;
}
}
@media screen,print{
BODY{
line-height:1.2;
}
}

login or register to post comments

Repeated headers & footers

Submitted by PeterS on January 11, 2004 - 17:41.

Great article & a good start to printing Web! One thing I haven't on any forum or article list is how to get repeated headers & footers (of our own crafting, not the default browser headers). Basically I'm looking for a div which will print at the top or bottom of every page. may be pushing the CSS envelope... Any thoughts? Thanks, PSt.

login or register to post comments

Safari margins?

Submitted by porkozone on February 28, 2004 - 20:27.

After reading this, I have been trying to get my site to work with a printer version or my CSS styles. I have noticed on a lot of sites with printer css styles, Safari gets it wrong, adding too much space on the left and right sides, and usually getting the font size larger than intended. I have seen a reference saying adding width:7in; helps with Safari, and notice that alistapart.com uses this as well. However, dong this has caused inconsistencies with IEwin and Mozilla.

In my testing, IE6 shows much less margin on the sides upon printing, and I am wondering if this will be beyond the default margins of some printers. Will margin-left: auto; change the margins depending to the printer's settings? What worries me most is that with various settings in addition to width:7in; causes Mozilla win to cut of the right edge of the text. In fact, the alistapart printstyles have this flaw.

Is there any way to help Safari play well without resorting to the 7in trick? Or does anyone have experiences in getting the 7in trick to play well with other browsers?

login or register to post comments

Print Image on Remote Framed Document

Submitted by Tishrii on April 28, 2004 - 13:56.

I keep a site for a real estate agent. According to mls (Multiple Listing Service) requirements, his site must use frames and call the house listings by menu link to the main framed viewing area. This is no problem, however, he is required to ensure that the "Equal Housing Opportunity" image shows on any document printed there.

How can I force this image to show up on a remotely served dynamic page when printed?

I tried <frame name="1" src="mid.html" scrolling="auto" style="vertical-align: top; @media print { body background-image: url('http://www.hisurl.com/images/eho.gif'); background-repeat: no-repeat; background-color: transparent; background-position: bottom; width: 79px; height: 72px;}">

... but it didn't work. Is the tag configured incorrectly or am I way off base on methodology here?

Thanks!!!!!! Lisa

login or register to post comments

Strucure Selectors

Submitted by falski on June 26, 2007 - 11:32.

This is probably a really simple question, but how do I find out what my site's structure selectors are called? In the Wordpress Codex page they suggest using "#sidebar { display:none }" but my theme has two sidebars. The filenames for these are left-sidebar.php and right-sidebar.php, so I thought that I should use "#left-sidebar { display:none }" and "#right-sidebar { display:none }" in my style sheet... but this doesn't work. I'm a newbie to this sort of thing so apologies if this should be really obvious!!! Cheers Alan

login or register to post comments

The access keys for this page are: ALT (Control on a Mac) plus:

evolt.orgEvolt.org is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.