January 01, 2002
URL:http://drdobbs.com/defining-css-media-types/184411838
In the beginning, HTML was intended to be a "media-free" document markup language. The idea was that authors would create documents with appropriate markup, and the browser would interpret the markup in ways that best suited the target media. Documents might display in a graphical browser one way, be printed in another, and be displayed on a line-based device in yet another. Needless to say, this naive view of the world has long since disappeared.
It is now recognized that HTML documents will be rendered on a variety of media, that those media may be very different, and that the document should present well in every possible media. Since authors are disinclined to let the browser decide what constitutes "present well" for a given media, the CSS style sheet model has been extended to let authors define styles for each type of media. This lets authors craft very specific styles for all those different media types, guaranteeing that each method of presenting the document is exactly what the author intended.
Within CSS2, you can use the @media
rule to define which style rules are applied for each media type. The general syntax of this rule is:
@media types { style rules }
You should replace types
with a comma-separated list of media types. When the document is processed by the browser, the associated style rules will only be used if the document is being rendered for one of the specified media types.
You cannot specify just any media type. Instead, CSS2 defines a specific set of media types that all conforming browsers are expected to understand. These types include:
all
aural
braille
embossed
handheld
print
projection
screen
tty
tv
Keep in mind that this list of media types will probably be extended in the future. To play it safe, you should use only these media types when defining style sheets for your documents.
It is possible to control imported style sheets using media types. As you may recall, you can use the @import
rule to import style rules contained in a separate style sheet for use in the current document. Normally, the @import
rule accepts a single parameter, the URL of the desired style sheet.
If you follow the URL with one or more media types, the external sheet will only be loaded if the document is being rendered for one of those types. For example, to include a sheet only if it is to be rendered by handheld devices, you could say:
@import url('palm-styles.css') handheld;
In general, you may want to use this method of media type control if you are defining a large set of rules for a specific media type. If you need only tweak one or two rules for a media type, the @media
rule is probably more useful.
Next: CSS2: Lists, Bidirectional Text, and Quotation Marks
Chuck is the author of the best-selling HTML: The Definitive Guide and now, the fourth and expanded edition, HTML & XHTML: The Definitive Guide. He also writes on a variety of Internet and Web-related topics for a number of online magazines.
CSS2 Standard Tour: Begin Here
Attribute Sectors in One Quick Bite
Print This!
Using CSS2 to Layout HTML Pages for Print
CSS2 Printing: Left and Right Layouts
Even More CSS2 Printing Power
Breaking Pages
Of related interest: See the Web Review Style Guide, edited by Eric Meyer.
Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.