This page is a translated version of the page Extension:CSS and the translation is 76% complete.
MediaWiki扩展手册
CSS
发行状态: 穩定版
实现 解析器函数
描述 提供一个用于在文章添加CSS文件、文章或规则的解析器函数。
作者
最新版本 3.5.0
MediaWiki >= 1.31.0
许可协议 GNU通用公眾授權條款2.0或更新版本
下載
示例 organicdesign.nz
  • $wgCSSPath
  • $wgCSSIdentifier
季度下載量 211 (Ranked 45th)
前往translatewiki.net翻譯CSS扩展
問題 开启的任务 · 报告错误

CSS擴展允許將CSS樣式表載入到特定文章中。CSS樣式表可以是另一個文檔,也可以是直接在解析器函數中定義的規則。

用法

例如,如果你有一個名為「MyStyles.css」的CSS樣式表,它們是用於页面「MyFancyUserPage」的樣式,你可以為後者添加以下解析器函數語法:

{{#css:MyStyles.css}}

再者,如果「MyStyles.css」是位於/wiki/skins目錄之下的文件,那麼路徑應該也被寫進去,如下所示。請注意,檔案路徑必須是帶有文件根目錄斜杠的絕對路徑,以將其與檔名區分開來。

{{#css:/skins/MyStyles.css}}

或者,CSS規則可以直接包含在解析器函數(inline)中,如下例所示:

{{#css:
  body {
    background: yellow;
    font-size: 20pt;
    color: red;
  }
}}

安裝

  • 下载文件,并将其放置在您extensions/文件夹中的CSS目录内。
  • 将下列代码放置在您的LocalSettings.php 的底部:
    wfLoadExtension( 'CSS' );
    
  • 按需求配置
  •   完成 – 在您的wiki上导航至Special:Version,以验证已成功安装扩展。

配置

你可以为扩展文件设置基本URL,可选。

$wgCSSPath = false;  # 默认,与$wgScriptPath有关
$wgCSSPath = '';  # 与服务器根基(root)有关
$wgCSSPath = 'https://example.org/stylesheets';  # 与不同的站点有关

作为页面CSS替代使用

此前有个Extension:PageCSS (现已存档),其使用‎<css>‎</css>标签而非#css:解析器函数。 此扩展可以完成PageCSS扩展提供的大部分功能,但语法不兼容。 避免破坏仍然使用旧的‎<css>标签的现有页面的一种方法是在同一个wiki上同时使用Extension:CSSExtension:NewPageCSS

An alternate solution (if you want to use only this extension, but have existing content which expects Extension:PageCSS) is to save this stub function as a PHP file and include it from your LocalSettings.php to remap <css> to #css:

<?php

// Stub to remap <css></css> tags to {{#css:}} parser function extension

// For use when installing https://www.mediawiki.org/wiki/Extension:CSS to replace Extension:NewPageCSS on existing wikis

$wgHooks['ParserFirstCallInit'][] = 'wfCSSParserStubInit';
 
// Hook our callback function into the parser
function wfCSSParserStubInit( Parser $parser ) {

        // When the parser sees the <css> tag, it executes the stub wfCSSTagRender function (below) to invoke the {{#css:}} parser
        $parser->setHook( 'css', 'wfCSSTagRender' );
        return true;
}
// Execute <css> tag
function wfCSSTagRender( $input, array $args, Parser $parser, PPFrame $frame ) {

        // Stub function, just redirect the user-provided input text blindly to the {{#css:}} parser function to re-parse it as wikitext
        $output = $parser->recursiveTagParse('{{#css:' . $input . '}}', $frame);
        return $output;
}

At this point, the prior PageCSS (or NewPageCSS) extensions may be removed from your configuration; <css> now goes to #css: and (if you have this CSS extension already up and running) will behave the way any of the other CSS extensions always have.

改变特定页面颜色或背景的问题

使用解析器函数使用另一个页面的CSS不能够用于改变页面的颜色或背景,然而,使用解析器函数在页面自身可以允许改变背景。 若要解决这个问题,通过嵌入来加载CSS,就不会导致无法改变页面背景的问题。

漏洞

讨论页

參見