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,Media Queries Publication History 19 June 2012 and is a cornerstone technology of
responsive web design
Responsive web design (RWD) or responsive design is an approach to web design that aims to make web pages render well on a variety of devices and window or screen sizes from minimum to maximum display size to ensure usability and satisfactio ...
(RWD).
History
Media queries were first sketched in
Håkon Wium Lie
Håkon Wium Lie (born July 26, 1965) is a Norwegian web pioneer, a standards activist, and the chairman of YesLogic, developers of Prince CSS-based PDF rendering software. He is best known for developing Cascading Style Sheets (CSS) while work ...
's initial CSS proposal in 1994, but they did not become part of CSS 1. The
HTML4
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ...
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. 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.
Media Queries Level 4, published as a Working Draft on 9 May 2017, were a W3C Candidate Recommendation Draft as of 25 December 2021.
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 (display-mode: fullscreen)
@media all and (orientation: landscape)
@media screen and (min-device-width: 500px)
Media types
A media type can be declared in the head of an
HTML
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
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
Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing data. It defines a set of rules for encoding electronic document, documents in a format that is both human-readable and Machine-r ...
processing instructions, the @import at-rule, and the @media at-rule. CSS 2 defines the following as media types:
* all (suitable for all devices)
*
braille
Braille ( , ) is a Tactile alphabet, tactile writing system used by blindness, blind or visually impaired people. It can be read either on embossed paper or by using refreshable braille displays that connect to computers and smartphone device ...
* embossed
*
handheld
A mobile device or handheld device is a computer small enough to hold and operate in hand. Mobile devices are typically battery-powered and possess a flat-panel display and one or more built-in input devices, such as a touchscreen or keypad. Mod ...
* print
* projection
* screen
* speech
* tty
* TV
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.