< Day Day Up > |
Recipe 8.1 Creating a Printer-Friendly PageProblemYou want to create a printer-friendly page without having to manually or dynamically generate another web page. SolutionCreate 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" /> DiscussionYou 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. 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 AlsoMedia Types in Section 7 of the CSS 2.1 Working Draft, http://www.w3.org/TR/CSS21/media.html. |
< Day Day Up > |