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 zero or more expressions that
check for the conditions of particular media features. 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/.

The W3C Membership and other interested parties are invited to review
the document and send comments through 23 May 2012. The (archived) public
mailing list
www-style@w3.org (see instructions) is preferred for
discussion of this specification. When sending e-mail, please put the text
“css3-mediaqueries” in the subject, preferably like this:
“[css3-mediaqueries] …summary of
comment…”. Advisory Committee
Representatives should consult their WBS questionnaires.
Note that
substantive technical comments were expected during the Last Call
review period that ended 21 November 2008.

Publication as a Proposed 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.

1. Background

(This section is not normative.)

HTML4 [HTML401]
and CSS2 [CSS21]
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:

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,
deprecates ‘aural’ and adds
‘embossed’ and ‘speech’. 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:

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"

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.

However, the parsing rules for media queries are incompatible with those
of HTML4 so that they are consistent with those of media queries used in
CSS.

HTML5 [HTML5] (at the moment of writing
still work in progress) references the Media Queries specification
directly and thus updates the rules for HTML.

2. Media Queries

A media query consists of a media type and zero or more expressions that check for the
conditions of particular media
features.

Statements regarding media queries in this section assume the syntax section is followed. Media queries that do not
conform to the syntax are discussed in the error
handling section. I.e. the syntax takes precedence over requirements
in this section.

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 (as defined in the
"Applies to" line), and all expressions in the media query are true.

A shorthand syntax is offered for media queries that apply to all media
types; the keyword ‘all’ can be left
out (along with the trailing ‘and’).
I.e. if the media type is not explicitly given it is ‘all’.

Several media queries can be combined in a media query list. A
comma-separated list of media queries. If one or more of the media queries
in the comma-separated list are true, the whole list is true, and
otherwise false. In the media queries syntax, the comma expresses a
logical OR, while the ‘and’ keyword
expresses a logical AND.

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) { … }

If the media query list is empty (i.e. the declaration is the empty
string or consists solely of whitespace) it evaluates to true.

I.e. these are equivalent:

@media all { … }
@media { … }

The logical NOT can be expressed through the ‘not’ keyword. The presence of the keyword
‘not’ at the beginning of the media
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.

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.

User agents are expected, but not required, to re-evaluate
and re-layout the page in response to changes in the user environment, for
example if the device is tilted from landscape to portrait mode.

3. Syntax

The media query syntax is described in terms of the CSS2 grammar. As such,
rules not defined here are defined in CSS2. The
media_query_list production defined below replaces the
media_list production from CSS2. [CSS21]

In this example, the first media query will be represented as
"not all" and evaluate to false and the second media query
is evaluated as if the first had not been specified, effectively.

@media (min-orientation:portrait) { … }

Is represented as "not all" because the ‘orientation’ feature does not accept the
‘min-’ prefix.

Unknown media feature values. As with unknown media
features, user agents are to represent a media query as "not
all" when one of the specified media feature values is not known.

The media query (color:20example) specifies an unknown
value for the ‘color’ media feature
and is therefore represented as "not all".

This media query is represented as "not all" because
negative lengths are not allowed for the ‘width’ media feature:

@media (min-width: -100px) { … }

Malformed media query. User agents are to handle
unexpected tokens encountered while parsing a media query by reading
until the end of the media query, while observing the rules for
matching pairs of (), [], {}, "", and '', and correctly
handling escapes. Media queries with unexpected tokens are represented
as "not all". [CSS21]

The following is an malformed media query because having no space
between ‘and’ and the expression is
not allowed. (That is reserved for the functional notation syntax.)

@media all and(color) { … }

Media queries are expected to follow the error handling rules of the
host language as well.

@media test;,all { body { background:lime } }

… will not apply because the semicolon terminates the
@media rule in CSS.

4. 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:

Properties are used in declarations to give information about
how to present a document. Media features are used in
expressions to describe requirements of the output device.

Most media features accept optional ‘min-’ or ‘max-’
prefixes to express "greater or equal to" and "smaller or equal to"
constraints. This syntax is used to avoid "<" and ">" characters
which may conflict with HTML and XML. Those media features that accept
prefixes will most often be used with prefixes, but can also be used
alone.

Properties always require a value to form a declaration. Media
features, on the other hand, can also be used without a value. For a
media feature feature, (feature) will
evaluate to true if (feature:x) will
evaluate to true for a value x other than zero or zero
followed by a unit identifier (i.e., other than 0,
0px, 0em, etc.). Media features that are
prefixed by min/max cannot be used without a value. When a media feature
prefixed with min/max is used without a value it makes the media query
malformed.

Properties may accept more complex values, e.g., calculations that
involve several other values. Media features only accept single values:
one keyword, one number, or a number with a unit identifier. (The only
exceptions are the ‘aspect-ratio’ and
‘device-aspect-ratio’ 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.

4.1. width

The ‘width’ media feature describes
the width of the targeted display area of the output device. For
continuous media, this is the width of the viewport (as described by CSS2,
section 9.1.1 [CSS21]) including the size of a
rendered scroll bar (if any). For paged media, this is the width of the
page box (as described by CSS2, section 13.2 [CSS21]).

A specified <length> cannot be negative.

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

4.2. height

The ‘height’ media feature describes
the height of the targeted display area of the output device. For
continuous media, this is the height of the viewport including the size of
a rendered scroll bar (if any). For paged media, this is the height of the
page box.

A specified <length> cannot be negative.

4.3. device-width

The ‘device-width’ media feature
describes the width of the rendering surface of the output device. For
continuous media, this is the width of the screen. For paged media, this
is the width of the page sheet size.

A specified <length> cannot be negative.

@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.

4.4. device-height

The ‘device-height’ media feature
describes the height of the rendering surface of the output device. For
continuous media, this is the height of the screen. For paged media, this
is the height of the page sheet size.

A specified <length> cannot be negative.

<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.

4.8. color

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 is zero.

A specified <integer> cannot be negative.

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.

4.11. resolution

The ‘resolution’ media feature
describes the resolution of the output device, i.e. the density of the
pixels. When querying devices with non-square pixels, in ‘min-resolution’ queries the least-dense dimension
must be compared to the specified value and in ‘max-resolution’ queries the most-dense dimensions
must be compared instead. A ‘resolution’ (without a "min-" or "max-" prefix)
query never matches a device with non-square pixels.

For printers, this corresponds to the screening resolution (the
resolution for printing dots of arbitrary color).

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:

4.13. grid

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.

5. Values

The <ratio> value is a positive (not zero or negative) <integer>
followed by optional whitespace, followed by a solidus (‘/’), followed by optional whitespace, followed by a
positive <integer>.

The <resolution> value is a positive <number> immediately followed
by a unit identifier (‘dpi’ or
‘dpcm’).

Whitespace, <integer>, <number> and other values used by this
specification are the same as in other parts of CSS, normatively defined
by CSS 2.1. [CSS21]

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.

Relative units in media queries are based on the initial value, which
means that units are never based on results of declarations. For example,
in HTML, the ‘em’ unit is relative to
the initial value of ‘font-size’.

6.1. Resolution

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

dpi

dots per CSS ‘inch’

dpcm

dots per CSS ‘centimeter’

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

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

Section 6: Clarify that units are never based on
the results of declarations.

Relative units in media queries are based on the initial value,
which means that units are never based on results of
declarations. For example, in HTML, the ‘em’ unit is relative to the initial value of
‘font-size’.

Acknowledgments

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