XML technologies / CSS / CSS Cookbook / Printing / Creating a printer-friendly page

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

Create a printer-friendly page without generating a separate document manually or dynamically.

Solution

Create an additional stylesheet defining how the page should look in print. Here, the print stylesheet is named print.css.

Then attach it to the document and set the attribute media 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

Stylesheets can be defined for a range of media. By default, the attribute media has the value all. Without this attribute, all stylesheet rules apply to every supported media type.

Besides the most commonly used media type screen for on-screen documents, the CSS 2.1 specification defines several other types. The following table provides an overview.

Table: Media types.

Media type

Description

all

Suitable for all output devices.

braille

Suitable for devices displaying Braille directly.

embossed

Suitable for Braille printers.

handheld

Suitable for handheld computers, or palmtops, with small screens and limited bandwidth.

print

Suitable for paged output, such as printing, and print previews.

projection

Suitable for projection media, such as video projectors.

screen

Primarily suitable for color screens.

speech

Suitable for speech output devices.

tty

Suitable for media using fixed-width fonts, such as telex machines, terminals or portable devices with limited displays.

tv

Suitable for televisions and similar devices with low-resolution color screens, limited scrolling and possibly sound output.

One stylesheet for all media types may suffice for your page’s styles:

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

Alternatively, share a stylesheet between selected media types. To use the same styles for projection and print, specify two comma-separated values for the attribute media:

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

In the example above, rules from print.css are used for printing and projection.

Assigning media types with @import

Besides link, other ways to specify media types include rules @import. The next example also specifies a stylesheet for print and projection:

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

The rule @import can appear inside an element style or in an external stylesheet.

Assigning media types with @media

Another way to specify stylesheets and media types uses blocks @media. These let you define rules for different media types within a shared stylesheet:

<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

The explanation of media types in section 7 of the CSS 2.1 specification.

  

 

 

 

back to top