Skip to: content, navigation

by Tommy Olsson and Paul O’Brien

@media

Browser support full matrix
IE6+ FF1+ Saf1.3+ Op9.2+
Buggy Full Full Full
Spec
Version
CSS2

Example

This rule set will be applied only when the document is printed or viewed in print preview mode:

@media print {
  body {
    padding: 1in;
    border: 0.5pt solid #666;
  }
}

Description

You can use the @media at-rule to specify that one or more rule sets in a style sheet will apply only to certain media types. The at-rule must be followed by a comma-separated list of media types and a block that contains rules.

It’s up to you to decide whether you prefer to use separate style sheets for different media, or to use a single style sheet with @media rules.

In the expanded example below, the first set of rules will be applied only for screen and projection media (Opera uses the latter in its full-screen mode). The second set of rules will be applied only when the document is printed or viewed in print preview mode:

@media screen, projection {
  html {
    background: #fffef0;
    color: #300;
  }
  body {
    max-width: 35em;
    margin: 0 auto;
  }
}

@media print {
  html {
    background: #fff;
    color: #000;
  }
  body {
    padding: 1in;
    border: 0.5pt solid #666;
  }
}

Compatibility

IE5.5Full
6.0Buggy
7.0Buggy
Firefox1.0Full
1.5Full
2.0Full
Safari1.3Full
2.0Full
3.0Full
Opera9.2Full
9.5Full

An @media rule won’t fail in Internet Explorer 6 or 7 if a media type is omitted, though it should.

User-contributed notes

ID:
#1
Date:
Thu, 06 Dec 2007 21:28:08 GMT
Contributed by:
tgoyer

If IE 5.5 support is full, but 6+ support is buggy, should this be listed at the top as "Buggy" or "Partial" instead of as "Full" since it is listed as "IE5.5+"?

Add a note

To post a note on this topic, please log in with your SitePoint username and password. If you don't have an account yet, you can create a new account for free.

Related Products

The Principles of Beautiful Web Design

Best Seller!

You don’t need to go to Art School to design great looking web sites!

Book Cover: The Principles of Beautiful Web Design

Download the FREE sample chapters