Help:TemplateStyles

TemplateStyles is a tool to enable complex styling of templates>Special:MyLanguage/Help:Templates|templates without administrator privileges.

How does it work?
Editors can add  to a page and the contents of   will be parsed as wp-css>w:Cascading Style Sheets|CSS, sanitized and loaded on pages where the   tag is used (directly or by being used in a template that's being used in a page).

must have the  () contenthandler>Special:MyLanguage/Content handlers|content model, which is the default for subpages in the Template namespace that end with. The recommended usage pattern is to store the styles for  under a subpage like.

If  lacks a namespace prefix, it defaults to the Template namespace. Thus, for example,  will load.

The  tag should be placed before the content that is styled, e.g. at the top of the template, to avoid a potential wp-en-fouc>w:en:Flash of unstyled content|flash of unstyled content if the page is partially rendered while being loaded.

What problems does it solve?
Traditionally, there are two ways to style templates (or any other content): by using inline styles (that is, using HTML code and adding attributes like  to it) or by using certain special system messages such as common-css>MediaWiki:Common.css. Neither of those approaches work very well.

For inline styling:


 * There is no separation of content and presentation. In cases where the content does not come from a template (e.g. tables in articles), that will result in article wikitext that's unintelligible for most editors.
 * Since styles are mixed with wikitext, syntax highlighting and other forms of CSS editing support are difficult or impossible.
 * Styles have to be repeated for each HTML element they apply to, which results in lots of copy-pasting and code that is hard to read and maintain.
 * Style attributes are limited to a subset of CSS. Most importantly,  rules needed for wp-en-responsive>w:en:Responsive web design|responsive design do not work so it's impossible to make templates that work well over a wide range of screen sizes. Furthermore, inline styles take precedence over CSS stylesheets so user-, skin- or device-specific customizations become more difficult.

For system  pages:


 * Editing is limited to administrators, which is a major barrier to participation.
 * Editing restrictions cannot be lifted as there is no way to limit what CSS rules can be used, and some of them could be abused to track readers' IP addresses or even execute scripts in some older browsers.
 * Changes are impossible to test without saving first.
 * All stylesheets must be loaded on all pages (whether they actually use the page or not), which wastes bandwidth and makes debugging style rules harder.

TemplateStyles allows editors to associate style rules to specific pages, provides the full power of CSS stylesheets while filtering dangerous constructs, and works with preview/debug tools (such as templatesandbox>Special:MyLanguage/Extension:TemplateSandbox|TemplateSandbox) as expected.

Lowering the access and maintainability barrier will hopefully result in more innovation in the way templates are visually designed, less maintenance overhead, and better adaptability to screen options (especially mobile devices which by now constitute half of Wikipedia pageviews).

Is it safe?
Yes! TemplateStyles includes a full-fledged CSS parser that reads, re-serializes and escapes all code and removes CSS rules which do not match its whitelist. The parser is sufficiently fine-grained to reject remote resources (such as background images) but allow local ones. CSS selectors are rewritten so that they cannot refer to elements outside article content. (Visually modifying areas outside article content by displacing parts of the article, e.g. via absolute positioning, is not prevented at this time. This is no change from the status quo, as such a thing was already possible with wikitext and inline styles.)

What CSS rules are recognized?
Currently, TemplateStyles accepts most CSS3 properties supported by one or more major browser (as of early 2017). Beyond simple rules,,  ,  ,   and  /  at-rules are also supported (with font-face restricted to fonts whose name starts with  , for security reasons).

Non-standard properties (including vendor prefixes) are not currently supported. See phab>phab:T162379|T162379 for plans.

In which order do CSS styles override?
Which CSS rule takes effect is controlled by specificity (roughly, the complexity of the selector - e.g.  is more specific than  ). In case of equal specificity, CSS styles that come later in the document override earlier styles.

Mediawiki:Commons.css, other site scripts, user scripts and gadgets are loaded in the  section of the page. TemplateStyles stylesheets are loaded in the, so they override site/user script and gadget rules with equal specificity, and in the case of two TemplateStyles rules, the second overrides the first. (Note though that TemplateStyles rules are deduplicated: if the same stylesheet is referenced multiple times on the page, it is only inserted the first time. Note also that "later" has to do with document position, not load order. Gadgets add their CSS after the page has fully loaded, by manipulating the page with Javascript; some add it on-demand when the user does some action such as clicking a button. Nevertheless, they add it to the head, so equally-specific CSS rules in the body get precedence over it.)

What anti-abuse features are provided?
The design choice to store CSS in separate pages was made in part to make integration with the standard anti-abuse toolset easy. TemplateStyles CSS pages have their own content model so changes to them can be tracked or controlled with abusefilter>Special:MyLanguage/Extension:AbuseFilter|AbuseFilter, using the   variable.

CSS inclusion is tracked the same way as template transclusion, so you can see where a stylesheet is used via the "What links here" option, see what stylesheets are used on a page under "Page information" (and possibly on the edit screen, depending on what editor you use), and see what recent changes might be affecting a page using "Related changes".

TemplateStyles also leaves identifying information in the HTML code; to find out where a specific rule comes from, look at the page source, and the enclosing  tag will have an attribute like , where 123456 is the revision ID of the stylesheet (viewable with Special:Diff, for example).

How were the decisions around TemplateStyles made?
The idea of including CSS with templates was proposed and accepted in a phab1>phab:T483|request for comments. Technical details were pinned down in phab2>phab:T155813|a second RfC and workflow details in a qa>Extension:TemplateStyles/Q&A|user consultation.

Who is working on TemplateStyles?
TemplateStyles was originally a project of the wminfr>Wikimedia Reading Infrastructure team (preceded by exploratory work Coren did as a volunteer), then people moved around. It is now maintained by an ad hoc WMF team consisting of Brad Jorsch (developer), Chris Koerner (community liaison), Dan Garry (product manager), Gergő Tisza (developer) and Grace Gellerman (project manager).

Where do I report errors / ask for features?
Please [https://phabricator.wikimedia.org/maniphest/task/edit/form/1/?tags=templatestyles</> file tasks] under the TemplateStyles component in phab>Special:MyLanguage/Phabricator</>|Phabricator.

Where can I see it in action?
You can look at some examples>Help:TemplateStyles/examples</>|curated examples.

The feature is currently (as of March 2018) enabled on the following Wikimedia sites:


 * here on mediawiki.org ([<tvar|mweg>https://www.mediawiki.org/w/index.php?search=contentmodel%3Asanitized-css&fulltext=1&profile=all</> all stylesheets])
 * [<tvar|de>https://de.wikipedia.org</> German Wikipedia] ([<tvar|deeg>https://de.wikipedia.org/w/index.php?search=contentmodel%3Asanitized-css&fulltext=1&profile=all</> all stylesheets])
 * [<tvar|sv>https://sv.wikipedia.org</> Swedish Wikipedia] ([<tvar|sveg>https://sv.wikipedia.org/w/index.php?search=contentmodel%3Asanitized-css&fulltext=1&profile=all</> all stylesheets])
 * [<tvar|wt>https://wikitech.wikimedia.org</> Wikitech] ([<tvar|wteg>https://wikitech.wikimedia.org/w/index.php?search=contentmodel%3Asanitized-css&fulltext=1&profile=all</> all stylesheets])
 * [<tvar|test>https://test.wikipedia.org</> testwiki] ([<tvar|testeg>https://test.wikipedia.org/w/index.php?search=contentmodel%3Asanitized-css&fulltext=1&profile=all</> all stylesheets])