18. XHTML Style Sheet Module


This section is normative.

The Style Sheet Module defines an element to be used when declaring internal style sheets. The element and attributes defined by this module are:

Elements Attributes Minimal Content Model
style Common, media (MediaDesc) PCDATA

When this module is used, it adds the style element to the content model of the head element of the Structure Module.

Implementation: RELAX NG

18.1. The style element


The Common collection
A collection of other attribute collections, including: Core, Events, I18N, Bi-directional, Edit, Embedding, Map, and Hypertext
media = MediaDesc
The value of this attribute specifies the type of media for which the element is intended.

The style element allows an author to put style sheet rules in the head of the document. XHTML permits any number of style elements in the head section of a document.

User agents that don't support style sheets, or don't support the specific style sheet language used by a style element, must hide the contents of the style element. It is an error to render the content as part of the document's text.

The syntax of style data depends on the style sheet language.

Rules for style rule precedences and inheritance depend on the style sheet language.


 <style type="text/css">
   h1 {border-width: thin; border-style: solid; text-align: center}

18.1.1. External style sheets

Authors may separate style sheets from XHTML documents. This offers several benefits:

18.1.2. Preferred and alternate style sheets

XHTML allows authors to associate any number of external style sheets with a document. The style sheet language defines how multiple external style sheets interact (for example, the CSS "cascade" rules).

Authors may specify a number of mutually exclusive style sheets called alternate style sheets. Users may select their favorite among these depending on their preferences. For instance, an author may specify one style sheet designed for small screens and another for users with weak vision (e.g., large fonts). User agents should allow users to select from alternate style sheets.

The author may specify that one of the alternates is a preferred style sheet. User agents should apply the author's preferred style sheet unless the user has selected a different alternate.

Authors may group several alternate style sheets (including the author's preferred style sheets) under a single style name. When a user selects a named style, the user agent must apply all style sheets with that name. User agents must not apply alternate style sheets with a different style name. The section on specifying external style sheets explains how to name a group of style sheets.

Authors may also specify persistent style sheets that user agents must apply in addition to any alternate style sheet.

User agents must respect media descriptors when applying any style sheet.

User agents should also allow users to disable the author's style sheets entirely, in which case the user agent must not apply any persistent or alternate style sheets.

18.1.3. Specifying external style sheets

Authors specify external style sheets with the following attributes of the link element:

User agents should provide a means for users to view and pick from the list of alternate styles. The value of the title attribute is recommended as the name of each choice.

In this example, we first specify a persistent style sheet located in the file mystyle.css:

<link href="mystyle.css" rel="stylesheet" type="text/css"/>

Setting the title attribute makes this the author's preferred style sheet:

 <link href="mystyle.css" title="compact" rel="stylesheet" type="text/css"/>

Adding the keyword "alternate" to the rel attribute makes it an alternate style sheet:

<link href="mystyle.css" title="Medium" rel="alternate stylesheet" type="text/css"/>

For more information on external style sheets, please consult the section on links and external style sheets.

If two or more link elements specify a preferred style sheet, the first one takes precedence.