W3C

Media Queries

W3C Candidate Recommendation 6 June 2007

This version:
http://www.w3.org/TR/2007/CR-css3-mediaqueries-20070606
Latest version:
http://www.w3.org/TR/css3-mediaqueries
Previous version:
http://www.w3.org/TR/2002/CR-css3-mediaqueries-20020708
Editors:
Håkon Wium Lie <>
Tantek Çelik <>
Daniel Glazman <>

Abstract

HTML4 and CSS2 currently support media-dependent style sheets tailored for different media types. For example, a document may use sans-serif fonts when displayed on a screen and serif fonts when printed. "Screen" and "print" are two media types that have been defined. Media Queries extend the functionality of media types by allowing more precise labeling of style sheets.

A Media Query consists of a media type and one or more expressions to limit the scope of style sheets. Among the media features that can be used in media queries are "width", "height", and "color". By using Media Queries, presentations can be tailored to a specific range of output devices without changing the content itself.

Status of this document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at http://www.w3.org/TR/.

This document was produced by the CSS Working Group as a Candidate Recommendation.

A Candidate Recommendation is a document that has been widely reviewed and ready for implementation. W3C encourages everybody to implement this specification and return comments to the (archived) public mailing list www-style@w3.org (see instructions). When sending e-mail, please put the text “css3-mediaqueries” in the subject, preferably like this: “[css3-mediaqueries] …summary of comment…

Publication as a Candidate Recommendation does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

This document is governed by the 24 January 2002 CPP as amended by the W3C Patent Policy Transition Procedure. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

For this specification to exit the CR stage, the following conditions shall be met:

  1. There must be at least two interoperable implementations. For the purposes of this criterion, we define the following terms:

    interoperable

    passing the respective test case(s) in the CSS test suite, or, if the implementation is not a Web browser, an equivalent test. Every relevant test in the test suite should have an equivalent test created if such a user agent (UA) is to be used to claim interoperability. In addition if such a UA is to be used to claim interoperability, then there must one or more additional UAs which can also pass those equivalent tests in the same way for the purpose of interoperability. The equivalent tests must be made publicly available for the purposes of peer review.

    implementation

    a user agent which:

    1. implements the specification.
    2. is available (i.e. publicly downloadable or available through some other public point of sale mechanism). This is the "show me" requirement.
    3. is shipping (i.e. development, private or unofficial versions are insufficient).
    4. is not experimental (i.e. is intended for a wide audience and could be used on a daily basis.)
  2. A minimum of another three months of the CR period must elapse. That is, this specification will not exit CR before 06 September 2007. When the specification exits CR, an implementation report will be published. At this point, no such report exists.

  3. The specified technology must not be harmful for accessibility.

W3C Members can also send comments directly to the CSS Working Group. The comments received on the last working draft are still available.

Table of contents

1. Dependencies on other modules

This CSS3 module has normative references to the following other CSS3 modules:

2. Background

(This section is not normative.)

HTML4 [HTML401] and CSS2 [CSS2] currently support media-dependent style sheets tailored for different media types. For example, a document may use different style sheets for screen and print. In HTML4, this can be written as:

<link rel="stylesheet" type="text/css" media="screen" href="sans-serif.css">
<link rel="stylesheet" type="text/css" media="print" href="serif.css">

Inside a CSS style sheet, one can declare that sections apply to certain media types:

@media screen {
  * { font-family: sans-serif }
}

The "print" and "screen" media types are defined in HTML4. The complete list of media types in HTML4 is: "aural", "braille", "handheld", "print", "projection", "screen", "tty", "tv". CSS2 defines the same list with the addition of "embossed" to differentiate between braille tactile feedback devices and braille printers. Also, "all" is used to indicate that the style sheet applies to all media types.

Media-specific style sheets are supported by several user agents. The most commonly used feature is to distinguish between "screen" and "print".

