Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1037924 of Alternative Style Sheets

  • Revision slug: Web/CSS/Alternative_style_sheets
  • Revision title: Alternative Style Sheets
  • Revision id: 1037924
  • Created:
  • Creator: Nickolay
  • Is current revision? No
  • Comment add links to the modern CSSOM/HTML specs

Revision Content

Specifying alternative style sheets in a web page provides a way for users to see multiple versions of a page, based on their needs or preferences.

Firefox lets the user select the stylesheet using the View > Page Style submenu, Internet Explorer also supports this feature (beginning with IE 8), also accessed from View > Page Style (at least as of IE 11), but Chrome requires an extension to use the feature (as of version 48). The web page can also provide its own user interface to let the user switch styles.

An example: specifying the alternative stylesheets

The alternate stylesheets are commonly specified using a {{HTMLElement("link")}} element with rel="stylesheet alternate" and title="..." attributes, for example:

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

<link href="default.css" rel="stylesheet" type="text/css" title="Default Style">
<link href="fancy.css" rel="alternate stylesheet" type="text/css" title="Fancy">
<link href="basic.css" rel="alternate stylesheet" type="text/css" title="Basic">

In this example, the styles "Default Style", "Fancy", and "Basic" will be listed in the Page Style submenu, with the Default Style pre-selected. When the user selects a different style, the page will immediately be re-rendered using that style sheet.

No matter what style is selected, the rules from the reset.css stylesheet will always be applied.

Try it out

Click here for a working example you can try out.

Details

Any stylesheet in a document falls into one of the following categories:

  • Persistent (no rel="alternate", no title=""): always applies to the document.
  • Preferred (no rel="alternate", with title="..." specified): applied by default, but disabled if an alternate stylesheet is selected. There can only be one preferred stylesheet, so providing stylesheets with different title attributes will cause some of them to be ignored. See Correctly Using Titles With External Stylesheets for a more detailed discussion.
  • Alternate (rel="stylesheet alternate", title="..." must be specified): disabled by default, can be selected.

When style sheets are referenced with a title attribute on the {{HTMLElement("link", "<link rel=\"stylesheet\">")}} or {{HTMLElement("style")}} element, the title becomes one of the choices offered to the user. Style sheets linked with the same title are part of the same choice. Style sheets linked without a title attribute are always applied.

Use rel="stylesheet" to link to the default style, and rel="alternate stylesheet" to link to alternative style sheets. This tells the browser which style sheet title should be selected by default, and makes that default selection apply in browsers that do not support alternate style sheets.

Specifications

Specification Status Comment
{{SpecName('CSSOM', '#css-style-sheet-collections', 'CSS Style Sheet Collections')}} {{Spec2('CSSOM')}} The CSS OM specification defines the concepts of the style sheet set name, its disabled flag, and the preferred CSS style sheet set name.
It defines how these are determined, and lets the HTML specification define the HTML-specific behaviors by requiring it to define when to create a CSS style sheet.

{{SpecName('HTML WHATWG', 'semantics.html#link-type-stylesheet', 'Link type "stylesheet"')}}
{{SpecName('HTML WHATWG', 'semantics.html#attr-style-title', 'The style element')}}
{{SpecName('HTML WHATWG', 'semantics.html#attr-meta-http-equiv-default-style', 'Default style state (http-equiv="default-style")')}}

{{Spec2('HTML WHATWG')}} The HTML specification defines when and how the create a CSS style sheet algorithm is invoked while handling <link> and <style> elements, and also defines the behavior of <meta http-equiv="default-style">.
{{SpecName("HTML4.01", "present/styles.html#h-14.3", "Alternative style sheets")}} {{Spec2("HTML4.01")}} Earlier, the HTML specification itself defined the concept of preferred and alternate stylesheets.

 

Revision Source

<p>Specifying <strong>alternative style sheets</strong> in a web page provides a way for users to see multiple versions of a page, based on their needs or preferences.</p>

<p>Firefox lets the user select the stylesheet using the View &gt; Page Style submenu, Internet Explorer also supports this feature (beginning with IE 8), also accessed from View &gt; Page Style (at least as of IE 11), but Chrome requires an extension to use the feature (as of version 48). The web page can also provide its own user interface to let the user switch styles.</p>

<h2 id="An_example_specifying_the_alternative_stylesheets">An example: specifying the alternative stylesheets</h2>

<p>The alternate stylesheets are commonly specified using a {{HTMLElement("link")}} element with <code>rel="stylesheet alternate"</code> and <code>title="..."</code> attributes, for example:</p>

