Media Queries
In last decade, we have seen so many devises of different shapes and sizes coming into the market. It has become more and more difficult to design a website that fits well in all these screens. But now there is a relief for developers, you can create a responsive design using the media queries. It allows you to display the same content in different layouts by using different style sheets depending on the output device.
HTML4 and CSS2 supports media dependent style sheets for different media types. For example, a document may use font height = 20px when it is displayed on the screen and height = 10px when it is printed. There are two available media types: ‘screen’ and ‘print’. 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.
<link rel="stylesheet" type="text/css" media="screen" href="font20px.css"> <link rel="stylesheet" type="text/css" media="print" href="font10px.css">
Media queries extend the functionality of media types by letting more precise labelling of style sheets. A Media query is a CSS3 extension to media types that allows you far greater control over rendering across different devices. A media query consists of a media type and zero or more expressions that tests one or more features of a output device to which the CSS is to be applied. 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. The CSS associated with the media query expression is only applied if the expression evaluates to true. You can use media queries in:
- <link> tag (HTML and XHTML)
- XML processing instructions
- @import at-rule
- @media at-rule
Here is a simple example written in HTML:
<link rel="stylesheet" media="screen and (color)" href="example.css" />
In the above example, a certain style sheet example.css applies to devices of a certain media type (‘screen’) and certain feature (color screen).
Here’s another example, which applies the CSS if the output device is a handheld device with a width between 20 and 200 pixels:
@import url(/style.css) screen and (min-width:800px) and (max-width:1280px);
You can combine several media queries by separating them by a comma. If one or more queries in this comma separated list are true then the complete query is true. Here is an example of several media queries in a comma-separated list:
@media <em>screen and (color), projection and (color)</em> { … }
Below you can find some more information about media queries:
- the comma expresses a logical OR
- the ‘
and’ keyword expresses a logical AND - the ‘
not’ keyword expresses a logical NOT, Thenotoperator has a very low precedence.
Here is the same example written in HTML, XHTML, XML, @import and @media:
<link rel="stylesheet" media="screen and (color), projection and (color)" rel="stylesheet" href="example.css">
<link rel="stylesheet" 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) { … }
Media features:
Media features resemble CSS properties. They have a name and accept certain values. Most media features accept optional ‘min-’ or ‘max-’ prefixes to express “greater or equal to” and “smaller or equal to” constraints. For example, the ‘color’ media feature can form expressions without a value (‘(color)’), or with a value (‘(min-color: 1)’).
| Feature | Value | Min/Max allowed? | Description |
| width | <length> | Yes | It describes the width of the targeted display area of the output device. For continuous media, this is the width of the viewport including the size of a rendered scroll bar (if any). Ex: <link rel="stylesheet" media="print and (min-width: 25cm)" href="http://…" />Style sheet is usable on printed output wider than 25cm. |
| height | <length> | Yes | It 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. |
| device-width | <length> | Yes | It 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. |
| device-height | <length> | Yes | It 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. |
| orientation | portrait | landscape | No | ‘portrait’ when the value of the ‘height’ media feature is greater than or equal to the value of the ‘width’ media feature. Otherwise ‘orientation’ is ‘landscape’. |
| aspect-ratio | <ratio> | Yes | It defined as the ratio of the value of the ‘width’ media feature to the value of the ‘height’ media feature. |
| device-aspect-ratio | <ratio> | Yes | It defined as the ratio of the value of the ‘device-width’ media feature to the value of the ‘device-height’ media feature. |
| color | <integer> | Yes | It describes the number of bits per color component of the output device. If the device is not a color device, the value is zero. |
| color-index | <integer> | Yes | It 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. |
| monochrome | <integer> | Yes | It 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. |
| resolution | <resolution> | Yes | It describes the resolution of the output device. The ‘dpi’ and ‘dpcm’ units describe the resolution of an output device. |
| scan | progressive | interlace | No | It describes the scanning process of “tv” output devices. |
| grid | <integer> | No | It is used to query whether the output device is grid or bitmap. If the output device is grid-based (e.g., a “tv” terminal, or a phone display with only one fixed font), the value will be 1. Otherwise, the value will be 0. |

Creating a responsive design is not that easy and the write up you give here is quite impressive. I need more time to read it completely so bookmarking it to read it later.
Thanks and looking forward for more articles related to UI Design.
Robin.