There have been requests for ways to describe in more detail what type of output devices a style sheet applies to. Fortunately HTML4 foresaw these requests and defined a forward-compatible syntax for media types. Here is a quote from HTML4, section 6.13:

Future versions of HTML may introduce new values and may allow parameterized values. To facilitate the introduction of these extensions, conforming user agents must be able to parse the media attribute value as follows:

  1. The value is a comma-separated list of entries. For example,
    media="screen, 3d-glasses, print and resolution > 90dpi"
    

    is mapped to:

    "screen"
    "3d-glasses"
    "print and resolution > 90dpi"
    
  2. Each entry is truncated just before the first character that isn't a US ASCII letter [a-zA-Z] (Unicode decimal 65-90, 97-122), digit [0-9] (Unicode hex 30-39), or hyphen (45). In the example, this gives:
    "screen"
    "3d-glasses"
    "print"
    

Media Queries, as described in this specification, build on the mechanism outlined in HTML4. The syntax of Media Queries fit into the media type syntax reserved in HTML4. The media attribute of HTML4 also exists in XHTML and generic XML. The same syntax can also be used inside in the ‘@media’ and ‘@import’ rules of CSS.

3. Media Queries

A Media Query consists of a media type and one or more expressions involving media features.

Here is a simple example written in HTML:

<link rel="stylesheet" media="screen and (color)" href="example.css" />

This example expresses that a certain style sheet (example.css) applies to devices of a certain media type (‘screen’) with certain feature (it must be a color screen).

Here the same media query written in an @import-rule in CSS:

@import url(color.css) screen and (color);

A Media Query is a logical expression that is either true or false. A Media Query is true if the media type of the Media Query matches the media type of the device where the user agent is running, and all expressions in the Media Query are true. Also, a Media Query which is otherwise false becomes true if the "not" keyword is present.

When a Media Query is true, the corresponding style sheet is applied as per the normal cascading rules.

Several Media Queries can be combined in a comma-separated list. If one or more of the Media Queries in the comma-separated list is true, the associated style sheet is applied, otherwise the associated style sheet is ignored.

Here is an example of several media queries in a comma-separated list using the an @media-rule in CSS:

@media screen and (color), projection and (color) { ... }

In the Media Queries syntax, the comma expresses a logical OR, while the ‘and’ keyword expresses a logical AND.

The logical NOT can be expressed through the ‘not’ keyword.

<link rel="stylesheet" media="not screen and (color)" href="example.css" />

The presence of the keyword ‘not’ at the beginning of the query negates the result. I.e., if the Media Query had been true without the ‘not’ keyword it will become false, and vice versa. User agents that only support media types (as described in HTML4) will not recognize the ‘not’ keyword and the associated style sheet is therefore not applied.

The keyword ‘only’ can also be used to hide style sheets from older user agents. User agents must process media queries starting with ‘only’ as if the ‘only’ keyword was not present.

<link rel="stylesheet" media="only screen and (color)" href="example.css" />

The Media Queries syntax can be used with HTML, XHTML, XML [XMLSTYLE] and the @import and @media rules of CSS.

Here is the same example written in HTML, XHTML, XML, @import and @media:

<link rel="stylesheet" media="screen and (color), projection and (color)" rel="stylesheet" href="example.css">

<link rel="stylesheet" media="screen and (color), projection and (color)" rel="stylesheet" href="example.css" />

<?xml-stylesheet media="screen and (color), projection and (color)" rel="stylesheet" href="example.css" ?>

@import url(example.css) screen and (color), projection and (color);

@media screen and (color), projection and (color) { ... }

If a media feature does not apply to the device where the UA is running, expressions involving the media feature will be false.

The media feature ‘device-aspect-ratio’ only applies to visual devices. On an aural device, expressions involving ‘device-aspect-ratio’ will therefore always be false:

<link rel="stylesheet" media="aural and (device-aspect-ratio: 16/9)" href="example.css" />

Expressions will always be false if the unit of measurement does not apply to the device.

