demosthenes.info

I’m Dudley Storey, the author of Pro CSS3 Animation. This is my blog, where I talk about web design and development with , and . To receive more information, including news, updates, and tips, you should follow me on Twitter.

featured articles

popular favourites

CSS3 media queries

Traditional CSS supported display rules for a limited number of devices, such as all, screen, print, handheld, television, and projector. None of these made any conclusions about the size, orientation, or resolution of the device: a “screen” could be 4 inches wide or 400, and the same presentational rules would apply to both. provides an alternate and impressive array of controls that allow you to target style rules to devices with particular capabilities, via media queries:

  1. <link rel=stylesheet href=styles.css>
  2. <link rel=stylesheet
  3. media="only screen and (max-device-width: 480px)" href="styles_h.css" />

In the code above, styles_h.css will only be used by devices that are screen based and have a maximum width of 480 pixels, which happens to be the native resolution of the iPhone. Note that this is device width: if you wished to use a stylesheet that targeted a browser that was minimized to 480 pixels wide, the syntax changes slightly:

  1. <link rel=stylesheet media="only screen and (max-width: 480px)" href="styles_small.css" />

This rule would work for both smartphones and minimized browsers running on a desktop computer, and is generally considered to be the better option.However, writing separate stylesheets for different media formats and screen sizes is neither practical nor efficient: when the browser loads the page it will access all of the referenced stylesheet files, regardless of the current resolution. These extra HTTP requests will slow the load time of your page. It's usually a far better practice to incorporate all of your designs into a central styles.css stylesheet, using the @media syntax:

  1. /* standard CSS rules read by all devices and applicable to all resolutions */
  2. html, body { }
  3. @media print {
  4. /* specific CSS rules for print, added only if they conflict with
  5. the rules above */
  6. }
  7. @media only screen and (max-width : 1200px) {
  8. /* style rules for desktops and laptops with smaller screens,
  9. again only added if the rules here conflict with those at the top
  10. of the stylesheet */
  11. }
  12. @media only screen and (min-width : 768px) and (max-width : 1024px) {
  13. /* iPads, most other tablets */
  14. }
  15. @media only screen and (max-width : 480px) {
  16. /* most smartphones */
  17. }

Even without specific @media queries, a website designed to fluid principles will usually scale well when displayed on smaller devices. If you find that this is not the case, the addition of a meta tag in the <head> may help:

  1. <meta name="viewport" content="width=device-width">

You must be signed up in order to leave comments.

I was having a lot of trouble with the concept of a 'viewport' on various devices, but this meta tag solved my issues:
<meta name="viewport" content="width=device-width">
It covers any browsing device with a 'viewport', no matter its dimensions or ppi.

posted by Tony Downey

This site helped me out a lot when testing out media queries: http://www.jensbits.com/demos/mq/

posted by tractorbeam

web developer guide

featured comment

by Aisling Brock in New Business Card Design

what i'm reading

A Feast for Crows: A Song of Ice and Fire: Book Four
A Feast for Crows: A Song of Ice and Fire: Book Four

what i'm watching

Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]
Prometheus: Collector's Edition (Bilingual) [Blu-ray 3D + Blu-ray + DVD + Digital Copy]

what i'm playing

Borderlands
Borderlands

what i'm hearing

Planets
Planets

blogs

podcasts

no ads ever

This blog is free of advertising, and always will be.

creative commons licensed

The content of this blog is free to use in whatever way you wish under the Creative Commons license.