CSS Cookbook: How to make a printer-friendly page with a style sheet

This article is excerpted from CSS Cookbook, by Christopher Schmitt, with permission of O'Reilly Media Inc. All rights reserved.

In this sample design, you will transform an existing Web document (like the one in Figure 10-3) to make it more suitable for print.

Figure 10-3. Web page stylized for screen delivery

Figure 10-3. Web page stylized for screen delivery (click image for larger view)

Although CSS has changed the way we design for the Web, it also has allowed developers to change the way they provide printer-friendly versions of their documents. Instead of having to create separate pages or write scripts, you can use CSS to create a printer-friendly document as soon as the user hits the Print button. The HTML for the page isn’t in this book because the miracle of CSS lets us change the presentation without having to change the HTML.

When you create a style sheet for print, you actually use a Web browser. This enables you to see quickly how the CSS rules affect the display of the document (just like for media delivery), but it’s also easier on the environment and you save money by not wasting ink in the printer. So, comment out the style sheet used for the screen in order to create new CSS rules:

<!-- Hide screen media CSS while
 working on print CSS -->
<!-- link href="adv.css"

 rel="stylesheet" media="screen" -->
<style type="text/css">
/* Print CSS rules go here */

Setting the Page for Black-and-White Printing

Apply the first CSS rule to the body element. In this rule, set the background color to white and set the type to black:

body {
 background-color: white;
 color: black;
1 2 3 4 5 6 Page 1
Page 1 of 6
Bing’s AI chatbot came to work for me. I had to fire it.
Shop Tech Products at Amazon