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 |
|---|---|
| Suitable for all output devices. |
| Suitable for devices displaying Braille directly. |
| Suitable for Braille printers. |
| Suitable for handheld computers, or palmtops, with small screens and limited bandwidth. |
| Suitable for paged output, such as printing, and print previews. |
| Suitable for projection media, such as video projectors. |
| Primarily suitable for color screens. |
| Suitable for speech output devices. |
| Suitable for media using fixed-width fonts, such as telex machines, terminals or portable devices with limited displays. |
| 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.
Copyright of the German edition © 2007 by O’Reilly Verlag GmbH & Co. KG
You may print the online version for your private use.
Otherwise, this chapter from “CSS Kochbuch” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
O’Reilly Verlag GmbH & Co. KG, Balthasarstr. 81, 50670 Köln