css @media rules

Here is an example stylesheet that uses @media rules:

body
{
    color:#444;
    background-color:#fff;
}
@media screen, projection
{
    body { background-image:url(background.png); }
}
@media print
{
    body { color:#000; }
}
@media handheld
{
    body { color:#0f6517; }
}

Assuming that the above rules are in a CSS file that applies to all media, the following will apply:

  • For the screen and projection media types, text will be dark grey and the background will consist of a repeated image.
  • For the print media type, text will be black on white.
  • User agents that apply the handheld media type will render dark green text on a white background.
  • For all other media types, text will be dark grey on white.

This may or may not work for you, but personally I’ve found that doing this leads to less redundancy and reduces the risk of one or more media types being overlooked when I make changes to the CSS.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s