home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Book HomeWeb Design in a NutshellSearch this book

5.3. Cascading Style Sheets for Printouts

A more sophisticated way to control the way your page looks when it is printed is to take advantage of media-specific style sheets. This feature in the CSS2 specification allows a single document to be formatted on the fly depending on the device displaying or outputting it. When it's on the screen, it looks one way; when it prints out, its format changes to be read clearly on hard copy. That eliminates the need to create a separate printer-friendly version of every page on your site. See Chapter 17, "Cascading Style Sheets" for background information on how style sheets work.

The process involves creating two style sheets: one appropriate to screen display and one appropriate for print. Both style sheets are associated with the HTML document using the media attribute or the @media rule. When the browser sends the page to print, the appropriate style sheet is chosen for the job. A more detailed explanation follows.

The downside to using any feature from the CSS2 specification is poor browser support. As of this writing, the media attribute is supported on Internet Explorer 5.0 and higher on Windows and IE 4.5 and higher on the Macintosh (IE does not support the @media rule). Navigator began supporting media types in Version 6 for both platforms. Because of the spotty browser support, you can't rely on these techniques for 100% of your audience, but if you know that your users are up-to-date with their browser downloads (such as in an intranet environment), you can begin taking advantage of them immediately.

5.3.1. Creating the Style Sheets

In this simple example, I begin with a simple, yet properly tagged, HTML file that includes a navigational bar, a headline, and a few lines of text. (Structural tags have been omitted to save space, but they are implied.) I'll call this document sample.html.

<DIV class="navigation">
    <P><IMG src="navigation_bar.gif"></P>
<H1>Alternative Media Style Sheets</H1>
<P>With CSS2 you can create style sheets that are specific to a medium. 
This enables on-the-fly formatting of the document.</P>

I now create a style sheet that specifies how I want my page to look on the screen. Just to be extreme, I've made my background black, my headline red and text gray. This style sheet is named browser.css.

BODY { background-color: black; }
H1 { color: red; font-family: impact; }
P { color: #999999; font-family: verdana; }

I also create a second style sheet that is better suited for a printout. I'd like all the text to be black on a white background. In addition, I don't want the navigation toolbar to appear, so I'll use the display selector to hide the div that contains the image. This style sheet is named print.css.

BODY { background-color: white; 
       color: black; 
       font-family: times; }
DIV.navigation { display: none;}

We're not done yet -- we still need to link the style sheets to the HTML document. Figure 5-2 gives a sneak preview of the results of our media-targeted style sheets so you have an image of where this is going.

Figure 5-2

Figure 5-2. Media-specific style sheets at work

5.3.2. Connecting the Style Sheets and HTML

There are four methods for associating the style sheets with the HTML document. Two use the media attribute within the <link> or <style> element to target the correct style sheet from within the HTML document. The other two use rules that are dependent on medium: @import and @media. The target medium can be one of ten different media types defined in the CSS2 specification. They are:


Applies the styles to all media output (the default)


For monitors


For printouts and print preview displays


For slideshow-like presentations

braille, embossed

For tactile output


For speech-generating devices


For television displays (à la WebTV)


For fixed-width character displays


For small palmtop devices

For now, of course, we are concerned with just the values screen and print. In all of the following methods, multiple media can be specified in comma-separated lists (for example: media="print,projection"). Any style that is set to all media will combine with other media-specific styles. Therefore, if you set a master style sheet to all and a second style sheet to print, the final printed output will reflect a combination of both style sheets.

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.