Manual:Coding conventions/CSS/zh

本页面记述了MediaWiki代码库中CSS和LESS样式表的代码编写约定.

检查
我们使用stylelint作为我们的代码质量工具，并为维基媒体定制配置（stylelint-config-wikimedia）. 你可以使用节点模块 来检查你的CSS和LESS，MediaWiki和大多数扩展将其作为的一部分运行. MediaWiki核心的设置可在 找到.

命名
用同样的方式命名Class：所有单词小写，用连接号分隔. 在MediaWiki核心中，用 前缀避免与用户在维基文本模板、章节标题、小工具以及其他软件库中添加的Class和Id冲突. 尽量使用Class，避免引入Id.

一些示例：

注意，单词用连接号分隔，是为了简化非英语母语者的代码可读性，并为依赖像屏幕阅读器之类辅助技术的程序员提供更好的体验. 是一个过去的Class名，可追溯到上述建议之前，因此它将是.

在扩展和皮肤中，使用或前缀.

一些示例：



构造Class名
当在代码中动态构造Class时，确保在上面放一个注释，列出所有可能的Class. 参见手册:代码编写约定#动态标识符. 例如：

优先级
尽量提供低优先级选择器，最好是单个Class的选择器. 这简化了他们在如某些扩展或用户脚本的特定上下文中重写他们的过程.

空格
这样添加空格：


 * 每个选择器后换行.
 * 每个属性声明后换行.
 * （最后一行）选择器与CSS声明块的大括号之间添加一个空格.
 * 用制表符缩进每个属性声明.
 * 冒号（ ）前不添加空格.
 * 冒号和值之间添加一个空格.
 * 在多个值的属性中，逗号（ ）后添加一个空格.
 * 在选择器（如 ）和属性（如 ）中，左括号（ ）后与右括号（ ）前添加一个空格.
 * 每行声明（包括最后一行）后的分号（ ）后换行.
 * 右大括号不缩进.
 * 和注释应放在他们单独的行上，所针对的CSS声明上方.
 * 两个CSS规则集之间空一行.

引号
维基媒体使用单引号表示引用的值. 例如CSS属性 中的值和属性选择器中的值.

声明的 参数中不必要引号. 这可能导致问题的唯一情况是路径中出现未转义的右括号，在这种情况下，你应该对这些字符进行URL转义.



颜色属性值
CSS3支持许多不同类型的CSS属性颜色值，如 、 、 等. 为了一致性和兼容性，只使用以下三种：


 * 十六进制颜色值，如 和 . (使用小写字母以获得更好的gzip压缩！ 尽量使用简略标记. )
 * 如果需要alpha透明度，使用 值，如 . (注意：IE8不支持 ，所以通常在这之前提供一个使用十六进制颜色值作为回退的匹配声明. )
 * 颜色关键词. (注意：IE8只支持 和 .  它渲染的 是黑色的. )

为了一致性和优化文件压缩，建议所有值都小写. 参见讨论页章节.

避免其他颜色值（包括颜色名/关键字——在使用 、 、 和 符号时考虑i18n）. 还要确保你的前景和背景（包括背景渐变和退色）的色彩对比度符合WCAG 2.0 AA级，最好是AAA级.

在MDN网络文档中阅读更多内容.



大小单位
维基媒体基金会旨在支持多样化的受众和广泛的技术与用例. 一个非常流行的可访问性用户设置是增加浏览器默认字体大小，同时为普通用户提供最佳设计. 它还减少了 计算的负担和将开发人员的可预测性降到最低. 对所有不直接影响阅读或理解的用户界面元素使用. 主要是 、 ，图标大小的 或.



变量命名
CSS和LESS变量被设置为遵循命名逻辑，从重复属性到应用程序使用再到可能的更改者，或者简而言之. 变量通常用于一个属性，如 ，很少有变量能够在不同的上下文中使用. 例如大小变量.

它更简单，特别是在有许多CSS/LESS文件的大型项目中，使用单个使用变量，即使可能导致更大的变量定义文件. 参见问题描述讨论存档. 当变量内容发生变化时，它也更安全（考虑 与 的应用程序）.

CSS变量示例：

LESS变量示例：



厂商前缀
通常把CSS属性的标准化版本放在厂商前缀版本之后. 这对避免旧实现中的错误很重要，如. 参见https://css-tricks.com/ordering-css3-properties/.

正确，包含注释：



