Tags

, , ,

Linking to an External Style Sheet

An external style sheet may be linked to an HTML document through HTML’s LINK element:

<link type="text/css" rel="stylesheet"
  media="only screen and (max-device-width: 480px)" href="iPhone.css"> 
<LINK REL=StyleSheet HREF="style.css" TYPE="text/css" MEDIA=screen>
<LINK REL=StyleSheet HREF="style.css" TYPE="text/css" TITLE="8-bit Color Style" 
   MEDIA="screen, print">
<LINK REL="Alternate StyleSheet" HREF="color-24b.css" TYPE="text/css" 
   TITLE="24-bit Color Style" MEDIA="screen, print">
<LINK REL=StyleSheet HREF="aural.css" TYPE="text/css" MEDIA=aural>

The <LINK> tag also takes an optional MEDIA attribute, which specifies the medium or media to which the style sheet should be applied. Possible values are

  • screen, for presentation on non-paged computer screens;
  • print, for output to a printer;
  • projection, for projected presentations;
  • aural, for speech synthesizers;
  • braille, for presentation on braille tactile feedback devices;
  • tty, for character cell displays (using a fixed-pitch font);
  • tv, for televisions;
  • all, for all output devices.

The REL attribute is used to define the relationship between the linked file and the HTML document. REL=StyleSheet specifies a persistent or preferred style while REL=”Alternate StyleSheet” defines an alternate style. A persistent style is one that is always applied when style sheets are enabled. The absence of the TITLE attribute, as in the first <LINK> tag in the example, defines a persistent style.

A preferred style is one that is automatically applied, such as in the second <LINK> tag in the example. The combination of REL=StyleSheet and a TITLE attribute specifies a preferred style. Authors cannot specify more than one preferred style.

An alternate style is indicated by REL=”Alternate StyleSheet”. The third <LINK> tag in the example defines an alternate style, which the user could choose to replace the preferred style sheet.

———————————————————————————————————————————-

Embedding a Style Sheet

A style sheet may be embedded in a document with the STYLE element:

<STYLE TYPE="text/css" MEDIA=screen>     
  <!--    
     BODY { background: url(foo.gif) red; color: black }    
     P EM { background: yellow; color: black }           
     .note { margin-left: 5em; margin-right: 5em }   
   -->  
</STYLE> 

The STYLE element is placed in the document HEAD. The required TYPE attribute is used to specify a media type, as is its function with the LINK element. Similarly, the TITLE and MEDIA attributes may also be specified with STYLE.

An embedded style sheet should be used when a single document has a unique style. If the same style sheet is used in multiple documents, then an external style sheet would be more appropriate.

———————————————————————————————————————————-

Importing a Style Sheet

A style sheet may be imported with CSS’s @import statement. This statement may be used in a CSS file or inside the STYLE element:

