Wikipedia:TemplateStyles

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

TemplateStyles allow custom CSS pages to be used to style content without an administrator having to edit sitewide CSS. TemplateStyles make it more convenient for editors to style templates; for example, those templates for which the sitewide CSS for the mobile skin or another skin (e.g. Timeless) currently negatively affects the display of the template.

Guidelines[edit]

  • 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, there are many table templates which should have TemplateStyles which are provided entirely in templates, or which 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 of the page.
  • 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:myTemplate/styles.css or Template:myTemplate/styles-foo.css, but not Template:styles-foo.css nor Template:foo.css.
  • Generally follow the Mediawiki coding conventions for CSS. Some specifics:
    • Use selectors that are highly likely to be unique to the template being used. This reduces the chance of conflicting CSS rules arising accidentally. Examples: Use .myTemplate-row rather than .row or .myTemplate tr rather than tr.
    • Avoid using #id 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 !important per the conventions, except in mobile views to override style input from the associated template.
  • In templates intended to be substituted, use inline CSS instead of TemplateStyles. Substituting a template which uses TemplateStyles leaves the <templatestyles /> tag in the resulting code, which may be confusing.
  • 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 that of their associated template. If a template is high-risk, then 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. And templates using CSS pages that lack the lock icon will be categories in Category:Templates using TemplateStyles without padlocks

Note that the Manual of Style, including the Accessibility guidelines, still applies.

Workflow for conversion[edit]

  1. In Template:myTemplate, identify all of the inline styles that can be moved to a separate stylesheet.
  2. Create Template:myTemplate/styles.css containing all the classes that will replace the inline styles. Use template-specific class names where possible.
  3. In Template:myTemplate (or its Template:myTemplate/sandbox 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).
  4. Amend the template (or sandbox) to replace the inline styles with the classes you defined in Template:myTemplate/styles.css
  5. Do as much checking as you can. If you tested in the sandbox you can check the testcases page where it exists, but specifically check that the styles you affected render properly.
    1. 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).
  6. 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.
  7. Request or amend the protection level of Template:myTemplate/styles.css to match that of Template:myTemplate as necessary.
  8. Add {{Uses TemplateStyles}} to the template's documentation to show which TemplateStyles stylesheets it uses.

Tips[edit]

Examples[edit]