The ‘px’ unit does not apply to ‘tty’devices so the following media query is always false:

<link rel="stylesheet" media="tty and (min-device-width: 800px)" href="example.css" />

Note that the media queries in this example would have been true if the keyword ‘not’ had been added to the beginning of the media query.

To avoid circular dependencies, it is never necessary to apply the style sheet in order to evaluate expressions. For example, the aspect ratio of a printed document may be influenced by a style sheet, but expressions involving ‘device-aspect-ratio’ will be based on the default aspect ratio of the user agent.

4. Syntax

Here is a pseudo-BNF definition of the syntax for media queries:

media_query_list: <media_query> [, <media_query> ]*
media_query: [[only | not]? <media_type> [ and <expression> ]*] 
  | <expression> [ and <expression> ]*
expression: ( <media_feature> [: <value>]? )
media_type: all | aural | braille | handheld | print | 
  projection | screen | tty | tv | embossed
media_feature: width | min-width | max-width 
  | height | min-height | max-height
  | device-width | min-device-width | max-device-width
  | device-height | min-device-height | max-device-height
  | device-aspect-ratio | min-device-aspect-ratio | max-device-aspect-ratio
  | color | min-color | max-color
  | color-index | min-color-index | max-color-index
  | monochrome | min-monochrome | max-monochrome 
  | resolution | min-resolution | max-resolution
  | scan | grid

CSS style sheets are generally case-insensitive, and this is also the case for Media Queries.

The parentheses ("()") around expressions are required. A user agent conforming to this specification must not process expressions without the parentheses. By following this requirement, there will be room for future extensions in the syntax.

Media queries involving unknown media types are always false.

Consider this example:

<link rel="stylesheet" media="3d and (color)" href="example.css" />

Because "3d" is an unknown type, the media query is false and the associated style sheet will not be applied.

Expressions involving unknown media features or unknown/illegal values are always false.

Consider this example:

<link rel="stylesheet" media="all and (max-weight: 3kg) and (color)" href="example.css" />

Because "max-weight" is an unknown media feature, the media query is false and the associated style sheet will not be applied.

If one media query in a comma-separated list is rejected, the other media queries in the list are evaluated as if the rejected media query had not been there.

<link rel="stylesheet" media="screen and (max-weight: 3kg) and (color), (color)" href="example.css" />

In this example, the first media query is rejected due to an unknown media feature and unknown value, but the second media query is evaluated as if the first had not been specified.

5. Media features

Syntactically, media features resemble CSS properties: they have names and accept certain values. There are, however, several important differences between properties and media features:

For example, the ‘color’ media feature can form expressions without a value (‘(color)’), or with a value (‘(min-color: 1)’).

This specification defines media features usable with visual and tactile devices. Similarly, media features can be defined for aural media types.

5.1. width

Value: <length>
Applies to: visual and tactile media types
Accepts min/max prefixes: yes

The ‘width’ media feature describes the width of the rendering surface of the output device. For continuous media, this is the width of the viewport (as described by CSS2, section 9.1.1 [CSS2]). For paged media, this is the width of the page box (as described by CSS2, section 13.2 [CSS2]).

For example, this Media Query expresses that the style sheet is usable on printed output wider than 25cm:

<link rel="stylesheet" media="print and (min-width: 25cm)" href="http://...." />

This Media Query expresses that the style sheet is usable on devices with viewport (the part of the screen/paper where the document is rendered) widths between 400 and 700 pixels:

@media screen and (min-width: 400px) and (max-width: 700px) { ... }

This Media Query expresses that style sheet is usable on screen and handheld devices if the width of the viewport is greater than 20em.

@media handheld and (min-width: 20em), 
  screen and (min-width: 20em) { ... }

The ‘em’ value is relative to the font size of the root element.

5.2. height

Value: <length>
Applies to: visual and tactile media types
Accepts min/max prefixes: yes

