Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

Defining CSS Media Types


WebReview.com: December 1, 2000: Columns: Tag of the Week: Defining CSS Media Types

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.

Media Types

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 <i>types</i> {
        <i>style rules</i>
        }

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
This media type indicates that the included style rules are suitable for all devices.

aural
The document is being "rendered" by a speech synthesizer or similar audio device.

braille
The document is being presented on a braille tactile feedback device.

embossed
The document is being printed on a braille printer.

handheld
The document is being rendered on a handheld device, with presumably limited display capabilities and bandwidth.

print
The document is being rendered by a printer.

projection
The document is intended for some form of projected presentation, either through a projector or as paged transparencies.

screen
The document will be displayed on a color computer screen.

tty
The document is being presented on a device with a fixed-pitch character grid such as a teletype or character-based display.

tv
The document will be displayed on a television, with a presumably lower resolution color display.

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.

Media Types and Imported Style Sheets

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.


Previously in Tag of the Week

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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.