Ïðèãëàøàåì ïîñåòèòü
Ëèòåðàòóðà (lit-info.ru)

Recipe 8.1 Creating a Printer-Friendly Page

Previous Page Table of Contents Next Page

Recipe 8.1 Creating a Printer-Friendly Page

Problem

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

Solution

Create a separate style sheet that dictates how a page looks when printed. 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">

If you're writing a web page in valid XHTML, you need to include a space and a forward slash before the closing bracket at the end of an empty element such as link:

<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 8-1.

Table 8-1. Media types for CSS

Media type

Description

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 presentations—for 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)


You can use one style sheet 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, simply 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.

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 url(print.css) print,projection;

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

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.

    Previous Page Table of Contents Next Page