Print Different

Although you may not realize it, the Web is a multi-medium information source. No, I'm not talking about multimedia-- audio and video streams, for example-- but the actual medium through which the Web's content is conveyed to us. For the vast majority of users, the medium is visual: the monitors we use every day to display pages. But there are many who also turn to the medium of print, creating "hard copies" of pages using their laser printers.

However, the overwhelming aim of Web design is visual, and many Web page designs don't print out very well. Navigation bars, ad banners, and other such things clutter up printouts and make them much harder to read. How many times have you seen a page on the Web which has, off to one side, a link which says, "Click here for a printer-friendly version"?

There are certainly other media-- audio browsers and handheld devices are two such examples-- but the output medium which most concerns authors is still the printer. CSS2 offers a way to address this, giving authors the ability to create stylesheets which are medium-specific. This month, we'll look how you can style a single page for both screen (monitor) and print output, and provide an example you can try using Internet Explorer 5 for Windows, or MSIE 4.5 for the Macintosh.

Making the Association

There are a variety of ways to associate media-specific stylesheets to a document. The most familiar way is to use the LINK element and simply add the media attribute:

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

In the previous example, we see that the stylesheet print.css has been given a media of print; thus, its styles will be used only when the document is printed. These styles will have no effect on the display of the document when viewed using a monitor, audio browser, or any other non-printing device.

There are currently ten defined media types:

all

aural

braille

embossed

handheld

print

projection

screen

tty

tv

These media types are described in some detail in Chapter 7 of the CSS2 specification. The most important is all, which is the default value for any stylesheet which does not have a defined media.

There are other ways to assign media to stylesheets. For example, you can list one or more media types on an @import statement:

@import url(paged.css) print,projection;

Thus, the styles in paged.css will be used in both print and projection media, both of which are known as paged media. Multiple media can be specified for LINKed stylesheets as well. The following two statements are equivalent:

Restyling for Print

Let's now look at a real example of alternate-media stylesheets. In order for these examples to work, you'll need to be using MSIE5/Win or MSIE4.5/Mac, and it will be helpful (though not strictly necessary) if you have a printer handy. Navigator 4.x does not support alternate media on any platform, so it won't be much help with these examples.

First, let's assume a simple page of text with some various elements: paragraphs, headings, hyperlinks, and so on. This file is called latinesque.html, and we want to display it differently depending on whether it's on a monitor, or on paper. First we write a stylesheet for screen display (remember, there's no accounting for taste):

We've added the last style in order to get rid of the ad banner in the printed version. To get an idea of what this will look like, take a peek at Example 2. Realize that this is a screen approximation of a printout, but for the moment, it will do.

Thus we get the document shown in Example 3, which has the screen appearance of Example 1 but the printed appearance of Example 2-- load it up in MSIE, print it out, and see what happens!

(If you'd like to see another example of print-media stylesheets in action, try printing out the CSS Support Grid.)

Just the Tip of the Iceberg

Of course, the simple examples I've provided are just a beginning. There is almost no limit to what can be done with alternate-media stylesheets. Using them appropriately is the key to creating pages which can be styled for regular screen output, printing, legible display on handheld devices, special styles for the blind, and much more. As browsers add support for more media, and more ways to set up media-specific styles, we'll return to this subject for more. Until then, try out some print-media stylesheets on your pages, and see how you can make your site better-- and more accessible-- than ever.