<pre class="eval">
&lt;link href="reset.css" rel="stylesheet" type="text/css"&gt;

&lt;link href="default.css" rel="stylesheet" type="text/css" title="Default Style"&gt;
&lt;link href="fancy.css" rel="alternate stylesheet" type="text/css" title="Fancy"&gt;
&lt;link href="basic.css" rel="alternate stylesheet" type="text/css" title="Basic"&gt;
</pre>

<p>In this example, the styles "Default Style", "Fancy", and "Basic" will be listed in the Page Style submenu, with the Default Style pre-selected. When the user selects a different style, the page will immediately be re-rendered using that style sheet.</p>

<p>No matter what style is selected, the rules from the reset.css stylesheet will always be applied.</p>

<h3 id="Try_it_out">Try it out</h3>

<p><a href="/samples/cssref/altstyles/index.html">Click here</a> for a working example you can try out.</p>

<h2 id="Details">Details</h2>

<p>Any stylesheet in a document falls into one of the following categories:</p>

<ul>
 <li><strong>Persistent</strong> (no <code>rel="alternate"</code>, no <code>title=""</code>): always applies to the document.</li>
 <li><strong>Preferred</strong> (no <code>rel="alternate"</code>, with <code>title="..."</code> specified): applied by default, but disabled if an alternate stylesheet is selected. <strong>There can only be one preferred stylesheet</strong>, so providing stylesheets with different title attributes will cause some of them to be ignored. See <a href="/en-US/docs/Correctly_Using_Titles_With_External_Stylesheets">Correctly Using Titles With External Stylesheets</a> for a more detailed discussion.</li>
 <li><strong>Alternate</strong> (<code>rel="stylesheet alternate"</code>, <code>title="..."</code> must be specified): disabled by default, can be selected.</li>
</ul>

<p>When style sheets are referenced with a <code>title</code> attribute on the {{HTMLElement("link", "&lt;link rel=\"stylesheet\"&gt;")}} or {{HTMLElement("style")}} element, the title becomes one of the choices offered to the user. Style sheets linked with the same <code>title</code> are part of the same choice. Style sheets linked without a <code>title</code> attribute are always applied.</p>

<p>Use <code>rel="stylesheet"</code> to link to the default style, and <code>rel="alternate stylesheet"</code> to link to alternative style sheets. This tells the browser which style sheet title should be selected by default, and makes that default selection apply in browsers that do not support alternate style sheets.</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('CSSOM', '#css-style-sheet-collections', 'CSS Style Sheet Collections')}}</td>
   <td>{{Spec2('CSSOM')}}</td>
   <td>The CSS OM specification defines the concepts of the <strong>style sheet set name</strong>, its <strong>disabled flag</strong>, and the <strong>preferred CSS style sheet set name</strong>.<br />
    It defines how these are determined, and lets the HTML specification define the HTML-specific behaviors by requiring it to define when to <strong><dfn class="dfn-paneled" data-dfn-type="dfn" data-lt="create a CSS style sheet" data-noexport="" id="create-a-css-style-sheet">create a CSS style sheet</dfn></strong>.</td>
  </tr>
  <tr>
   <td>
    <p>{{SpecName('HTML WHATWG', 'semantics.html#link-type-stylesheet', 'Link type "stylesheet"')}}<br />
     {{SpecName('HTML WHATWG', 'semantics.html#attr-style-title', 'The style element')}}<br />
     {{SpecName('HTML WHATWG', 'semantics.html#attr-meta-http-equiv-default-style', 'Default style state (http-equiv="default-style")')}}</p>
   </td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>The HTML specification defines when and how the <strong><dfn class="dfn-paneled" data-dfn-type="dfn" data-lt="create a CSS style sheet" data-noexport="" id="create-a-css-style-sheet">create a CSS style sheet</dfn></strong><dfn class="dfn-paneled" data-dfn-type="dfn" data-lt="create a CSS style sheet" data-noexport=""> algorithm is invoked while handling &lt;link&gt; and &lt;style&gt; elements, and also defines the behavior of <code>&lt;meta </code></dfn><code>http-equiv="default-style"&gt;</code>.</td>
  </tr>
  <tr>
   <td>{{SpecName("HTML4.01", "present/styles.html#h-14.3", "Alternative style sheets")}}</td>
   <td>{{Spec2("HTML4.01")}}</td>
   <td>Earlier, the HTML specification itself defined the concept of preferred and alternate stylesheets.</td>
  </tr>
 </tbody>
</table>

<p>&nbsp;</p>
Revert to this revision