XML technologies / CSS / CSS Cookbook / General information / Stylesheets for different output media

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

Define separate stylesheets for printing, screens, PDAs, speech devices and other media.

Solution

Create external stylesheets for each medium, naming them by purpose, such as print.css, screen.css and handheld.css. Use the element link with a media type to load each stylesheet, or use a rule @media.

Here is the file print.css:

body {
  font: 10pt times, georgia, serif;
  line-height: 120%
}

A new file named screen.css:

body {
  font: 12pt verdana, arial, sans-serif;
  line-height: 120%
}

And another file named projection.css:

body {
  font: 14pt;
  line-height: 120%
}

Add these lines to the head section to attach the three files, specifying each medium:

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

The rule @media also defines styles for different media directly within a stylesheet:

<style type="text/css">
<!--
  @media print {
    body { font: 10pt times, georgia, serif }
  }

  @media screen {
    body { font: 12pt verdana, arial, sans-serif }
  }

  @media projection {
    body { font-size: 14pt }
  }

  @media screen, print, projection {
    body { line-height: 120% }
  }
-->
</style>

Discussion

Save print styles in print.css. Only these apply on a printer, saving space and ink by omitting images, for example.

Media-specific styles affect only devices supporting that media type. They do not affect other media or the web page itself.

The rule @media lets one stylesheet cover multiple media.

The next figure shows printing with the original screen layout. Sidebars are unnecessary in print, so hide them with a print stylesheet. Copy screen.css and name the copy print.css, then adjust it. This is often easier than starting from scratch. These declarations in screen.css were changed for print.css:

#sub_banner {
  background-color: #ccc;
  border-bottom: solid 1px #999;
  font-size:.8em;
  font-style: italic;
  padding: 3px 0 3px 5px;
}
#nav1 {
  position: absolute;
  width: 30%;
  left: 60%;
  top: 100px;
  padding: 5px 5px px 5px 0;
}
#nav2 {
  position: absolute;
  width: 15%;
  left: 1%;
  top: 100px;
  padding: 5px 5px px 5px 0;
}
h1 {
  text-align: left;
  color: #fff;
  font-size: 1.2em;
  text-align: left;
  margin-bottom: 5px;
  margin-top: 5px;
}
.entry {
  padding-bottom: 20px;
  padding: 5px;
  border: solid 1px #999;
  background-color: #fcfcfc;
  margin-bottom: 25px;
}

Print view without print.css

Figure: Print view without print.css.

The next figure shows the adjusted file print.css:

#sub_banner {
  display: none
}
#nav1 {
  display: none
}
#nav2 {
  display: none
}
h1 {
  display: none
}
.entry {
  padding: 5px;
}

Adding print.css makes the page more printer-friendly

Figure: Creating and attaching print.css makes the page more printer-friendly.

This hides the contents element marked sub_banner and both navigation columns. The initial element h1 is also unnecessary, saving more space. Entries appear in gray boxes on screen, consuming much ink in print. Simplify them to use only margins between entries.

Remember to include the element link in the HTML:

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

That is all. CSS simplifies many things, including media-specific design. This table lists the current media types in CSS 2.1.

Table: CSS 2.1 media types.

Media type

Output medium

all

All output devices

aural

Speech output devices

braille

Braille displays and other direct Braille devices

embossed

Braille printers

handheld

Handheld devices such as PDAs and smartphones

print

Printers and print previews

projection

Projectors and similar devices

screen

Color monitors

tty

Fixed-width devices such as terminals, teletypes and portable devices with limited character displays

tv

Televisions and WebTV

See also

  

 

 

 

back to top