This page documents an English Wikipedia editing guideline. Editors should generally follow it, though exceptions may apply. Substantive edits to this page should reflect consensus. |
TemplateStyles enable the use of custom CSS pages to style content without requiring an interface administrator to edit sitewide CSS. This feature allows editors to create and maintain template-specific styles, improving flexibility and avoiding the need for global CSS changes, like MediaWiki:Common.css, that could have unintended effects elsewhere on the site.
TemplateStyles make it easier to ensure templates display consistently across different skins (for example, Vector) when sitewide CSS rules would otherwise interfere.
TemplateStyles work on all types of pages, not only templates, despite the name.
In general, follow MediaWiki CSS coding conventions. Some specifics:
!important is exceptionally difficult to override because of the loading order of styles (personal CSS then TemplateStyles).{{ifsubst}} to ensure it is not included when substituted.
/* {{pp-template}} */ to the top of protected CSS pages.A simplified step-wise workflow to convert inline styles in templates to use a CSS stylesheet with TemplateStyles.
<templatestyles src="myTemplate/styles.css"/> (you do not have to specify the Template: namespace). It is preferable to include it as the first wikitext, as only the wikitext after it is called will have the styles applied to it. It also ensures that it is prominent and avoids a flash of unstyled content.
It needs to be on its own line if the template starts with wiki markup that must start at the beginning of a line, as is the case with wikitables:
<templatestyles src="myTemplate/styles.css"/>
{| class="wikitable"
|-
...
|}
{{Uses TemplateStyles|Template:myTemplate/styles.css}} to the template's documentation.{{Uses TemplateStyles}} is placed on a template's documentation, it adds the template to Category:Templates using TemplateStyles. If the TemplateStyles stylesheet has a lower protection level, the template will be tracked in Category:Templates using TemplateStyles with a different protection level (1 page).Because of the way TemplateStyles is implemented, overriding TemplateStyles in your personal CSS requires a little more effort than normal. The rules on a specific TemplateStyles sheet are not the full CSS rules, nor can you match the selectors to override them.
.mw-parser-output, so to override a rule in a TemplateStyles sheet that looks like .documentation {}, a naive override in your personal CSS file would need to look like .mw-parser-output .documentation {}..mw-parser-output div.documentation {}. Another way would be to double one of the class selectors, as in .mw-parser-output.mw-parser-output .documentation {} or .mw-parser-output .documentation.documentation {}. This latter way is a little more future-proof but looks a little weirder.!important can always override styles in your personal CSS. The usual caveats regarding !important apply. Prefer one of the two options in bullet two if possible. (You must do this to override inline styles, regardless of any of the above; some templates cannot move everything to TemplateStyles per the flexibility given to template users. Implementers of templates should consider whether parameters like style and width are actually necessary. See also phab:T200632.)<table id="wikibase-full-name" class="wikibase-item" /> avoid using table#wikibase-full-name but rather table.wikibase-item.</templatestyles> tag remains after substitution, this may confuse non-technical users.