ProblemYou want to create a printer-friendly page without having to generate another web page manually or dynamically. SolutionFirst, create a separate style sheet containing CSS rules that dictate the desired look when a page is printed. For this example, the style sheet with print-only CSS rules is named print.css. 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" /> 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 10-1.
When defining the styles for your web page, one style sheet can be used 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, 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. Using @import when assigning media typesYou 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 URI(print.css) print,projection; The @import rule needs to be placed within a style element or within an external style sheet. Using @media when assigning media typesAnother 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 Also"Media Types" in Section 7 of the CSS 2.1 Working Draft, http://www.w3.org/TR/CSS21/media.html. |