Media queries is a feature of CSS 3 allowing content rendering to adapt to different conditions such as screen resolution (e.g. mobile and desktop screen size). It became a W3C recommended standard in June 2012,[1] and is a cornerstone technology of responsive web design (RWD).
Media queries were first sketched in Håkon Wium Lie's initial CSS proposal in 1994,[2] but they did not become part of CSS 1. The HTML4 Recommendation from 1997 shows an example of how media queries could be added in the future.[3] 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.[4] The first public working draft for media queries was published in 2001.[5] Media Queries Level 3, published as a Candidate Recommendation on 27 July 2010, became a W3C Recommendation on 19 June 2012. Proposed corrections were published on 5 April 2022.[6]
Media Queries Level 4, published as a Working Draft on 9 May 2017, were a W3C Candidate Recommendation Draft as of 25 December 2021.[7]
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.[8] [9] Media queries use the
The following are examples of CSS media queries:
A media type can be declared in the head of an HTML document using the "media" attribute inside of a
The media type "all" can also be used to indicate that a style sheet applies to all media types.[12]
The following table contains the media features listed in the latest W3C recommendation for media queries, dated 6 June 2007.[13]
Feature | Value | Min/Max | Description | |
---|---|---|---|---|
color | integer | Number of bits per color component | ||
color-index | integer | Number of entries in the color lookup table | ||
device-aspect-ratio | integer/integer | Aspect ratio | ||
device-height | length | Height of the output device | ||
device-width | length | Width of the output device | ||
grid | integer | True for a grid-based device | ||
height | length | Height of the rendering surface | ||
monochrome | integer | Number of bits per pixel in a monochrome frame buffer | ||
orientation | "portrait" or "landscape" | Orientation of the screen | ||
resolution | resolution ("dpi", "dpcm" or "dppx") | Resolution | ||
scan | "progressive" or "interlaced" | Scanning process of "tv" media types | ||
width | length | Width of the rendering surface |