Group: csswg Shortname: mediaqueries Level: 4 Status: ED Work Status: Exploring ED: http://dev.w3.org/csswg/mediaqueries4/ TR: http://www.w3.org/TR/mediaqueries-4/ Editor: Florian Rivoal, Invited Expert, florian@rivoal.net, http://florian.rivoal.net Editor: Tab Atkins Jr., Google, http://xanthir.com/contact/ Former Editor: Håkon Wium Lie, Opera, howcome@opera.com Former Editor: Tantek Çelik, Mozilla, tantek@cs.standard.edu Former Editor: Daniel Glazman, Disruptive Innovations, daniel@glazman.org Former Editor: Anne van Kesteren, Mozilla, annevk@annevk.nl Abstract: Media Queries allow authors to test and query values or features of the user agent or display device, independent of the document being rendered. They are used in the CSS @media rule to conditionally apply styles to a document, and in various other contexts and languages, such as HTML and Javascript. Abstract: Abstract: Media Queries Level 4 describes the mechanism and syntax of media queries, media types, and media features. It extends and supersedes the features defined in Media Queries Level 3. Ignored Terms: min-resolution, max-resolution, none, view-mode, mediaText, DOMString Link Defaults: css-break-3 (property) break-inside
<link rel="stylesheet" type="text/css" media="screen" href="style.css"> <link rel="stylesheet" type="text/css" media="print" href="print.css">
@media screen {
* { font-family: sans-serif }
}
Similarly, stylesheets can be conditionally imported based on media queries:
@import "print-styles.css" print;
<link media="screen and (color), projection and (color)"
rel="stylesheet" href="example.css">
<link 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) { … }
Note: The [[XMLSTYLE]] specification has not yet been updated to
use media queries in the media pseudo-attribute.
Or: N: media condition And: Or: 1 T: only S: T: not N: media type Opt: And: T: and N: media conditionA media query is a logical expression that is either true or false. A media query is true if: * the media type, if specified, matches the media type of the device where the user agent is running, and * the media condition is true. 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 [[#error-handling]]. I.e. the syntax takes precedence over requirements in this section.
<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 is the same media query written in an @import-rule in CSS:
@import url(example.css) screen and (color);
Star: N: media query T: ,A media query list is true if any of its component media queries are true, and false only if all of its component media queries are false.
@media screen and (color), projection and (color) { … }
@media all { … }
@media { … }
<link rel="stylesheet" media="not screen and (color)" href="example.css" />
<link> element
will not be used by legacy user agents,
even if they would normally match the ''screen'' media type.
<link rel="stylesheet" media="only screen and (color)" href="example.css" />
media attribute on <link> elements.
Unfortunately, media types have proven insufficient as a way of discriminating between devices with different styling needs.
Some categories which were originally quite distinct,
such as ''screen'' and ''handheld'',
have blended significantly in the years since their invention.
Others, such as ''tty'' or ''tv'',
expose useful differences from the norm of a full-featured computer monitor,
and so are potentially useful to target with different styling,
but the definition of media types as mutually exclusive
makes it difficult to use them in a reasonable manner;
instead, their exclusive aspects are better expressed as media features
such as 'grid' or 'scan'.
As such, the following media types are defined for use in media queries:
T: ( Choice: And: N: feature name T: : N: feature value N: feature name And: N: range form C: (see below) T: )There are, however, several important differences between properties and media features:
<link media="speech and (device-aspect-ratio: 16/9)" rel="stylesheet" href="example.css">
T: ( Choice: Seq: N: feature name/value Choice: 4 T: = T: < T: <= T: > T: >= N: feature value/name Seq: N: value Choice: T: < T: <= N: feature name Choice: T: < T: <= N: value Seq: N: value Choice: T: > T: >= N: feature name Choice: T: > T: >= N: value T: )The basic form, consisting of a feature name, a comparison operator, and a value, returns true if the relationship is true.
<media-query> = <The <> | [ not | only ]? < > [ and < > ]? <media-type> = < > <media-condition> = < > | < > | < > | < > <media-condition-without-or> = < > | < > | < > <media-not> = not < > <media-and> = < > [ and < > ]+ <media-or> = < > [ or < > ]+ <media-in-parens> = ( < > ) | < > | < > <media-feature> = ( [ < > | < > | < > ] ) <mf-plain> = < > : < > <mf-boolean> = < > <mf-range> = < > [ '<' | '>' ]? '='? < > | < > [ '<' | '>' ]? '='? < > | < > '<' '='? < > '<' '='? < > | < > '>' '='? < > '>' '='? < > <mf-name> = < > <mf-value> = < > | < > | < > | < > <general-enclosed> = [ < > < > ) ] | ( < > < > )
@media all { body { background:lime } }
@media example { body { background:red } }
@media (example, all,), speech { /* only applicable to speech devices */ }
@media &test, speech { /* only applicable to speech devices */ }
Both of the above media query lists are turned into ''not all, speech'' during parsing,
which has the same truth value as just ''speech''.
Note that error-recovery only happens at the top-level of a media query;
anything inside of an invalid parenthesized block
will just get turned into ''not all'' as a group.
For example:
@media (example, speech { /* rules for speech devices */ }
Because the parenthesized block is unclosed,
it will contain the entire rest of the stylesheet from that point
(unless it happens to encounter an unmatched ")" character somewhere in the stylesheet),
and turn the entire thing into a ''not all'' media query.
<link media="screen and (max-weight: 3kg) and (color), (color)" rel="stylesheet" href="example.css" />As ''max-weight'' is an unknown media feature, this media query list is turned into ''not all, (color)'', which is equivalent to just ''(color)''.
@media (min-orientation:portrait) { … }
The 'orientation' feature does not accept prefixes,
so this is considered an unknown media feature,
and turned into ''not all''.
@media (min-width: -100px) { … }
@media test;,all { body { background:lime } }
The media query ''test;,all'' is, parsed by itself,
equivalent to ''not all, all'', which is always true.
However, CSS's parsing rules cause the ''@media'' rule,
and thus the media query,
to end at the semicolon.
The remainder of the text is treated as a style rule
with an invalid selector and contents.
Name: width Value: <The '@media/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 <> For: @media Type: range
<link rel="stylesheet" media="print and (min-width: 25cm)" href="http://…" />
@media (400px <= min-width <= 700px) { … }
@media (min-width: 20em) { … }
The ''em'' value is relative to the initial value of 'font-size'.
Name: height Value: <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 <> For: @media Type: range
Name: aspect-ratio Value: <The 'aspect-ratio' media feature is defined as the ratio of the value of the 'width' media feature to the value of the 'height' media feature.> For: @media Type: range
Name: orientation Value: portrait | landscape For: @media Type: discrete
@media (orientation:portrait) { … }
Name: resolution Value: <The 'resolution' media feature describes the resolution of the output device, i.e. the density of the pixels, taking into account the page zoom but assuming a pinch zoom of 1.0. When querying media with non-square pixels, 'resolution' queries the density in the vertical dimension.> For: @media Type: range
@media (resolution >= 2dppx)
@media print and (min-resolution: 300dpi) { … }
This media query is equivalent, but uses the CSS ''cm'' unit:
@media print and (min-resolution: 118dpcm) { … }
Name: scan Value: interlace | progressive For: @media Type: discreteThe 'scan' media feature describes the scanning process of some output devices.
@media (scan: interlace) { body { font-family: sans-serif; } }
Name: grid Value: <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.> For: @media Type: discrete
@media (grid) and (max-width: 15em) { … }
Name: update-frequency Value: none | slow | normal For: @media Type: discreteThe 'update-frequency' media feature is used to query the ability of the output device to modify the apearance of content once it has been rendered. It accepts the following values:
a { text-decoration: none; }
a:hover, a:focus { text-decoration: underline; }
@media (update-frequency: none) {
a { text-decoration: underline; }
}
Name: overflow-block Value: none | scroll | optional-paged | paged For: @media Type: discreteThe 'overflow-block' media feature describes the behavior of the device when content overflows the initial containing block in the block axis.
Name: overflow-inline Value: none | scroll For: @media Type: discreteThe 'overflow-inline' media feature describes the behavior of the device when content overflows the initial containing block in the inline axis.
Name: color Value: <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 <> For: @media Type: range
@media (color) { … }
@media (min-color: 1) { … }
@media (color >= 8) { … }
Name: color-index Value: <The 'color-index' media feature describes the number of entries in the color lookup table of the output device. If the device does not use a color lookup table, the value is zero. A specified <> For: @media Type: range
@media (color-index) { … }
@media (color-index >= 1) { … }
<?xml-stylesheet media="(min-color-index: 256)" href="http://www.example.com/…" ?>
Name: monochrome Value: <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. A specified <> For: @media Type: range
@media (monochrome) { … }
@media (monochrome >= 2) { … }
<link rel="stylesheet" media="print and (color)" href="http://…" /> <link rel="stylesheet" media="print and (monochrome)" href="http://…" />
Name: inverted-colors Value: none | inverted For: @media Type: discreteThe 'inverted-colors' media feature indicates whether the content is displayed normally, or whether colors have been inverted.
This is an indication that the user agent or underlying operating system has forcibly inverted all colors, not a request to do so. This is sometimes provided as a simple accessibility feature, allowing users to switch between light-on-dark and dark-on-light text. However, this has unpleasant side effects, such as inverting pictures, or turning shadows into highlights, which reduce the readability of the content.
@media (inverted-colors) {
img { filter: invert(100%); }
* { text-shadow: none; background-shadow: none; }
}
Name: pointer Value: none | coarse | fine For: @media Type: discreteThe 'pointer' media feature is used to query about the presence and accuracy of a pointing device such as a mouse. If a device has multiple input mechanisms, the 'pointer' media feature must reflect the characteristics of the “primary” input mechanism, as determined by the user agent. (To query the capabilities of any available input mechanism, see the 'any-pointer' media feature.)
| pointer | |||
|---|---|---|---|
| coarse | fine | ||
| hover | none | smartphones, touch screens | stylus-based screens (Cintiq, Wacom, etc) |
| hover | Nintendo Wii controller, Kinect | mouse, touch pad | |
/* Make radio buttons and check boxes larger if we have an inaccurate pointing device */
@media (pointer:coarse) {
input[type="checkbox"], input[type="radio"] {
min-width:30px;
min-height:40px;
background:transparent;
}
}
Name: hover Value: none | on-demand | hover For: @media Type: discreteThe 'hover' media feature is used to query the user's ability to hover over elements on the page. If a device has multiple input mechanisms, the 'hover' media feature must reflect the characteristics of the “primary” input mechanism, as determined by the user agent. (To query the capabilities of any available input mechanism, see the 'any-hover' media feature.)
/* Only use a hover-activated drop down menu on devices that can conveniently hover. */
@media (hover) {
.menu > li {display:inline-block;}
.menu ul {display:none; position:absolute;}
.menu li:hover ul {display:block; list-style:none; padding:0;}
/* ... */
}
Name: any-pointer Value: none | coarse | fine For: @media Type: discrete
Name: any-hover Value: none | on-demand | hover For: @media Type: discreteThe 'any-pointer' and 'any-hover' media features are identical to the 'pointer' and 'hover' media features, but they correspond to the union of capabilities of all the pointing devices available to the user. More than one of their values can match, if different pointing devices have different characteristics.
Name: light-level Value: dim | normal | washed For: @media Type: discreteThe 'light-level' media feature is used to query about the ambient light-level in which the device is used, to allow the author to adjust style of the document in response. The following values are valid:
Using this media feature for accessibility purposes overlaps a lot with the high-contrast media feature proposed by Microsoft. Can we adjust this so that it covers all use cases for both, or somehow modify them to work in an orthogonal, rather than overlapping, fashion?
@media (light-level: normal) {
p { background: url("texture.jpg"); color: #333 }
}
@media (light-level: dim) {
p { background: #222; color: #ccc }
}
@media (light-level: washed) {
p { background: white; color: black; font-size: 2em; }
}
Name: scripting Value: none | initial-only | enabled For: @media Type: discreteThe 'scripting' media feature is used to query whether scripting languages, such as JavaScript, are supported on the current document.
@custom-media = @custom-media <The <> [ < > | true | false ] ;
@custom-media --narrow-window (max-width: 30em);
@media (--narrow-window) {
/* narrow window styles */
}
@media (--narrow-window) and (script) {
/* special styles for when script is allowed */
}
/* etc */
<script>
CSS.customMedia.set('--foo', 5);
</script>
<style>
@media (_foo: 5) { ... }
@media (_foo < 10) { ... }
</style>
partial interface CSSRule {
const unsigned short CUSTOM_MEDIA_RULE = 17;
};
The CSSCustomMediaRule interface represents a ''@custom-media'' rule.
interface CSSCustomMediaRule : CSSRule {
attribute DOMString name;
[SameObject, PutForwards=mediaText] readonly attribute MediaList media;
};
DOMString
DOMString object
that contains the serialization of the <The following media features are deprecated. They kept for backward compatibility, but are not appropriate for newly written style sheets. Authors must not use them. User agents must support them as specified.
To query for the size of the viewport (or the page box on page media), the 'width', 'height' and 'aspect-ratio' media features should be used, rather than 'device-width', 'device-height' and 'device-aspect-ratio', which refer to the physical size of the the device regardless of how much space is available for the document being laid out. The device-* media features are also sometimes used as a proxy to detect mobile devices. Instead, authors should use media features that better represent the aspect of the device that they are attempting to style against.
Name: device-width Value: <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 <> For: @media Type: range
@media (device-width < 800px) { … }
In the example above, the style sheet will apply only to screens
less than ''800px'' in length.
The ''px'' unit is of the logical kind,
as described in the Units section.
Name: device-height Value: <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.> For: @media Type: range
<link rel="stylesheet" media="(device-height > 600px)" />In the example above, the style sheet will apply only to screens taller than 600 vertical pixels. Note that the definition of the ''px'' unit is the same as in other parts of CSS.
Name: device-aspect-ratio Value: <The 'device-aspect-ratio media feature is defined as the ratio of the value of the 'device-width' media feature to the value of the 'device-height media feature.> For: @media Type: range
@media (device-aspect-ratio: 16/9) { … }
@media (device-aspect-ratio: 32/18) { … }
@media (device-aspect-ratio: 1280/720) { … }
@media (device-aspect-ratio: 2560/1440) { … }