Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Wikipedia:TemplateStyles

From Wikipedia, the free encyclopedia
Wikimedia project guideline for TemplateStyles
This page documents an English Wikipediaediting guideline.
Editors should generally follow it, thoughexceptions may apply.Substantive edits to this pageshould reflect consensus.

TemplateStyles enable the use of customCSS pages to style content without requiring aninterface 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, likeMediaWiki:Common.css, that could have unintended effects elsewhere on the site.

TemplateStyles make it easier to ensuretemplates display consistently across differentskins (for example,Vector) when sitewide CSS rules would otherwise interfere.

TemplateStyles work on all types of pages, not only templates, despite the name.

Guidelines

[edit]

Limit scope

[edit]
  • Styles must apply only to the associated template's output by using specificCSS selectors.
  • Styles 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.

Naming convention

[edit]
  • Style pages must be clearly associated with their templates and named accordingly (e.g.Template:Blockquote usesTemplate:Blockquote/styles.css).
  • The naming convention is to save the stylesheet for an associated page, template or module in a subpage of said page called/styles.css.
  • 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 orTemplate:myTemplate/styles-foo.css, but notTemplate:styles-foo.css norTemplate:foo.css.

Coding conventions

[edit]
Main article:mw:Manual:Coding conventions/CSS

In general, follow MediaWiki CSS coding conventions. Some specifics:

  • Use specific selectors unique to the template. This reduces the chance of conflicting CSS rules arising accidentally. For example,.myTemplate-row rather than.row, and.myTemplate tr rather thantr.
  • Avoid#id selectors where ID is the HTML ID attribute.[a] 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!important unless absolutely required, except in mobile view to override style input from the associated template. Use of!important is exceptionally difficult to override because of the loading order of styles (personal CSS then TemplateStyles).
  • In templates that will besubstituted, instead oftranscluded, nest the stylesheet inside the second parameter of{{ifsubst}} to ensure it isnot included when substituted.[b]
    • For example,{{ifsubst| | <templatestyles src="..." /> }}, and{{allcaps}}. Inline styles may be used in the first parameter in a substituted template. Example:{{smallcaps}}.
  • Only usepublic domain images as CSS backgrounds.[c]
  • TheProtection level of the stylesheet must match that of the template. Add/* {{pp-template}} */ to the top of protected CSS pages.[d]
  • Manual of Style andaccessibility guidelines, such as the guidelines related tocolor contrast, still apply.

Workflow for conversion

[edit]

A simplified step-wise workflow to convert inline styles in templates to use a CSS stylesheet with TemplateStyles.

  1. InTemplate:myTemplate, identify all of the inline styles that can be moved to a separate stylesheet.
  2. CreateTemplate:myTemplate/styles.css containing all the classes that will replace the inline styles. Use template-specific class names where possible.
  3. InTemplate:myTemplate (or itsTemplate:myTemplate/sandbox if you want to test first), add<templatestylessrc="myTemplate/styles.css"/> (you do not have to specify theTemplate: 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 aflash of unstyled content.

    It needs to be on its own line if the template starts with wiki markup that must start on anewline, as is the case withwikitables:

    <templatestylessrc="myTemplate/styles.css"/>{|class="wikitable"|-...|}
  4. Amend the template (or sandbox) to replace the inline styles with the classes you defined inTemplate: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.
    • For templates used inline (versus block 'box' content), specifically ensure the template is not transcluded inside awikilink. TemplateStyles of templates will not work inside links (as of 2025[update]).
  6. If you used the sandbox, either make anedit request or copy the changes across to the main template yourself if you are confident in your changes.
  7. Request to have amended or amend the protection level ofTemplate:myTemplate/styles.css to match that ofTemplate:myTemplate as necessary.
  8. Add{{Uses TemplateStyles|Template:myTemplate/styles.css}} to thetemplate's documentation.

Tips

[edit]

Overriding TemplateStyles

[edit]

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.

  1. 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 {}.
  2. However, in the HTML each TemplateStyles style isalways placed after your personal CSS file loads. Accordingly, the new rule would need to be morespecific. That can come in a couple ways. The easiest is to select the HTML element also 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.
  3. Lastly,!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 likestyle andwidth are actually necessary. See alsophab:T200632.)

Examples

[edit]

See also

[edit]

Notes

[edit]
  1. ^For example, for<tableid="wikibase-full-name"class="wikibase-item"/> avoid usingtable#wikibase-full-name but rathertable.wikibase-item.
  2. ^When a template is substituted, its wikitext is copied directly into the page rather than being transcluded. If the</templatestyles> tag remains after substitution, this may confuse non-technical users.
  3. ^Images that do not require attribution
  4. ^Any templates using CSS pages with the wrong protection level are categorized inCategory:Templates using TemplateStyles with a different protection level. Protected templates using CSS pages that lack the lock icon are categorized inCategory:Templates using TemplateStyles without padlocks.

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Wikipedia:TemplateStyles&oldid=1320014940"
Categories:

[8]ページ先頭

©2009-2025 Movatter.jp