The ‘height’ media feature describes the height of the rendering surface of the output device. For continuous media, this is the height of the viewport. For paged media, this is the height of the page box.

5.3. device-width

Value: <length>
Applies to: visual and tactile media types
Accepts min/max prefixes: yes

The ‘device-width’ media feature describes the width of the output device.

@media screen and (device-width: 800px) { ... }

In the example above, the style sheet will apply only to screens that currently displays exactly 800 horizontal pixels. The ‘px’ unit is of the logical kind, as described in the Units section.

5.4. device-height

Value: <length>
Applies to: visual and tactile media types
Accepts min/max prefixes: yes

The ‘device-height’ media feature describes the height of the output device.

<link rel="stylesheet" media="screen and (device-height: 600px)" />

In the example above, the style sheet will apply only to screens that have exactly 600 vertical pixels. Note that the definition of the ‘px’ unit is the same as in other parts of CSS.

5.5. device-aspect-ratio

Value: <integer> / <integer>
Applies to: bitmap media types
Accepts min/max prefixes: yes

The ‘device-aspect-ratio’ media feature describes the aspect ratio of the output device. The value consists of two positive integers separated by a ‘/’.

In this specification, aspect ratio is defined as the number of horizontal pixels over the number of vertical pixels.

For example, if a screen device has 1280 horizontal pixels and 720 vertical pixels (commonly referred to as "16:9"), the following Media Queries will all match the device:

@media screen and (device-aspect-ratio: 16/9) { ... }
@media screen and (device-aspect-ratio: 32/18) { ... }
@media screen and (device-aspect-ratio: 1280/720) { ... }
@media screen and (device-aspect-ratio: 2560/1440) { ... }

Note that determining if a specified aspect ratio matches the device aspect ratio can be done through multiplication (e.g., 1280*9 = 11520 = 720*16) to avoid floating point operations.

Using prefixes, portrait and landscape aspect ratios can be expressed:

@media screen and (max-device-aspect-ratio: 1/1) /* portrait or square */
@media screen and (min-device-aspect-ratio: 1/1) /* landscape or square */

5.6. color

Value: <integer>
Applies to: visual media types
Accept min/max prefixes: yes

The ‘color’ media feature describes the number of bits per color component of the output device. If the device is not a color device, the value will be 0.

For example, these two media queries express that a style sheet applies to all color devices:

@media all and (color) { ... }
@media all and (min-color: 1) { ... }

This Media Query expresses that a style sheet applies to color devices with 2 or more bits per color component:

@media all and (min-color: 2) { ... }

If different color components are represented by different number of bits, the smallest number is used.

For instance, if an 8-bit color system represents the red component with 3 bits, the green component with 3 bits and the blue component with 2 bits, the ‘color’ media feature will have a value of 2.

In a device with indexed colors, the minimum number of bits per color component in the lookup table is used.

The described functionality is only able to describe color capabilities at a superficial level. If further functionality is required, RFC2531 [RFC2531] provides more specific media features which may be supported at a later stage.

5.7. color-index

Value: <integer>
Applies to: visual media types
Accepts min/max prefixes: yes

The ‘color-index’ media feature describes the number of entries in the color lookup table of the output device.

For example, here are two ways to express that a style sheet applies to all color index devices:

@media all and (color-index) { ... }
@media all and (min-color-index: 1) { ... }

This Media Query expresses that a style sheet applies to a color index device with 256 or more entries:

<?xml-stylesheet media="all and (min-color-index: 256)" 
  href="http://www.example.com/..." ?>

5.8. monochrome

Value: <integer>
Applies to: visual media types
Accepts prefixes: yes

The ‘monochrome’ media feature describes the number of bits per pixel in a monochrome frame buffer. If the device is not a monochrome device, the output device value will be 0.

For example, here are two ways to express that a style sheet applies to all monochrome devices:

@media all and (monochrome) { ... }
@media all and (min-monochrome: 1) { ... }

Express that a style sheet applies to monochrome devices with more than 2 bits per pixels:

