TemplateStyles
ADVANCED EDITORS ONLY This document is very technical and contains complex and technical language. Do not use this or attempt to do anything if you have little to no experience in programming and how templates work on Wikipedia. This is being used to update the templates to a certain format. If you have any questions, please post on Simple Talk and clarify the issue. You will be responsible for breaking anything, so test all changes and preview everything before committing it. If you break anything by introducing articles, you can be blocked for disruptive editing as these templates are across our entire platform. |
' TemplateStyles' allow custom CSS pages to style content without an interface administrator having to edit sitewide CSS. TemplateStyles make it more convenient for editors to style templates. For example, those for which the sitewide CSS for the mobile skin or another skin (e.g., Timeless) currently negatively affects the display of the template.
Despite the name, templateStyles works on all types of pages, not just templates.
Guidelines
- The style must apply only to the associated template's output. It may also apply to highly relevant nearby wikitext where the template is used. (For example, many table templates should have TemplateStyles that are provided entirely in templates or that may provide a legend to a wikitext table.) It would otherwise be confusing if adding a template to one part of a page were to completely or partially change the display or styling of an unrelated part.
- Style pages should be associated with a specific template or group of templates and named accordingly. This allows style pages to be easily identified and edited. In general, this means that a style page should be a subpage of the related template, e.g.: Template:Mxt is only for examples of style and formatting. Do not use it in actual articles. or Template:Mxt is only for examples of style and formatting. Do not use it in actual articles., but not Template:!mxt is only for examples of style and formatting. Do not use it in actual articles. nor Template:!mxt is only for examples of style and formatting. Do not use it in actual articles..
- Generally follow the MediaWiki coding conventions for CSS. Some specifics:
- Use selectors that are highly likely unique to the template being used. This reduces the chance of conflicting CSS rules arising accidentally. Examples: Use Template:Mxt is only for examples of style and formatting. Do not use it in actual articles. rather than Template:!mxt is only for examples of style and formatting. Do not use it in actual articles. or Template:Mxt is only for examples of style and formatting. Do not use it in actual articles. rather than Template:!mxt is only for examples of style and formatting. Do not use it in actual articles..
- Avoid using Template:!mxt is only for examples of style and formatting. Do not use it in actual articles. per the conventions. HTML IDs are supposed to be unique on a page. Templates are rarely used uniquely, and those that are initially single-use-per-page are often later used in unanticipated ways. Use classes instead of IDs for styling.
- Avoid using Template:!mxt is only for examples of style and formatting. Do not use it in actual articles. per the conventions, except in mobile views to override style input from the associated template. Use of
!important
in TemplateStyles is exceptionally difficult to override because of the loading order of styles (personal CSS then TemplateStyles).
- In templates intended to be substituted, or those likely to be substituted, use {{ifsubst}} to remove the TemplateStyles tag. Example: {{allcaps}}.
- Inline styles may be used in a substituted template as the "if substed" case. Example: {{smallcaps}}.
- Images that do not require attribution (i.e., those in the public domain) are the only images that may be used as background images. For normal file usage, attribution is provided on the file description page, accessed by clicking the image. This is not possible if the image is being used as the background.
- The protection level of style pages should match their associated template. If a template is high-risk, its styles are also high-risk and should have the same protection. If a template is not high-risk, then vandalism to cause chaos could be achieved just by editing the template itself. A higher protection level for the style page would encourage editors to add inline styles to the template since the template would be editable, but the style page would not. Any templates using CSS pages with the wrong protection level will be categorized in Category:Templates using TemplateStyles with a different protection level.
- Remember to add
/* {{pp-template}} */
to any protected CSS pages to ensure that they display the appropriate lock icon. Protected templates using CSS pages that lack the lock icon will be categorized in Category:Templates using TemplateStyles without padlocks.
Note that the Manual of Style, including the Accessibility guidelines, still applies.
Workflow for conversion
- In Template:Mxt is only for examples of style and formatting. Do not use it in actual articles., identify all of the inline styles that can be moved to a separate stylesheet.
- Create Template:Mxt is only for examples of style and formatting. Do not use it in actual articles. containing all the classes that will replace the inline styles. Use template-specific class names where possible.
- In Template:Mxt is only for examples of style and formatting. Do not use it in actual articles. (or it's Template:Mxt is only for examples of style and formatting. Do not use it in actual articles. if you want to test first), add
<templatestyles src="myTemplate/styles.css"/>
(you don't need to specify the Template: namespace). It's probably best at the top so that it is obvious and to avoid a flash of unstyled content, but it will need to be on its own line if the template begins with wiki markup that has to start on a new line (e.g. wiki-table). - Amend the template (or sandbox) to replace the inline styles with the classes you defined in Template:Mxt is only for examples of style and formatting. Do not use it in actual articles.
- Do as much checking as you can. If you tested in the sandbox, you can check the test cases page where it exists, but specifically check that the styles you affected render properly.
- Specifically, for templates meant to be used inline, check to see if there are uses inside of links. TemplateStyles templates will not work inside links (right now).
- If you used the sandbox, either make an edit request for the main template or do the update if you are confident of your changes.
- Request or amend the protection level of Template:Mxt is only for examples of style and formatting. Do not use it in actual articles. to match that of Template:Mxt is only for examples of style and formatting. Do not use it in actual articles. as necessary.
- Add
{{Uses TemplateStyles}}
to the template's documentation to show which TemplateStyles stylesheets it uses.
Tips
- Uses of TemplateStyles stylesheets are recorded just like template transclusion and Scribunto module invocation. Special:WhatLinksHere and the "Templates used in this preview" section beneath the edit form will work for them as usual.
- To search for TemplateStyles stylesheets, you can search for contentmodel:sanitized-css. See also mw:Help:CirrusSearch#Contentmodel.
- When
{{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 (0 pages). - TemplateStyles experiments can be created at Template:TemplateStyles sandbox, e.g. for use in Module:Sandbox module experiments.
- TemplateStyles pages can be created outside of the template or module namespaces by either first creating them in template namespace and moving them, or (for admins and template editors) by using Special:ChangeContentModel. Regardless of the method, creating a .css page in userspace can only be done by the user in question or an interface admin, but once it is created, anyone can edit it.
- Moving a sanitized CSS page will not create a redirect.
Overriding TemplateStyles
Because of how 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.
- Each selector is 'hoisted' to
.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 {}
. - However, in the HTML each TemplateStyles style is always placed after your personal CSS file loads. Accordingly, the new rule would need to be more specific. That can come in a couple of ways. The easiest is to select the HTML element as in:
.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. - Lastly,
!important
can always override styles in your personal CSS. The usual caveats regarding!important
apply. I prefer one of the two options in bullet two if possible. (You must do this to override inline styles, regardless of the above; some templates cannot move everything to TemplateStyles per the flexibility given to template users. Implementers of templates should consider whether parameters likestyle
andwidth
are necessary. See also phab:T200632.)
Examples
- mw:Help:TemplateStyles/examples
- Template:Thermometer and Template:Thermometer/styles.css give a simple demonstration of how TemplateStyles may be mixed with inline styles (where a parameter is passed to change one or more styles)