<STYLE TYPE="text/css" MEDIA="screen, projection"> 
 <!-- 
    @import url(http://www.htmlhelp.com/style.css); 
    @import url(/stylesheets/punk.css);   
    DT { background: yellow; color: black  }  
 -->  
</STYLE>

Note that other CSS rules may still be included in the STYLE element, but that all @import statements must occur at the start of the style sheet. Any rules specified in the style sheet itself override conflicting rules in the imported style sheets. For example, even if one of the imported style sheets contained DT {background: aqua}, definition terms would still have a yellow background.

The order in which the style sheets are imported is important in determining how they cascade. In the above example, if the style.css imported style sheet specified that STRONG elements be shown in red and the punk.css style sheet specified that STRONG elements be shown in yellow, then the latter rule would win out, and STRONG elements would be in yellow.

Imported style sheets are useful for purposes of modularity. For example, a site may separate different style sheets by the selectors used. There may be a simple.css style sheet that gives rules for common elements such as BODY, P, H1, and H2. In addition, there may be an extra.css style sheet that gives rules for less common elements such as CODE, BLOCKQUOTE, and DFN. A tables.css style sheet may be used to define rules for table elements. These three style sheets could be included in HTML documents, as needed, with the @import statement. The three style sheets could also be combined via the LINK element.

———————————————————————————————————————————-

Inlining Style

Style may be inlined using the STYLE attribute. The STYLE attribute may be applied to any BODY element (including BODY itself) except for BASEFONT, PARAM, and SCRIPT. The attribute takes as its value any number of CSS declarations, where each declaration is separated by a semicolon. An example follows:

<P STYLE="color: red; font-family: 'New Century Schoolbook', serif">  
   This paragraph is styled in red with the New Century Schoolbook font, if available. 
</P>

Note that New Century Schoolbook is contained within single quotes in the STYLE attribute since double quotes are used to contain the style declarations.

Inlining style is far more inflexible than the other methods. To use inline style, one must declare a single style sheet language for the entire document using the Content-Style-Type HTTP header extension. With inlined CSS, an author must send text/css as the Content-Style-Type HTTP header or include the following tag in the HEAD:

<META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css">

Inlining style loses many of the advantages of style sheets by mixing content with presentation. As well, inlined styles implicitly apply to all media, since there is no mechanism for specifying the intended medium for an inlined style. This method should be used sparingly, such as when a style is to be applied on all media to a single occurrence of an element. If the style should be applied to a single element instance but only with certain media, use the ID attribute instead of the STYLE attribute.

———————————————————————————————————————————-

The CLASS Attribute

The CLASS attribute is used to specify the style class to which the element belongs. For example, the style sheet may have created the punk and warning classes:

 .punk { color: lime; background: #ff80c0 } 
 P.warning { font-weight: bolder; color: red; background: white }

These classes could be referenced in HTML with the CLASS attribute:

<H1 CLASS=punk>Proprietary Extensions</H1> 
<P CLASS=warning>Many proprietary extensions can have negative side-effects. 

In this example, the punk class may be applied to any BODY element since it does not have an HTML element associated with it in the style sheet. Using the example’s style sheet, the warning class may only be applied to the P element.

A good practice is to name classes according to their function rather than their appearance. The warning class in the previous example could have been named red, but this name would become meaningless if the author decided to change the style of the class to a different color, or if the author wished to define an aural style for those using speech synthesizers.

Classes can be a very effective method of applying different styles to structurally identical sections of an HTML document. For example, this page uses classes to give a different style to CSS code and HTML code.

———————————————————————————————————————————-

The ID Attribute

The ID attribute is used to define a unique style for an element. A CSS rule such as

#wdg97 { font-size: larger }

may be applied in HTML through the ID attribute:

<P ID=wdg97>Welcome to the Web Design Group!</P>

Each ID attribute must have a unique value over the document. The value must be an initial letter followed by letters, digits, or hyphens. The letters are restricted to A-Z and a-z.

Note that HTML 4 allows periods in ID attribute values, but CSS1 does not allow periods in ID selectors. Also note that CSS1 allows the Unicode characters 161-255 as well as escaped Unicode characters as a numeric code, but HTML 4 does not allow these characters in an ID attribute value.

The use of ID is appropriate when a style only needs to be applied once in any document. ID contrasts with the STYLE attribute in that the former allows medium-specific styles and can also be applied to multiple documents (though only once in each document).

———————————————————————————————————————————-

The SPAN Element

The SPAN element was introduced into HTML to allow authors to give style that could not be attached to a structural HTML element. SPAN may be used as a selector in a style sheet, and it also accepts the STYLE, CLASS, and ID attributes.

SPAN is an inline element, so it may be used just as elements such as EM and STRONG in HTML. The important distinction is that while EM and STRONG carry structural meaning, SPAN has no such meaning. It exists purely to apply style, and so has no effect when the style sheet is disabled.

Some examples of SPAN follow:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">      
<HTML>
  <HEAD> 
    <TITLE>Example of SPAN</TITLE> 
    <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css"> 
    <STYLE TYPE="text/css" MEDIA="screen, print, projection">
       <!-- .firstwords { font-variant: small-caps } --> 
    </STYLE> 
  </HEAD> 
  <BODY> 
    <P> 
     <SPAN CLASS=firstwords>The first few words</SPAN> of a paragraph 
       could be in small-caps. Style may also be inlined, such as to change the style 
       of a word like <SPAN STYLE="font-family: Arial"> Arial</SPAN>.
    </P>
  </BODY> 
</HTML>

The DIV Element

The DIV element is similar to the SPAN element in function, with the main difference being that DIV (short for "division") is a block-level element. DIV may contain paragraphs, headings, tables, and even other divisions. This makes DIV ideal for marking different classes of containers, such as a chapter, abstract, or note. For example:

<DIV CLASS=note>      
 <H1>Divisions</H1>  
 <P>The DIV element is defined in HTML 3.2, but only the ALIGN attribute  is permitted
   in HTML 3.2. HTML 4.0 adds the CLASS, STYLE, and ID attributes, among others.            
   </P> 
 <P>Since DIV may contain other block-level containers, 
   it is useful for marking large sections of a document, such as this note. 
   </P>  
 <P>The closing tag is required. 
   </P> 
</DIV>
Advertisements