Alternative style sheets - CSS: Cascading Style Sheets 编辑

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. 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 <link> element with rel="alternate stylesheet" 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 "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="alternate stylesheet", title="..." must be specified): disabled by default, can be selected.

When style sheets are referenced with a title attribute on the <link rel="stylesheet"> or <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

SpecificationStatusComment
HTML Living Standard
The definition of 'link type "alternate"' in that specification.
Living Standard
HTML Living Standard
The definition of 'alternative stylesheet' in that specification.
Living Standard
HTML Living Standard
The definition of 'the "title" attribute for the style element' in that specification.
Living Standard
HTML Living Standard
The definition of 'meta http-equiv="default-style")' in that specification.
Living Standard
CSS Object Model (CSSOM)
The definition of 'CSS Style Sheet Collections' in that specification.
Working DraftThe 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.
HTML 4.01 Specification
The definition of 'Alternative style sheets' in that specification.
RecommendationEarlier, the HTML specification itself defined the concept of preferred and alternate stylesheets.

Browser compatibility

BCD tables only load in the browser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:162 次

字数:6925

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文