@media all and (min-monochrome: 2) { ... }

Express that there is one style sheet for color pages and another for monochrome:

<link rel="stylesheet" media="print and (color)" href="http://..." />
<link rel="stylesheet" media="print and (monochrome)" href="http://..." />

5.9. resolution

Value: <resolution>
Applies to: bitmap media types
Accepts min/max prefixes: yes

The ‘resolution’ media feature describes the resolution of the output device, i.e. the density of the pixels.

For example, this Media Query expresses that a style sheet is usable on devices with resolution greater than 300 dots per inch:

@media print and (min-resolution: 300dpi) { ... }

This Media Query expresses that a style sheet is usable on devices with resolution greater than 118 dots per centimeter:

@media print and (min-resolution: 118dpcm) { ... }

5.10. scan

Value: progressive | interlace
Applies to: "tv" media types
Accepts min/max prefixes: no

The ‘scan’ media feature describes the scanning process of "tv" output devices.

For example, this Media Query expresses that a style sheet is usable on tv devices with progressive scanning:

@media tv and (scan: progressive) { ... }

5.11. grid

Value: <integer>
Applies to: all media types
Accepts min/max prefixes: no

The ‘grid’ media feature is used to query whether the output device is grid or bitmap. If the output device is grid-based (e.g., a "tty" terminal, or a phone display with only one fixed font), the value will be 1. Otherwise, the value will be 0.

The ‘em’ unit has a special meaning in grid output devices. Since the exact with of an ‘em’ cannot be determined, one ‘em’ is assumed to be the width of one grid cell horizontally and the height of one grid cell vertically.

Here are two examples:

@media handheld and (grid) and (max-width: 15em) { ... }
@media handheld and (grid) and (device-max-height: 7em) { ... }

6. Units

The units used in Media Queries are the same as in other parts of CSS. For example, the pixel unit represents CSS pixels and not physical pixels.

Note, however, that the ‘em’ unit has a special meaning on grid output devices, as discussed above.

Relative units in Media Queries are based on the initial value of the document's root element. For example, in HTML, the ‘em’ unit is relative to the initial value of ‘font-size’ on the html element.

6.1. Resolution

The ‘dpi’ and ‘dpcm’ units describe the resolution of an output device, i.e., the density of pixels. Resolution unit identifiers are:

dpi
dots per inch
dpcm
dots per centimeter

In this specification, these units are only used in the ‘resolution’ media feature.

Acknowledgments

This specification is the product of the W3C Working Group on Cascading Style Sheets.

Comments from Christoph Päper, Simon Pieters, Anne van Kesteren, Sigurd Lerstad, Arve Bersvendsen, Susan Lesch, Melinda Grant, and L. David Baron improved this specification.

References

Normative references

[CSS2]
Bert Bos; et al. Cascading Style Sheets, level 2 (CSS2) Specification. 12 May 1998. W3C Recommendation. URL: http://www.w3.org/TR/1998/REC-CSS2-19980512
[CSS3PAGE]
Håkon Wium Lie; Melinda Grant. CSS3 Paged Media Module. 25 February 2004. W3C Candidate Recommendation. (Work in progress.) URL: http://www.w3.org/TR/2004/CR-css3-page-20040225
[CSS3VAL]
Håkon Wium Lie; Chris Lilley. CSS3 module: Values and Units. 13 July 2001. W3C Working Draft. (Work in progress.) URL: http://www.w3.org/TR/2001/WD-css3-values-20010713
[HTML401]
David Raggett; Arnaud Le Hors; Ian Jacobs. HTML 4.01 Specification. 24 December 1999. W3C Recommendation. URL: http://www.w3.org/TR/1999/REC-html401-19991224
[XMLSTYLE]
James Clark. Associating Style Sheets with XML documents. 29 June 1999. W3C Recommendation. URL: http://www.w3.org/1999/06/REC-xml-stylesheet-19990629

Other references

Index