spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / authoring / languages / xhtml / beginning / chap13 / 2 current pageTo page 2To page 3
[next]

Beginning XHTML

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

Using the <link> Element

The second mechanism for associating style sheets with media types is to use the <link> element's media attribute, which specifies the intended destination media for the external style information. This allows user agents to load and apply external style sheets based on the characteristics of the media where the document is being rendered.

For example, if you write:

<head>
  <link rel="stylesheet" type="text/css" media="print" href="myprint.css"/>
  <link rel="stylesheet" type="text/css" media="screen" href="myscrn.css"/>
</head>

you are indicating that the myprint.css style sheet should be used when printing the document, and that the myscrn.css style sheet should be used when displaying the document.

At the time that this book was written, only Microsoft Internet Explorer properly supported the <link> element with media types. With Netscape Navigator, if you specify anything other than the screen media type, Navigator will ignore the entire element, and therefore ignore the entire external style sheet!

Try It Out - Using the <link> Element to Handle Different Media Types

In this exercise, we will use two external style sheets to control the display and printing.

  • Type the following into your text editor:
    body { font-size: 18pt }
    span.speaker { background-color: yellow } 
    .stage { font-style: italic }
    p.stage { text-align: center }
  • Save the file as linkscrn.css
  • Create a new document in your text editor and type the following:
    body { font-size: 10pt }
    span.speaker { font-weight: bold }
    .stage { font-style: italic }
    p.stage { text-align: center }
    
  • Save this file as linkprnt.css
  • Edit the file style1.htm and make the following changes to the <head> section:
    <head>
      <title>Tempest Links</title>
      <link rel="stylesheet" type="text/css" media="print" href="linkprnt.css"/>
      <link rel="stylesheet" type="text/css" media="screen" href="linkscrn.css"/>
    </head>
  • Save the file as link.htm and run it in Microsoft Internet Explorer. You should see something like:

    1
  • From within your browser, select the File | Print menu item. You should see something like:

    2
How It Works

We simply took our two style sheets out of the document and put them into separate documents of their own. Note that the content of the style sheets wasn't changed in any way, so that what is seen on the browser and the printed page is identical to that of the last example. In the <head> of the document, we simply associate these external style sheets with the document content like so:

<head>
  <title>Tempest</title>
  <link rel="stylesheet" type="text/css" media="print" href="linkprnt.css"/>
  <link rel="stylesheet" type="text/css" media="screen" href="linkscrn.css"/>
</head>

We can see that there are two <link> elements. The first identifies the style sheet to use for the print media type and the second identifies the style sheet to use for the screen media type. The files where the style information is contained is given as the value of the href attribute.

When we looked at our document in the Web browser, the style sheet corresponding to the screen media type was opened and used. When we sent our document to the printer, the style sheet corresponding to the print media type was opened and used instead.

Contents

home / authoring / languages / xhtml / beginning / chap13 / 2 current pageTo page 2To page 3
[next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


Created: April 4, 2001
Revised: April 4, 2001


URL: http://webreference.com/authoring/languages/xhtml/beginning/chap13/2/