Media queries


Media queries is a feature of CSS3 allowing content rendering to adapt to different conditions such as screen resolution. It became a W3C recommended standard in June 2012, and is a cornerstone technology of responsive web design.

History

Media queries were first sketched in Håkon Wium Lie's initial CSS proposal in 1994, but they did not become part of CSS1. The HTML4 Recommendation from 1997 shows an example of how media queries could be added in the future. In 2000, W3C started work on media queries and also on another scheme for supporting various devices: CC/PP. The two address the same problem, but CC/PP is server-centric, while media queries are browser-centric. The first public working draft for media queries was published in 2001, and the specification became a W3C Recommendation in 2012 after browsers added support.

Usage

A media query consists of a media type and one or more expressions, involving media features, which resolve to either true or false. The result of the query is true if the media type specified in the media query matches the type of device the document is being displayed on and all expressions in the media query are true. When a media query is true, the corresponding style sheet or style rules are applied, following the normal cascading rules.
Media queries use the @media CSS "at-rule".

Examples

The following are examples of CSS media queries:

@media screen and


@media all and


@media screen and

Media types

A media type can be declared in the head of an HTML document using the "media" attribute inside of a element. The value of the "media" attribute specifies on what device the linked document will be displayed. Media types can also be declared within XML processing instructions, the @import at-rule, and the @media at-rule. CSS2 defines the following as media types:
The media type "all" can also be used to indicate that a style sheet applies to all media types.

Media features

The following table contains the media features listed in the latest W3C recommendation for media queries, dated 6 June 2007.
FeatureValueMin/MaxDescription
colorintegernumber of bits per color component
color-indexintegernumber of entries in the color lookup table
device-aspect-ratiointeger/integeraspect ratio
device-heightlengthheight of the output device
device-widthlengthwidth of the output device
gridintegertrue for a grid-based device
heightlengthheight of the rendering surface
monochromeintegernumber of bits per pixel in a monochrome frame buffer
resolutionresolution resolution
scan"progressive" or "interlaced"scanning process of "tv" media types
widthlengthwidth of the rendering surface