.client-js和.client-nojs
MediaWiki在每个页面的元素上输出Class. 在运行时，JavaScript代码用 替换它. 因此，你可以在选择器中使用这个Class来有条件地显示、隐藏或定制某些元素，取决于浏览器是否启用了JavaScript并支持. Note that for this to be useful, the stylesheet in question must be loaded with, not  (see )

反模式
Anti-patterns are mostly covered by use of central 'stylelint-config-wikimedia' already. Please also see the inline comments on master. Beyond there are some special properties/functionalities that deserve further explanation.



不要依赖基于px单位的值
This is not a general rule, but it is general for a few properties. When a user increases their default font size preferences in a browser due to addressing visual impairments, a widely used accessibility feature, all sizes defined in  are not scaled. In order to let users scale text with this setting, rely on relative sizes like  or , specifically on properties that would result in non-scalable text like font-size or line-height or would overflow and be hidden, often with certain width or height limitations.

z-index
Avoid using  when possible. Instead, try to use the natural stacking order in the DOM.

When use of z-index is required, please use the appropriate Codex design token.

!important
Avoid using  (except for working around upstream code running on the same page that also uses , because only   can override  ).

In most cases you don't need it at all. In other cases it may be the result of a bug elsewhere in the program. In general, to override a rule you use the same selector as the original style rule. Since CSS cascades, this works naturally (styles applied later override styles applied earlier, selectors don't need to be of higher specificity).

If the overriding styles apply before the original styles, the styles got loaded in the wrong order. That should be addressed, but you may resort to workarounds to artificially increase the specificity:


 * Repeat the same selector to increase weight, like.
 * Add or repeat attribute selectors, like.
 * Use default elements as ancestor selector (e.g.,  ).

Add however many points you need. It will still allow multiple stylesheets to use the same technique and each express their specificity. Better than adding in ancestors classes not related to your code. ( And more maintainable as they won't change. )

LESS
Starting with, there is native support in for using LESS (with file extension  ) in place of CSS. Most of the LESS syntax can be formatted using the CSS conventions:


 * Indent nested blocks with 1 tab (same as for indenting declarations inside CSS rules).
 * Don't space-align declarations values inside mixins (same as for CSS rules).
 * No spaces on the outside of the parameter lists in function invocations, mixin uses and mixin definitions (same as for  in CSS).
 * No quotes around parameter values (same as for  in CSS).

示例：

There's a few new concepts that don't map to CSS conventions, outlined below.

结构

 * Separate nested CSS rules from the parent declarations by 1 empty line.
 * tags must be on the line immediately above the declaration, as shown in the example above.



导入LESS/CSS文件

 * The filename of an import statement has to include the  file extension.
 * If the extension was omitted, in a folder with 'foo.css' and 'foo.less' LESS would import the latter.
 * If the code is ever used outside MediaWiki context, for example in Webpack as part of storybook, it will throw an error as it will assume it is a package with an index.css or index.js file meaning the code cannot be used.
 * Use  to load mixins and variables so that they may be used by the current LESS stylesheet; these are processed synchronously by LESS and will not be present in the generated CSS output. Load mixins first, variables second. Mixins should be free of variable values and parameters in mixin calls provide values with variables.
 * Don't use  to bundle stylesheets that are related to one another only conceptually; instead, reference the set of files in the   array of a ResourceLoader module.
 * Don't use  to import CSS files in LESS files, as the LESS parser will create an invalid import statement based on the physical location of the CSS file. Use   instead.



故障排除导入
If your LESS  doesn't work please check:


 * Does your code contain ? See this question on Stack Overflow about how to use @font-face with LESS.

混合
Mixin names should use hyphen-case, just like CSS class names, property keys and variable names.

They should be prefixed with  to avoid confusing developers who are familiar with CSS, but not LESS and distinguish them from classes, the syntax for which is similar.

As mentioned above, no spaces on the outside of the parameter list and avoid quoting values.

If you need to call a mixin with one or more arguments that contain a comma use a semicolon  in LESS to separate the arguments. This allows you to use commas in the literal value.



内置混合
is a LESS library maintained as part of MediaWiki that is automatically available to any LESS stylesheet in MediaWiki. It can be imported from any LESS stylesheet in core, skins, and extensions.

Since, the built-in mixins contain utilities for using CSS Flexbox. Support is present all browsers that implemented at least one of the specs, including Internet Explorer 10. You need to specify your own fallback for older browsers, e.g. you can use floats for a more basic experience. Mixins available:

Example usage: