Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
 Discussion Forums
 HTML, XML, JavaScript...
 Software Reviews
 Editors,Others...
 Top100
 JavaScript Tutorials, ...
 Tutorials
 ASP, CSS, Databases...
 Discussion List
 FAQ, Roundup, Configure ...
 Authoring
 HTML, JavaScript, CSS...
 Design
 Layout, Navigation,...
 Graphics
 Tools, Colors, Images...
 Software
 Browsers, Editors, XML...
 Internet
 Domains, E-Commerce, ...
 WDVL Resources
  Intermdiate, Tutorials,...
 WDVL
 Discussion Lists, Top 100,...
 Technology Jobs


WDVL Newsletter

Active Server Pages
JSP/Java Servlets
Microsoft SQL Server
Daily Backup
Dedicated Servers
Streaming Audio/Video
24-hour Support    

jobs.webdeveloper.com

Hiermenus


e-commerce
Partner With Us















Developer Channel
FlashKit.com
JavaScript.com
JavaScriptSource
Developer Jobs
ScriptSearch
StreamingMediaWorld
Web Developer's Journal
Web Developer's Virtual Library
WebDeveloper.com
Webreference
Web Hosts
XMLfiles.com

internet.com
IT
Developer
Internet News
Small Business
Personal Technology
International

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers


Top 10 Articles
  1. Web Developer's Virtual Library: Encyclopedia of Web Design Tutorials, Articles and Discussions
  2. JavaScript Tutorial for Programmers
  3. Design
  4. JavaScript Tutorial for Programmers - Objects
  5. JavaScript Tutorial for Programmers - JavaScript Grammar
  6. JavaScript Tutorial for Programmers - Versions of JavaScript
  7. Cascading Style Sheets
  8. JavaScript Tutorial for Programmers - Embedding JavaScript
  9. JavaScript Tutorial for Programmers - Functions
  10. Authoring JavaScript
Domain Name Lookup
Search to find the availability of a domain name. Just enter the complete domain name with extension (.com, .net, .edu)

Using the '@media' Rules

October 30, 2000

The third mechanism for associating style sheets with media types is to use the @media rules. This specifies the target media types for a set of style sheet rules. For example, if you write:

  @media screen {
 h1 { font-size: 18pt }
}
@media print {
 h1 { font-size: 10pt }
}
@media screen, print {
 h1 { text-align: center }
}
. . . you are indicating that first level header has an 18-point font when displayed on the screen, a 10-point font when printed, and center-aligned on both media types. The third of the @media rules above points out that @media rules may apply to more than one media type. In this case, media types are separated by a comma.

Note that @media rules are applied in the order in which they are processed. In other words, if two applicable @media rules define different styles for the same element or selector, the latter @media rule will apply. For example, if we wrote:

  @media print {
 h1 { font-size: 10pt }
}
@media screen, print {
 h1 { font-size: 18pt; text-align: center }
}
an 18pt font will be used when displaying on the screen and when printing. With @media rules, you can specify media-dependencies within an internal style sheet or with an external style sheet.

Using '@media' Rules Within an XHTML Document

In order to specify media dependencies within an internal style sheet, you simply include the above information within your document by use of the <style> element:

<head>
  ...
  <style type="text/css">
  <!--
   @media screen {
    h1 { font-size: 18pt }
   }
   @media print {
    h1 { font-size: 10pt }
   }
   @media screen, print {
    h1 { text-align: center }
   }
  -->
  </style>
 ...
</head>
In this example, the <style> element's content is three @media rules. The first @media rule applies to the screen media type, the second to the print media type, and the third to both screen and print media types. The benefit of this approach is that common styles (such as center-alignment in the example above) can be collected under one @media rule to reduce redundancy and improve maintainability of the style information. The greatest benefit, however, of using @media rules is when they are used in external style sheets.

Using '@media' Rules Within External Style Sheets

In order to specify media-dependencies within an external style sheet is quite straightforward. Simply write your rules in a separate file:

/* mystyle.css */
@media screen {
   /* style rules for screen devices */
h1 { font-size: 18 }
}
@media print {
   /* style rules for print devices */
h1 { font-size: 10 }
}
@media screen, print {
   /* style rules shared by screen and print devices */
h1 { text-align: center }
}
[Note: The 2nd and 3rd lines above are one line, as are the 6th and 7th and the 10th and 11th. They were split for formatting purposes]

and save as, for example, mystyle.css. Then, as we've done before, simply use the href attribute of the <link> element to reference the style sheet from within the XHTML document:

<link rel="stylesheet" type="text/css" href="mystyle.css" />
Make sure, though, that you do not use the <link> elements media attribute in addition to the @media rules within the external style sheet – the web browser might not download your style sheet!

Using the <link> Element
Beginning XHTML
Creating Media-Dependent Style Sheets


Up to => Home / Authoring / Languages / XML / BeginningXHTML




Jupiter Online Media: internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and Jupiter Online Media

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers