This page is a translated version of the page Extension:TemplateStyles and the translation is 31% complete.
Other languages:
Bahasa Indonesia • ‎Deutsch • ‎English • ‎Türkçe • ‎français • ‎polski • ‎中文 • ‎日本語
MediaWiki 拡張機能マニュアル
OOjs UI icon advanced.svg
リリースの状態: 安定
実装 タグ , ContentHandler , フック
説明 CSSスタイルシートをテンプレートに読み込むことができます。
最新バージョン Continuous updates
互換性ポリシー Snapshots releases along with MediaWiki. Master is not backwards compatible.
MediaWiki 1.31+
PHP 5.5+
ライセンス GNU 一般公衆利用許諾書 2.0 以降
  • $wgTemplateStylesNamespaces
  • $wgTemplateStylesDisallowedProperties
  • $wgTemplateStylesDisallowedAtRules
  • $wgTemplateStylesUseCodeEditor
  • $wgTemplateStylesDisable
  • $wgTemplateStylesAutoParseContent
  • $wgTemplateStylesMaxStylesheetSize
  • $wgTemplateStylesAllowedUrls
<templatestyles src=... />
translatewiki.net で翻訳を利用できる場合は、TemplateStyles 拡張機能の翻訳にご協力ください

使用状況とバージョン マトリクスを確認してください。

問題点 未解決のタスク · バグを報告

TemplateStyles拡張機能は、Wikiマークアップ記法のページからCSSを読み込むことを可能にするための<templatestyles>タグを導入します。 これをテンプレートに書くことで、MediaWiki:Common.css に配置しなくてもテンプレートにカスタムCSSを影響させることができます。

拡張機能をWiki上でエディタとして使用する方法は、Help:TemplateStyles を参照してください。


まず、CSSページを作ってください。 規定では、「.css」で終わるテンプレート名前空間のサブページは、すべて、文法エラーがなければ「サニタイズ済みCSS」content model で作成されます。

名前空間の集合が$wgTemplateStylesNamespacesで修正されるか、あるいはSpecial:ChangeContentModel が任意のページで使用される場合があります。 そして、Wikitextのテンプレートに<templatestyles src="..." />タグを追加してCSSを読み込みます。

「サニタイズ済みCSS」コンテンツモデルを使用して保存されたCSSは厳密な妥当性要件を満たさなければなりません。無効なCSS、認識されない@-規則、認識されない、あるいはサポートされないプロパティーまたはプロパティーの値は保存できません。 無効なCSSがなぜが保存されてしまった場合、そのCSSがブラウザに出力された際に問題のある内容は取り除かれます。

The value of the src attribute on the tag is the title of the page, defaulting to the Template namespace. For example, <templatestyles src="Example/styles.css" /> will load the page "Template:Example/styles.css". This will fail if that page does not exist or has a content model other than "Sanitized CSS".

Styles can be scoped within the page by using the optional wrapper parameter to the tag, e.g. <templatestyles src="Example/styles.css" wrapper="div.example" /> would scope the styles loaded to any <div class="example"> inside the main parsed content. Any CSS simple selector sequence can be used for the wrapper parameter. This is intended to allow side-by-side comparison of live and sandbox versions of a template.

Use of sanitized CSS is tracked like transclusion of templates, and will show up as a transclusion on Special:WhatLinksHere .


  • Styles added by TemplateStyles are scoped into .mw-parser-output to avoid tampering with the user interface outside of the main parsed content.
    • To use TemplateStyles to style something like w:MediaWiki:Protectedpagetext, you would need to enclose the message's contents in <div class="mw-parser-output">...</div>.
  • The styles should be written to target specific CSS classes, and anything that generates elements with those classes should be sure to also include the styles itself rather than relying on some other template to have done so.
    • Styles included by a template can currently affect content on the page outside of the content generated by that template, but this ability may be removed in the future and should not be relied upon.
    • Including styles on a template that affects contents outside of that template, will cause those styles to not be applied when editing a section that doesn't contain that template. Example: including styles on an infobox that affect all tables of the page, when editing a section that doesn't contain the infobox, those tables won't be styled when previewing that section.
  • TemplateStyles allows few non-standardized CSS properties.

