{"id":15046783,"url":"https://github.com/cssdream/css-creating","last_synced_at":"2025-04-05T05:09:14.653Z","repository":{"id":11659278,"uuid":"14167270","full_name":"cssdream/css-creating","owner":"cssdream","description":"CSS创作指南","archived":false,"fork":false,"pushed_at":"2021-11-22T09:52:24.000Z","size":45,"stargazers_count":552,"open_issues_count":10,"forks_count":77,"subscribers_count":26,"default_branch":"master","last_synced_at":"2025-03-29T04:11:22.507Z","etag":null,"topics":["css","css3"],"latest_commit_sha":null,"homepage":null,"language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cssdream.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-11-06T08:44:07.000Z","updated_at":"2025-03-26T05:22:34.000Z","dependencies_parsed_at":"2022-07-15T14:47:43.895Z","dependency_job_id":null,"html_url":"https://github.com/cssdream/css-creating","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cssdream%2Fcss-creating","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cssdream%2Fcss-creating/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cssdream%2Fcss-creating/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cssdream%2Fcss-creating/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cssdream","download_url":"https://codeload.github.com/cssdream/css-creating/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247289429,"owners_count":20914464,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["css","css3"],"created_at":"2024-09-24T20:53:34.553Z","updated_at":"2025-04-05T05:09:14.634Z","avatar_url":"https://github.com/cssdream.png","language":null,"funding_links":[],"categories":[],"sub_categories":[],"readme":"# CSS 创作指南（Beta）\n\nCSS 是一种领域语言（DSL），层叠与继承赋予了 CSS 优雅多姿的无限创造力。正是由于它如此「简单」，\n我们需要一些规范来使其变得更加可依赖。在使用 CSS 的时候不要把它当做一种编程语言，应该把它当做\n绘画或者创作。所以本文档不仅仅是一份 CSS 书写规范，更像是 CSS 创作的调色盘。\n\n\u003e 「作为成功的项目的一员，很重要的一点是意识到只为自己写代码是很糟糕的行为。如果将有成千上万人\n使用你的代码，那么你需要编写最具明确性的代码，而不是以自我的喜好来彰显自己的智商。」  \n—— Idan Gazit\n\n## 目录\n\n1. [缩进与换行](#indentation)\n2. [注释](#comments)\n3. [基本格式](#basic-format)\n4. [垂直对齐](#vertical-alignment)\n5. [省略](#ellipsis)\n6. [缩写](#shorthand)\n7. [书写顺序](#order)\n8. [选择器使用规则](#selector-rule)\n9. [字体](#fonts)\n10. [其他](#other)\n11. [预处理工具](#preprocessors)\n12. [后处理工具](#postprocessors)\n13. [代码组织](#organization)\n14. [构建部署](#build)\n\n[许可](#license)\n\n\u003ca name=\"indentation\"\u003e\u003c/a\u003e\n## 一、缩进与换行\n\n用空格好还是 TAB 好？4个空格还是2个空格好？这是永远的圣战，累觉不爱，本文档不做详细说明。  \n\n但是无论如何，项目中应该保持统一的缩进风格，以利于代码的阅读，同时可以避免在 git 等版本管理工具中造\n成冗余的 diff 信息，而且千万不要空格和制表符（TAB）混用。\n\n本文档规定：\n\n* **使用2个空格缩进。**\n* **使用 Unix 风格换行符（LF）** \n  保证跨平台的一致性，[更多](https://github.com/cssmagic/blog/issues/22)。\n\n* **删除行尾多余的空格**  \n  因为这些空格通常是不必要的（JSHint 中会通过 ```trailing``` 来检测是否存在多余的空格）。\n\n* **文件末尾增加一个空行**  \n  没有空行在合并多个文件时可能会把上一个文件的最后一行与下一个文件的第一行合并为一行，特别是，JS 中如果没有这个空行而恰巧末尾没有写分号，整个文件可能就会报错了。\n\n  扩展阅读：   \n  * [为什么 C 语言源程序最后一行要是一个空行？](http://www.zhihu.com/question/20018991)     \n  * [Why should files end with a newline?](http://stackoverflow.com/questions/729692/why-should-files-end-with-a-newline)\n\n### 1. 如何保证统一的缩进风格？\n\nSublime Text 在新建工程的时候会生成 ```xxx.sumlime-project``` 文件，可以配置一些基本缩进和排除目录等，但遗憾的是无法与其他编辑器通用。\n\n[cube.css](https://github.com/thx/cube/blob/gh-pages/cube.sublime-project) 中的示例：\n\n```js\n{\n  \"folders\": [{\n    \"path\": \".\",\n    \"folder_exclude_patterns\": [\"node_modules\", \"_site\"] // 排除目录\n  }],\n  \"settings\": {\n    \"tab_size\": 4,\n    \"translate_tabs_to_spaces\": true, // tab 转换为空格\n    \"ensure_newline_at_eof_on_save\": true, // 保存时文件末尾自动增加一个空行\n    \"trim_trailing_white_space_on_save\": true // 删除行尾多余的空格\n  }\n}\n```\n\n[EditorConfig](http://editorconfig.org/) 是一个帮助开发者在不同的编辑器中保持统一编码\n风格的插件，支持了大部分流行的编辑器。它包括两部分：代码风格规则定义(```.editorconfig``` 文件)和支持此规则的编辑器插件。\n\n\n### 2. 快速开始\n\n 1. 在项目根目录新建一个 ```.editorconfig``` 文件，保存为 utf-8 格式。Windows 用户由于无法直接新建一个只有扩展名的文件，新建的时候在末尾多加一个点 即可（```.editorconfig.``` ），也可以在命令行（CMD）中使用 ```echo.\u003e.editorconfig``` 来创建。    \n   ![Windows 中创建 .editorconfig 文件示例](http://gtms03.alicdn.com/tps/i3/TB1shUbGFXXXXbfXpXXXdvPSXXX-380-287.gif)\n\n 2. 编辑 ```.editorconfig``` 文件\n\n ```ini\n # css-creating coding style\n root = true\n \n # 为所有文件设置风格\n [*]\n charset = utf-8\n indent_style = space\n indent_size = 2\n end_of_line = lf\n trim_trailing_whitespace = true\n insert_final_newline = true\n \n # 为 Markdown 文件保留行尾空格\n [*.md]\n trim_trailing_whitespace = false\n\n ＃ 匹配多种类型的文件（逗号前后不能有空格）\n [*.{css,scss,sass}]\n indent_size = 2\n\n # 匹配多个指定的文件（逗号前后不能有空格）\n [{package.json,.travis.yml}]\n indent_style = space\n indent_size = 2\n ```\n\n 4. 安装编辑器插件\n\n\u003ctable\u003e\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/editorconfig/editorconfig-jetbrains#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/appCode.png\" title=\"AppCode\"\u003e\n        \u003cdiv\u003eAppCode\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/sindresorhus/atom-editorconfig#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/atom.png\" title=\"Atom\"\u003e\n        \u003cdiv\u003eAtom\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/kidwm/brackets-editorconfig/\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/brackets.png\" title=\"Brackets\"\u003e\n        \u003cdiv\u003eBrackets\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/editorconfig/editorconfig-codeblocks#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/codeblocks.png\" title=\"Code::Block\"\u003e\n        \u003cdiv\u003eCode::Block\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/editorconfig/editorconfig-emacs#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/emacs.png\" title=\"Emacs\"\u003e\n        \u003cdiv\u003eEmacs\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/editorconfig/editorconfig-geany#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/geany.png\" title=\"Geany\"\u003e\n      \u003cdiv\u003eGeany\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/editorconfig/editorconfig-gedit#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/gedit.png\" title=\"Gedit\"\u003e\n        \u003cdiv\u003eGedit\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/RReverser/github-editorconfig#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/github.png\" title=\"GitHub (code viewer and editor)\"\u003e\n        \u003cdiv\u003eGitHub\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/editorconfig/editorconfig-jetbrains#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/intellijIDEA.png\" title=\"inteltdJ\"\u003e\n        \u003cdiv\u003einteltdJ\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/editorconfig/editorconfig-jedit#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/jedit.png\" title=\"jEdit\"\u003e\n        \u003cdiv\u003ejEdit\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/editorconfig/editorconfig-notepad-plus-plus#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/notepad.png\" title=\"Notepad++\"\u003e\n        \u003cdiv\u003eNotepad++\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/editorconfig/editorconfig-jetbrains#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/phpStorm.png\" title=\"PHPStorm\"\u003e\n        \u003cdiv\u003ePHPStorm\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/editorconfig/editorconfig-jetbrains#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/pyCharm.png\" title=\"PyCharm\"\u003e\n        \u003cdiv\u003ePyCharm\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/editorconfig/editorconfig-jetbrains#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/rubyMine.png\" title=\"RubyMine\"\u003e\n        \u003cdiv\u003eRubyMine\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/sindresorhus/editorconfig-sublime#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/sublimetext.png\" title=\"Subtdme Text\"\u003e\n        \u003cdiv\u003eSubtdme Text\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/Mr0grog/editorconfig-textmate#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/textmate.png\" title=\"TextMate\"\u003e\n        \u003cdiv\u003eTextMate\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n\n  \u003ctr\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/editorconfig/editorconfig-vim#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/vim.png\" title=\"Vim\"\u003e\n        \u003cdiv\u003eVim\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/editorconfig/editorconfig-visualstudio#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"https://editorconfig.org/logos/visualstudio-pro.png\" title=\"Visual Studio\"\u003e\n        \u003cdiv\u003eVisual Studio\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"https://user-images.githubusercontent.com/2784308/49776537-566fdb80-fd37-11e8-8480-a27c0faf2b41.png\" title=\"Visual Studio Code\"\u003e\n        \u003cdiv\u003eVisual Studio Code\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n    \u003ctd align=\"center\"\u003e\n      \u003ca traget=\"_blank\" href=\"https://github.com/editorconfig/editorconfig-jetbrains#readme\"\u003e\n        \u003cimg width=\"150\" height=\"150\" src=\"http://editorconfig.org/logos/webStorm.png\" title=\"WebStorm\"\u003e\n        \u003cdiv\u003eWebStorm\u003c/div\u003e\n      \u003c/a\u003e\n    \u003c/td\u003e\n  \u003c/tr\u003e\n\u003c/table\u003e\n\n\n### 3. EditorConfig 文档\n\n 1. 通配符规则：\n    \n    ```*``` 匹配任意字符串，但不包括 ```/```\n\n    ```**``` 匹配任何字符串\n\n    ```?``` 匹配任何单字符\n\n    ```[name]``` 匹配任何括号中的单字符\n\n    ```[!name]``` 匹配任何非括号中的单字符\n\n    ```{s1,s2,s3}``` 匹配任何给出的多个字符串\n\n    说明：\n\n    * ```[]``` 匹配规则是从当前目录算起。\n    * ```?``` 只能匹配且必须有类似 f1.js、f2.js、f12.js 的文件，通过f??.js只能匹配到 f12.js，而f**.js可以匹配到所有。\n    * ```{s1,s2,s3}``` 之间不能有空格。```{f1.js, f2.js, f3.js}``` 无法匹配```f2.js``` 和 ```f3.js```。\n    * ```*``` 和 ```**``` 可以匹配空字符串，例如 ```f*.js``` 可以匹配到 ```f.js```。\n    * 多个匹配之间的规则如果不冲突是可以合并的。\n    * 优先级问题，如果两个匹配所定义的规则冲突，则会以最靠近打开文件的```.editorconfig``文件为准。\n    如果同一个文件中匹配定义冲突，则会以最后定义的为准。**所以在定义规则的时候，须先定义通用规则，后定义特殊规则。**\n \n 2. 属性规则（所有属性不区分大小写）\n\n    \n| 属性                      | 值                                              | 说明  |\n| :--------                 | :-----                                          | :----  |\n| root                      | true, false                                     | 设置是否是当前项目的根目录 |\n| indent_style              | tab, space                                      | 设置缩进格式 |\n| indent_size               | number                                          | 设置缩进大小 |\n| tab_width                 | number                                          | 设置 tab 表示的空格数，默认等于indent_size，无需设定 |\n| end_of_line               | lf(Unix \\n), cr(Mac OS \\r), crlf(Windows \\r\\n)  | 设置行尾换行符格式 |\n| charset                   | atin1, utf-8, utf-8-bom, utf-16be, utf-16le 等  | 设置字符编码 |\n| trim_trailing_whitespace  | true, false                                     | 设置是否自动删除行尾多余空格 |\n| insert_final_newline      | true, false                                     | 设置是否在保存文件时自动在行尾插入空行 |\n\n\u003ca name=\"comments\"\u003e\u003c/a\u003e\n## 二、注释\n\n良好的注释使代码更具有可读性和可维护性，尤其是多人协作的项目，不要让团队其他成员来猜测一段\n代码的意图。\n\n注释风格应当相对简洁，做如下规范：\n\n* 区块注释放在单独一行。\n* 保持注释内代码的缩进。\n* 为了注释文字更具有可读性，合理控制每行的字数，推荐每行80个字符（40个汉字）以内。\n\n提示：通过扩展 [emmet](http://emmet.io/) 等工具（例如[emmet-plus](https://github.com/yisibl/emmet-plus)）可以快速输出统一的注释风格。\n\nCSS 文件中有如下几种注释：\n\n### 1. 普通注释，注释文字左右各留一个空格。\n\n```\n/* 普通注释 */\n```\n\n### 2. 区块注释\n\n注释横线的长度为80个字符，作为换行参考。\n\n一级区块注释 \n\n```\n/* ==========================================================================\n   一级区块注释\n   ========================================================================== */\n```\n\n二级区块注释 \n\n```\n/* --------------------------------------------------------------------------\n   二级区块注释\n   -------------------------------------------------------------------------- */\n```\n### 3. Doxygen 风格的注释\n\n每个 CSS 文件的头部或区块的开始处应当包含 [Doxygen](http://zh.wikipedia.org/wiki/Doxygen) 风格的注释，以阐明该文件或这段代码的\n作用、作者、最后更新时间等信息。\n\n\n[Doxygen](http://zh.wikipedia.org/wiki/Doxygen) 风格的注释以 /** 开始，每行以 * 号开头。\n\n```\n/**\n * Doxygen 风格的注释示例\n * @file：    文件信息\n * @author:   一丝\n * @update:   2013-11-5\n * @note:     注解\n * @doc:      相关文档\n *\n * 这里是更详细的描述，当然我们要把字数控制在每行80个字符（40个汉字）以内。如果\n * 一行写不下，需要另起一行。\n */\n```\n\n [Doxygen](http://zh.wikipedia.org/wiki/Doxygen) 风格的注释内如果包含其他代码，不写开头的 * 号，以方便复制代码。\n\n```\n /**\n * Doxygen 风格的注释包含代码\n * \n * 例如我们可以在注释中嵌入 HTML 代码，同样保持代码的缩进。\n *\n  \u003cdiv class=\"mod\"\u003e\n    \u003cp\u003e这个模块名叫 mod\u003c/p\u003e\n  \u003c/div\u003e\n */\n```\n\n### 4. CSS 预处理工具中的单行注释\n\nSass， LESS， Stylus 中可以使用单行注释。\n\n```\n// 注释内容\n\n```\n\n### 5. clean-css 等压缩工具中的注释\n\nclean-css 是一个 CSS 压缩工具，为了保留 CSS 文件的版权信息等特殊需求，支持以下形式的注释\n\n```\n/*!\n  这里是版权信息或者重要的注释，压缩后不会被删除\n*/\n```\n\n\u003ca name=\"basic-format\"\u003e\u003c/a\u003e\n## 三、基本格式\n\n### 1. CSS文件头部声明 `@charset`\n\n为了避免 HTML 和 CSS 文件编码不同时造成中文解析乱码，造成的不必要的麻烦，CSS 文件头部统一加上文件对应的编码，例如文件编码为 `UTF-8` 时：\n\n```css\n@charset \"UTF-8\";\n/* 开始书写样式 */\n```\n\n需要注意的是：\n\n* `@charset` 前面不能有任何字符。\n* `@charset` 必须出现在 CSS 文件的最开始。\n\n\u003e注：在使用 `SASS` 时如果不指定 `@charset` 也可能造成乱码。\n\n### 2. 统一使用小写。\n\n* 字体名称以及特殊的 CSS 属性/值（```translateX```等）不要求强制小写\n* 颜色值如果是16进制，推荐小写，更加容易辨识。\n* 如果是关键字色值，推荐使用更加直观的[颜色关键字](http://dev.w3.org/csswg/css-color-3/#svg-color)。\n\n不推荐的写法：\n\n```css\n.Foo{\n  BACKGROUND: #CCC;\n  color: currentColor;\n  border-color: #F00; /* 红色 */\n  transform: translateX(20px);\n}\n```\n\n推荐的写法：\n\n```css\n.foo{\n  background: #ccc;\n  color: currentColor;\n  border-color: red;\n  transform: translateX(20px);\n}\n```\n\n\n### 3. 保持空格\n\n留白是一种艺术，合理的留白（空格）可以更好的阅读代码\n\n* 规则集的左大括号前保留一个空格\n* 属性值前增加个空格(使用 [Emmet](https://emmet.io/) 会自动生成这个空格)\n  ```css\n  .selector {\n    width: 200px;\n    font-size: 22px;\n  }\n  ```\n\n* 逗号后面保留一个空格。\n  ```css\n  .foo {\n    color: rgba(0, 0, 0, .5);\n    transition: color .3s, width .5s cubic-bezier(.6, 0, .2, 1);\n    transform: matrix(0, 1, 1, 1, 10, 10);\n  }\n\n  @keyframes flash {\n    0%, 50%, 100% {\n      opacity: 1;\n    }\n\n    25%, 75% {\n      opacity: 0;\n    }\n  }\n  ```\n\n* 属性值中的 `/` 分隔符前后保留一个空格。\n  ```css\n  .foo {\n    font: 12px / 1.5 sans-serif;\n    background: center / cover;\n    grid-area: 1 / 1 / 4 / 2;\n    grid-column: 2 / 3;\n    grid-template: 100px 1fr / 50px fit-content(100px);\n  }\n  ```\n\n* `calc()` 函数中的 `+, -, *, /` 运算符前后保留一个空格。\n  \n  错误的语法\n\n  ```css\n  .foo {\n    width: calc(100%/3-2*1em+2*1px);\n    margin: calc(1rem -2px) calc(1rem- 1px);\n  }\n  ```\n\n  正确的语法\n\n  ```css\n  .foo {\n    width: calc(100%/3 - 2*1em + 2*1px);\n    margin: calc(1rem - 2px) calc(1rem - 1px);\n  }\n  ```\n\n  注意：`*, /` 虽然不用加前后空格，但是为了统一，建议都加空格。\n\n  推荐的写法：\n\n  ```css\n  .foo {\n    width: calc(100% / 3 - 2 * 1em + 2 * 1px);\n    margin: calc(1rem - 2px) calc(1rem - 1px);\n  }\n  ```\n\n* Media Queries 中的 `\u003e, \u003c, \u003e=, \u003c=` 标识符前后保持一个空格。\n  ```css\n  @media (width \u003e= 500px) and (width \u003c= 1200px) {\n    \n  }\n\n  @custom-media --only-medium-screen (width \u003e= 500px) and (width \u003c= 1200px);\n  ```\n\n* 其他未来 CSS 新增的语法以此类推，保留合适的空格。\n\n### 4. 每个声明前保留一级缩进\n\n不推荐的写法：\n\n```css\nh3 {\nfont-weight: bold;\n}\n```\n\n推荐的写法：\n\n```css\nh3 {\n  font-weight: bold;\n}\n```\n\n### 5. 右大括号保持与该规则集第一个字符对齐\n\n不推荐的写法：\n\n```css\nh3{\n  font-weight: bold;\n  }\n```\n\n推荐的写法：\n\n```css\nh3 {\n  font-weight: bold;\n}\n```\n\n### 6. 多个选择器和声明都独占一行\n\n不推荐的写法：\n\n```css\nh1, h2, h3 {\n  font-weight: normal; line-height: 1.5;\n}\n```\n\n推荐的写法：\n\n```css\nh1,\nh2,\nh3 {\n  font-weight: normal;\n  line-height: 1.5;\n}\n```\n\n### 7. 每个规则集之间保留一个空行\n\n不推荐的写法：\n\n```css\n.selector1 {\n  display: block;\n  width: 100px;\n}\n.selector2 {\n  padding: 10px;\n  margin: 10px auto;\n}\n```\n\n推荐的写法：\n\n```css\n.selector1 {\n  display: block;\n  width: 100px;\n}\n\n.selector2 {\n  padding: 10px;\n  margin: 10px auto;\n}\n```\n\n### 8. 统一使用双引号`\"\"`\n\n不推荐的写法：\n\n```css\n.foo:before {\n  content: '';\n}\n\ninput[type='checkbox'] {\n  position: absolute;\n}\n```\n\n推荐的写法：\n\n```css\n.foo:before {\n  content: \"\";\n}\n\ninput[type=\"checkbox\"] {\n  position: absolute;\n}\n```\n\n### 9. 每增加一级花括号嵌套，则增加一级缩进\n\n例如：\n\n```css\n@supports (animation: 1s foo both) {\n  @keyframes foo {\n    50% {\n      transform: scale(1.2);\n    }\n\n    80% {\n      transform: scale(0);\n    }\n  }\n}\n```\n\n\u003ca name=\"vertical-alignment\"\u003e\u003c/a\u003e\n## 四、垂直对齐\n\n### 1. `grid-template-areas` 属性值保持换行，并使用空格保持每列垂直对齐。\n\n例如：\n\n```css\n.foo {\n  grid-template-areas: \"header header\"\n                       \"nav    main\"\n                       \"footer ....\";\n\n}\n```\n\n### 2. `grid`、`grid-template` 属性值保持换行，并使用空格保持每列垂直对齐。\n\n例如：\n\n```css\n.foo {\n  grid-template: [header-left] \"header header\" 30px [header-right]\n                 [main-left]   \"nav    main\"   1fr  [main-right]\n                 [footer-left] \"nav    footer\" 30px [footer-right]\n                 / 120px 1fr;\n  grid: \"hd hd hd hd   hd   hd   hd   hd   hd\"   minmax(100px, auto)\n        \"sd sd sd main main main main main main\" minmax(100px, auto)\n        \"ft ft ft ft   ft   ft   ft   ft   ft\"   minmax(100px, auto)\n        / 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr 1fr;\n}\n```\n\n### 3. 包含多个前缀的声明不强制对齐。\n\n通常可以写作这样：\n\n```css\n.selector {\n  -webkit-transition: .3s ease;\n  -moz-transition: .3s ease;\n  -ms-transition: .3s ease;\n  -o-transition: .3s ease;\n  transition: .3s ease;\n}\n```\n\n如果使用 CSS 预处理器或后处理器，推荐以冒号对齐，使代码更加美观。[autoprefixer](https://github.com/postcss/autoprefixer#visual-cascade) 中默认开启这种风格，请保证 ```cascade``` 参数为 true。\n\n```css\n.selector {\n  -webkit-transition: .3s ease;\n     -moz-transition: .3s ease;\n      -ms-transition: .3s ease;\n       -o-transition: .3s ease;\n          transition: .3s ease;\n}\n```\n\n\n### 4. 以逗号分隔的多属性值推荐放在多行，属性值开头保持一级缩进。\n\n比如多个`box-shadow`、`background`、@font-face 中的 `src` 等，这有助于提高代码的可读性，且易于生成有效的 Diff。\n\n```css\n.selector {\n  box-shadow: \n    1px 1px 5px #000,\n    0 0 6px blue,\n    2px 0 3px 5px #ccc inset;\n  background-image:\n    linear-gradient(to top right, green, blue),\n    linear-gradient(130deg, pink, lightblue),\n    linear-gradient(to right, blue, red);\n  background-size:\n    100px 20px,\n    30px, 100%;\n    cover;\n}\n\n@media\n  only screen and (-o-min-device-pixel-ratio: 2/1), /* Opera */\n  only screen and (min--moz-device-pixel-ratio: 2), /* Firefox 16 之前 */\n  only screen and (-webkit-min-device-pixel-ratio: 2), /* WebKit */\n  only screen and (min-resolution: 192dpi), /* 不支持dppx的浏览器 */\n  only screen and (min-resolution: 2dppx) /* 标准 */\n{\n  .selector {\n  \n  }\n}\n\n@font-face {\n  font-family: 'FontName'; /* IE9 */\n  src: url('FileName.eot');\n  src:\n    url('FileName.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */\n    url('FileName.woff') format('woff'), /* Chrome,Firefox */\n    url('FileName.ttf') format('truetype'), /* Chrome,Firefox,Opera,Safari,Android, iOS 4.2+ */\n    url('FileName.svg#FontName') format('svg'); /* iOS 4.1- */\n}\n```\n\n\u003ca name=\"ellipsis\"\u003e\u003c/a\u003e\n## 五、省略\n\n* 不推荐省略 0 值单位，原因如下：\n  1. CSS 规范中可以省略单位只有 `[\u003clength-percentage\u003e](https://drafts.csswg.org/css-values-3/#typedef-length-percentage)`，其他比如角度单位 `deg` 在 Chrome 中可以省略，\n  这是浏览器的 Bug。\n  注：可以省略的单位包括：`%|em|ex|ch|rem|vw|vh|vmin|vmax|cm|mm|q|in|pt|pc|px` 以及 [CSS Values 4]() 新增的：`cap|ic|lh|rlh|vi|vb`\n  \n  2. 大部分编辑中都可以通过快捷键快速加减数值，如果省略了单位就会失去这个便捷性。\n\n* 如无必要，省略 url 中的引号。\n* 省略 ```font-family``` 内中文字体名称的引号。\n* 不强制要求缩写属性，`font`，`background`，`margin`，推荐使用工具自动合并，比如 [clean-css](https://github.com/jakubpawlowicz/clean-css)、[cssnano](https://github.com/ben-eb/cssnano)。\n* 不强制要求缩写颜色中的16进制写法。\n* 不要省略选择器内最后一个声明末尾的分号。  \n多人协作时，如果他人新增了其他代码很可能没有注意到上一行末尾没有写分号，导致 CSS 解析错误。\n\n不推荐的写法：\n\n```css\n.selector {\n  background: url(\"../foo.png\");\n  transform: rotate3d(1, 1, 1, 0);\n  width: 100px\n}\n```\n\n推荐的写法：\n\n```css\n.selector {\n  background: url(../foo.png);\n  transform: rotate3d(1, 1, 1, 0deg);\n  width: 100px;\n}\n```\n\n\u003ca name=\"shorthand\"\u003e\u003c/a\u003e\n## 六、缩写规则\n\n待更新……\n\n\u003ca name=\"order\"\u003e\u003c/a\u003e\n## 七、书写顺序\n\n### 1. 不强制要求声明的书写顺序。\n\n如果团队规范有要求，建议使用工具来自动化排序，比如 [CSScomb](http://csscomb.com/)，或者使用 @wangjeaf 开发的  [ckstyle](https://github.com/wangjeaf/ckstyle-node/)\n推荐以声明的特性作为分组，不同分组间保留一个空行，例如：\n\n```css\n.dialog {\n  /* 定位 */\n  margin: auto;\n  position: absolute;\n  top: 0;\n  bottom: 0;\n  right: 0;\n  left: 0;    \n\n  /* 盒模型 */\n  width: 500px;\n  height: 300px;\n  padding: 10px 20px;\n\n  /* 皮肤 */\n  background: #FFF;\n  color: #333;\n  border: 1px solid;\n  border-radius: 5px;                      \n}\n```\n\n### 2. 无前缀属性一定要写在最后\n\n由于 CSS 后面的属性会覆盖前面的，无前缀属性写在最后可以保证浏览器一旦支持了，则用标准的无前缀属性来渲染。\n\n不推荐的写法：\n\n```css\n.foo {\n  -webkit-border-radius: 6px;\n  border-radius: 6px;\n  -moz-border-radius: 6px;\n}\n```\n\n推荐的写法：\n\n```css\n.foo {\n  -webkit-border-radius: 6px;\n  -moz-border-radius: 6px;\n  border-radius: 6px; \n}\n```\n\n\u003ca name=\"selector-rule\"\u003e\u003c/a\u003e\n## 八、选择器使用规则\n\n### 1. 可以使用 `*` 通用选择器。\n\n`*` 通用选择器效率低是一个误区，如有必要可以使用。测试文章[关于css通配符性能问题不完全测试](http://i.wanz.im/2012/01/03/performance_testing_about_css_universal_selector/)\n\n例如：\n\n```css\n* {\n  -webkit-box-sizing: border-box;\n  -moz-box-sizing: border-box;\n  box-sizing: border-box; \n}\n```\n\n### 2. 不要在选择器末尾使用 `*` 通用选择器。\n\nCSS 选择器匹配规则是从右往左，例如：\n\n```css\n.mod .foo * {\n  border-radius: 6px;\n}\n```\n\n### 3. 如果是页面唯一的元素请使用 ID 选择器。\n\n引用[为后代选择器和ID选择器而辩护](http://hax.iteye.com/blog/1850571)\n\n\u003e 我一直以来所主张的使用 id 的方式，其实就是 HTML5 新增元素的前身。2000 年时，我们没有 footer 元素，为了给该div中的内容赋以结构上的意义，我们这样写：div id=\"footer\"。今天，根据人们访问我们网站所用的浏览器和设备，我们可以选择用 HTML5 的 footer 元素替代老方式。但若是我们不能使用 HTML5 元素，使用 id 也没有什么不对的。\n\n但应避免使用多个 ID 选择器。\n\n不推荐的写法：\n\n```css\n#header #search {\n  float: right;\n}\n```\n\n推荐的写法：\n\n```css\n#search {\n  float: right;\n}\n```\n\n### 4. 避免重复修饰选择器\n\n在一定意义上，这会降低选择器性能。\n\n不推荐的写法：\n\n```css\ndiv#search {\n  float: right;\n}\n\nul.nav {\n  overflow: hidden;\n}\n```\n\n推荐的写法：\n\n```css\n#search {\n  float: right;\n}\n\n.nav {\n  overflow: hidden;\n}\n```\n\n### 5. 所有[组合选择器](https://drafts.csswg.org/selectors-3/#combinators)（`\u003e, +, ~, \u003e\u003e`）前后保留一个空格（以空格表示的后代选择器除外）\n\n不推荐的写法：\n\n```css\n.foo\u003e.bar+div~#baz {\n  color: blue;\n}\n```\n\n推荐的写法：\n\n```css\n.foo \u003e .bar + div ~ #baz {\n  color: blue;\n}\n```\n\n\u003ca name=\"fonts\"\u003e\u003c/a\u003e\n## 九、字体\n\n待更新\n\n\u003ca name=\"other\"\u003e\u003c/a\u003e\n## 十、其他\n\n* 如果需要 CSS Hacks，需详细注明解决什么问题。\n* 尽量避免使用 IE 中的 CSS filters。\n* `font-weight`普通字重使用`normal`，加粗使用`bold`。大部分字体只有两个字重，所以  \n不建议使用容易混淆的数值表示方法。\n* 如无特别精确的要求，推荐使用不带单位的`line-height`，这样当前元素的行高只与自身`font-size`成比例关系，使排版更加灵活。例如`line-height:1.5`\n\u003cstrong\u003eline-height: 1.5 ≠ line-height: 150%\u003c/strong\u003e \n\n```html\n\u003cdiv class=\"box\"\u003e\n  \u003cp\u003eline-height\u003c/p\u003e\n\u003c/div\u003e\n```\n\n```css\n.box {\n  line-height: 50px;\n  font-size: 20px;  \n}\n/**\n * 这里 p 的行高直接继承父元素的行高，最终\n   p { line-height: 50px; }\n */\np {\n  font-size: 40px;\n}\n```\n\n```css\n.box {\n  line-height: 150%;\n  font-size: 20px;  \n}\n/**\n * p 的行高计算过程为：\n * 1. 先计算出父元素的行高（150% * 20px = 30px）\n * 2. 然后 p 继承父元素的行高，最终\n   p { line-height: 30px; }\n */\np {\n  font-size: 40px;\n}\n```\n\n```css\n.box {\n  line-height: 1.5;\n  font-size: 20px;  \n}\n/**\n * p 的行高计算过程为：\n * 1. 先继承父元素的 1.5（1.5 * 40px = 60px）\n * 2. 然后乘以 p 的 font-size，最终\n   p { line-height: 60px; }\n */\np {\n  font-size: 40px;\n}\n```\n\n\u003ca name=\"preprocessors\"\u003e\u003c/a\u003e\n## 十一、预处理工具\n\n不同的 CSS 预处理工具有着不同的特性、功能以及语法。编码习惯应当根据使用的预处理工具进行扩展，\n以适应其特有的功能。推荐在使用 SCSS 时遵守以下指导。\n\n* 将嵌套深度限制在1级。对于超过2级的嵌套，给予重新评估。这可以避免出现过于详实的 CSS 选择器。\n* 避免大量的嵌套规则。当可读性受到影响时，将之打断。推荐避免出现多于20行的嵌套规则出现。\n* 始终将@extend语句放在声明块的第一行。\n* 如果可以的话，将@include语句放在声明块的顶部，紧接着@extend语句的位置。\n* 考虑在自定义函数的名字前加上x-或其它形式的前缀。这有助于避免将自己的函数和 CSS 的原生函数混淆，  \n或函数命名与库函数冲突。\n\n```css\n.selector {\n  @extend .other-rule;\n  @include clearfix();\n  @include box-sizing(border-box);\n  width: x-grid-unit(1);\n  // 其他声明\n}\n```\n\n\u003ca name=\"postprocessors\"\u003e\u003c/a\u003e\n## 十二、后处理工具\n\n随着 PostCSS 的不断完善，各种插件如雨后春笋般的涌现出来，其中不乏一些很优秀的插件。\n\n1. 面向未来的 CSS\n    * [Autoprefixer](https://github.com/postcss/autoprefixer) 根据 [caniuse](http://caniuse.com/) 的数据自动增加前缀，解放双手的利器（🌟🌟🌟 推荐👍 ）\n    * [postcss-cssnext](http://cssnext.io/) 支持大量浏览器未实现的标准特性（CSS 变量，嵌套等）（🌟🌟 推荐👍 ）\n\n\n2. 格式化工具\n    * [stylefmt](https://github.com/morishitter/stylefmt) 支持 CSS、SCSS 等多种语法，支持 stylelint（🌟🌟🌟 推荐👍 ）\n    * [perfectionist](https://github.com/ben-eb/perfectionist) 支持一些 `stylefmt` 不支持的语法（🌟🌟 推荐）\n    * [sublime-perfectionist](https://github.com/yisibl/sublime-perfectionist)\n    * [sublime-stylefmt](https://github.com/dmnsgn/sublime-stylefmt)\n\n3. Linter 工具\n    * [stylelint](https://github.com/stylelint/stylelint)（🌟🌟🌟 推荐👍 ）\n    * [CSSLint](https://github.com/CSSLint/csslint)\n    * [scss-lint](https://github.com/brigade/scss-lint)\n\n4. 压缩代码工具\n    * [cssnano](https://github.com/ben-eb/cssnano) 后起之秀，支持规则更多（🌟🌟🌟 推荐👍 ）\n    * [clean-css](https://github.com/jakubpawlowicz/clean-css) 老牌压缩工具，速度更快（🌟🌟🌟 推荐）\n    * [CSSWring](https://github.com/hail2u/node-csswring)\n    * [postcss-csso](https://github.com/lahmatiy/postcss-csso)\n  \n    附：[CSS 压缩工具对比](http://goalsmashers.github.io/css-minification-benchmark/)\n5. 删除页面未使用 CSS\n    * [uncss](https://github.com/giakki/uncss) 使用 PhantomJS 和 `document.querySelector` 动态筛除未使用的 CSS（🌟🌟🌟 推荐👍 ）\n    * [css-razor](https://github.com/tscanlin/css-razor) 仅仅静态检测 HTML 中未使用的 CSS\n\n\u003ca name=\"organization\"\u003e\u003c/a\u003e\n## 十三、代码组织\n\n待更新……\n\n\u003ca name=\"build\"\u003e\u003c/a\u003e\n## 十四、构建部署\n\n待更新……\n\n\n\u003ca name=\"license\"\u003e\u003c/a\u003e\n## 许可\n\nMIT License\n\nCopyright (c) 2013-2017 一丝(@yisibl)\n\n新浪微博： http://weibo.com/jieorlin/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcssdream%2Fcss-creating","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcssdream%2Fcss-creating","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcssdream%2Fcss-creating/lists"}