CSS Cookbook, 2nd Edition

Problem

You want to create a printer-friendly page without having to generate another web page manually or dynamically.

Solution

First, create a separate style sheet containing CSS rules that dictate the desired look when a page is printed. For this example, the style sheet with print-only CSS rules is named print.css.

Then associate the style sheet and set the media property to print:

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

Discussion

You can use style sheets to dictate the presentation of documents in a wide range of media. By default, the value for the media attribute is all. Without the attribute, the user agent will apply the CSS rules in the style sheet to all media.

Although the most common attribute you probably have encountered is screen, which is used mainly for displaying documents on color monitors, the CSS 2.1 specification actually defines a total of ten media types, as shown in Table 10-1.

Table 10-1. Media types

Media typeDescription

all

Suitable for all devices

braille

Intended for Braille tactile feedback devices

embossed

Intended for paged Braille printers

handheld

Intended for handheld devices (typically small-screen, limited-bandwidth devices)

print

Intended for paged material and for documents viewed on-screen in print preview mode

projection

Intended for projected presentationsfor example, projectors

screen

Intended primarily for color computer screens

speech

Intended for speech synthesizers

tty

Intended for media using a fixed-pitch character grid (such as teletypes, terminals, or portable devices with limited display capabilities)

tv

Intended for television-type devices (with low-resolution, limited-scrollable color screens, and available sound)

When defining the styles for your web page, one style sheet can be used for all media:

<link rel="stylesheet" type="text/css" href="uber.css" media="all" />

Or you can use one style sheet for several, but not all, media.

For instance, to use one style sheet for both projection and print media, separate the media values with a comma:

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

In the preceding code, the print.css style sheet is used for projection and print media when rendering the web document.

Using @import when assigning media types

You can use other methods besides link to assign media types. One method is @import, as shown in the following line, which specifies the style sheet for both print and projection media:

@import URI(print.css) print,projection;

The @import rule needs to be placed within a style element or within an external style sheet.

Using @media when assigning media types

Another method you can use to associate and dictate style sheets and media types is @media, which enables you to write blocks of CSS rules that can be set for different media, all in one style sheet:

<style type="text/css"> @media print { body { font-size: 10pt; background-color: white; color: black; } } @media screen { body { font-size: medium; background-color: black; color: white; } } </style>

See Also

"Media Types" in Section 7 of the CSS 2.1 Working Draft, http://www.w3.org/TR/CSS21/media.html.

Категории