Web to Print: Restyling Pages on the Fly with CSS

April 28th, 2008 in Design Tips & Tutorials

by: Matthew Griffin

Whether we like it or not, people are still printing information from the web. For years now the big question for newsletters, newspapers, and magazines has been, how do we get our printed product on the web? But as these mediums have gradually adapted to become more webcentric, the opposite question is becoming more appropriate—how do we get our web product to the printed page? Fortunately, this is a much easier question to answer than the first one—and one worth addressing. Until recently, I didn't give this idea much thought. But writing for a blog has brought the issue to the top of the list and now I'd like to share the experience I've gained writing a print stylesheet for Bits O' NewMedia.

Setting Up Your Print Stylesheet

If you've never used a print stylesheet, you'll be surprise at how easy it is. Start by copying your web stylesheet into a new css file called, for example, print_style.css. Then, all you have to do is add a new link tag in the head of your page like this:

<link rel="stylesheet" type="text/css" media="print" href="print_style.css" />

You'll notice that the only difference between this and a link to your primary stylesheet is that the media attribute is set to "print". If you have this link tag in your page and you select file>>print, the print_style.css stylesheet will be used in the printing instead of the primary stylesheet. When you do this, it's important to set the media attribute to "screen" on your web stylesheet link tag. If you don't, your web stylesheet will bleed over into your print stylesheet and result in some funky and frustrating problems. IE7 users won't experience this issue, but EI6 and Firefox users will.

Flushing the Body Out

We will start by focusing on the body selector. Most of the time, the body of a website will have a background color or image. Start by deleting the background property so the background will appear white in the printed version of the page. Most browsers do this automatically when a page is printed, but it's best to be safe. While you're there, make sure that the color property is set to #000 or "black". Colored text looks great on the web but it wastes expensive ink when it's printed. For now, the last body property you should consider is the margin property. I find that the default print margin is a little too small. Try adding 3em to the right and left like this: margin 0em 3em 0em 3em.

Div-Be-Gone: Reformatting the Layout

Naturally, there are parts of a web page that don't need to be printed. The navigational buttons, for example, have no place on the printed page. Go through your print stylesheet and add "display: none" to all of the selectors that shouldn't be printed. When you're done with that, change the float property on all the visible selectors so that your content appears stacked vertically, rather than side-by-side. You can do this by deleting the float property on each selector or by changing it to "none". Finally, change the width property to "auto" on all page-filling selectors. For example, you may have a div tag that wraps the main content of every page. Changing the width property will ensure that the content in that div will fill the entire printed page. Here's a sample comparison from my stylesheets:

In the print stylesheet

div.template_fill_wrapper_top{
    display: none;
}
div.template_fill_wrapper_middle{
    margin: 0;
    width: auto;
    text-align: center;
    clear: both;
}

In the web stylesheet
div.template_fill_wrapper_top{
    overflow:hidden;
    text-align:center;
    background-color: #313131;
    height: 48px;
    overflow: hidden;
}
div.template_fill_wrapper_middle{
    margin: 0 auto;
    text-align: left;
    padding-top:10px;
    clear:both;
}

PX is Out... Em and PT Are In

When we build stylesheets for the web, we typically use the pixel or em measurements for all our padding, width, height, margin, and font-size properties. This is primarily because a digital display measures itself in pixels and letter width. On the printer, though, pixels are out. You will need to convert all your padding, width, height, and margin properties to em values. And your font-size properties should all be converted to pt. Also, keep in mind that your nice big on-screen text will look horsey in print. Keep your body text between 9pt and 11pt. Here's some sample code from the Bits O' NewMedia print stylesheet.

body    {
    margin: 0em 3em 0em 3em;
    padding: 0;
    border: 0;
    color: black;
    background: white;
    font: normal 10pt "Bakersville Old Face", "Times New Roman", Times, serif;
    text-align: left;
}

Putting It All Together

A lot of this work is trial and error. As you make changes to your print stylesheet, test it out, make adjustments, and test again. It a lot of ways it's a new world for those of us who have focused exclusively on the web. But in the end, you'll learn something new and give your visitors something useful. Here's a screen-shot of a print preview from an article on Bits O' NewMedia. You can test it out yourself by printing this page.


  • 11 Comments
  • 9210 Views

Comments

Posted By: Chris on 04/28/08

Interestingly enough, as I was reading this article, an issue came up in which I had to implement the print style sheet. Never had done it before, so boy was I glad I read this blog. So thanks for the coincidence! :)

Posted By: on 04/28/08

Glad it helped, Chris.

Posted By: Joshua Clanton - Design for the WEB on 04/28/08

Good suggestions. For the most part I haven't had to worry about print stylesheets, but I'm going to try to pay more attention to them in the future. Stumbled.

Posted By: Jeremy Sexton on 05/07/08

I had never really seen this in action before. Totally just rocked my world.

Posted By: on 05/07/08

Yeah, it's pretty cool.

Posted By: Yesh Nandi on 05/11/08

Using CSS to print is pretty cool .... but should I be using XSL-FO for printing multi-page outputs. Not sure what the difference is between 2 technologies ...

Posted By: on 05/12/08

Yesh, honestly, I've never used XSL-FO. I do know that using it to restyle a blog page for print will take quite a few more steps than a simple print stylesheet. I think it just depends on your goal.

Posted By: Charles Shields on 10/04/08

What would you charge me to do this? I have most of it done already, but it's not exactly how I want. Also, to change all my px to pt or whatever is best, on both web and printable versions.

Posted By: Joe Johnson on 10/14/08

I did this: Used javascript to put a Print button with this code: <input type="Button" name="printit" value="Print Report" onclick="javascript:window.print();"> Then I did this in the style sheet to force a page-break: h4 {page-break-before: always;} This works, except I also want to identify some page sections to Not Print. In CSS is there something like: H5 {print: none;} ? TIA

Posted By: Joe on 10/14/08

Joe, all you need to do is create a print only css file as described above and set elements to display:none. Just read the article.

Post Your Comment

Comments are closed.