Requests to support additional properties should be filed in Phabricator in the css-sanitizer and TemplateStyles projects.

    • Requests should include links to standards-track documents (e.g. on w3.org) describing the syntax of the properties being requested, and an analysis of current browser support for the properties (e.g. a link to a caniuse.com page about the properties).
    • Vendor-prefixed properties (e.g. anything starting with -webkit-, -moz-, or -ms-) are likely to be declined if they're not needed for modern browsers.
  • @font-face rules must use a font-family prefixed with "TemplateStyles".

This should largely prevent redefining fonts used elsewhere in the document.

  • To target styles based on skins, use a selector such as body.skin-vector .myClass; specification of the body element is required and must be followed by a descendant combinator (i.e. the space).

Other classes on the body or html elements may be targeted in the same manner. 1.32+


  • Vagrant を使用している場合は、vagrant roles enable templatestyles --provision でインストールしてください
  • ダウンロードして、ファイルを extensions/ フォルダー内の TemplateStyles という名前のディレクトリ内に配置します。
  • Git でのインストールの場合のみ、PHP の依存関係をインストールするため Composer を実行します。 (See タスク T173141 for potential complications.)
  • 以下のコードを LocalSettings.php の末尾に追加します:
    wfLoadExtension( 'TemplateStyles' );
  • 必要に応じて設定します。
  •   完了 – ウィキの「Special:Version」に移動して、拡張機能が正しくインストールされたことを確認します。
For a more pleasant user interface, with syntax highlighting and a code editor with autoindent, install the following extensions:


Configuration settings
パラメーター 既定値 コメント
    "audio" => [
    "image" => [
    "svg" => [
    "font" => [],
    "namespace" => [
    "css" => []
PCRE regular expressions to match allowed URLs for various types of external references.

Keys are external reference types, values are arrays of regular expressions (including delimiters) to match allowed URLs. Current external reference types are:

Sound file, for cue properties of the CSS Speech Module.
Image file, for properties such as background.
SVG file, for Masking and Filters.
For @font-face's src.
[ NS_TEMPLATE => true ]
Namespaces in which to set the "Sanitized CSS" content model for titles ending in ".css".

Enabling this for 2 (User) or 8 (MediaWiki) is a bad idea, as it will conflict with the normal CSS files in those namespaces.

Properties to blacklist in CSS style rules.

The TemplateStylesPropertySanitizer hook allows for finer-grained control.

At-rules to blacklist in stylesheets.

The TemplateStylesStylesheetSanitizer hook allows for finer-grained control.

Whether to enable Extension:CodeEditor for the "Sanitized CSS" content type.
If true, the "Sanitized CSS" content model will be added to $wgTextModelsToParse if the CSS content model is already present in that array.

If false, add 'sanitized-css' to that array manually if you want categories and such parsed out of the CSS page.

Maximum size (in bytes) of a stylesheet. null for no limit.


$wgTidyConfig should be configured to use no tidying or RemexHtml. If used with any of the Raggett drivers, a <templatestyles /> tag in the middle of a paragraph (including in an inline template) will cause tidy to break the paragraph at that point. The other drivers have not been tested for this issue.


If you experience an error when importing pages from another wiki, for example, it may help to enable $wgShowExceptionDetails in your LocalSettings.php to determine if you are experiencing any of the errors below.

  • Class 'Wikimedia\CSS\Parser\Parser' not found
    This error may come up when attempting to import a wiki CSS page or when changing the content model of a page to "santized-css". There is, as of April 16, 2020, an open bug in the extension distributor which requires composer install --no-dev to be run in the extension subdirectory even when the extension is not installed from Git. 詳細は Topic:Ukv6pdo96a8qfur2 を参照してください。
  • Import failed: The content model 'sanitized-css' is not registered on this wiki.
    Some people get this error on a wiki when importing pages. This extension provides that content model. Install TemplateStyles to fix your import.