diff --git a/README copy 2.md b/README copy 2.md new file mode 100644 index 0000000..9e49a2c --- /dev/null +++ b/README copy 2.md @@ -0,0 +1,154 @@ +# CSS规则 + +## 术语 + +### 规则声明 +“规则声明”是赋予一个选择器(或一组选择器)以及一组属性的名称。这是一个例子: +```css +.listing { + font-size: 18px; + line-height: 1.2; +} +``` +### 选择器 +在规则声明中,“选择器”是确定 DOM 树中哪些元素将由定义的属性设置样式的位。选择器可以匹配 HTML 元素,以及元素的类、ID 或其任何属性。以下是选择器的一些示例: +```css +.my-element-class { + /* ... */ +} +[aria-hidden] { + /* ... */ +} +``` +### 特性 +最后,属性赋予规则声明中选定的元素其样式。属性是键值对,规则声明可以包含一个或多个属性声明。属性声明如下所示: + +```css +/* some selector */ { + background: #f1f1f1; + color: #333; +} +``` + +## CSS + +### 格式化 +* 使用软制表符(2 个空格)进行缩进。 +* 在类名中优先使用破折号而不是驼峰式命名。 + 如果您使用 BEM,下划线和 PascalCasing 是可以的(请参阅下面的OOCSS 和 BEM)。 +* 不要使用 ID 选择器。 +* 在规则声明中使用多个选择器时,为每个选择器指定自己的行。 +* 在规则声明中的左大括号之前放置一个空格{。 +* 在属性中,在字符后面而不是前面放置一个空格:。 +* 将规则声明的右大括号放在}新行上。 +* 在规则声明之间放置空行。 + +**坏的** +```css +.avatar{ + border-radius:50%; + border:2px solid white; } +.no, .nope, .not_good { + // ... +} +#lol-no { + // ... +} +``` +**好的** +```css +.avatar { + border-radius: 50%; + border: 2px solid white; +} + +.one, +.selector, +.per-line { + // ... +} +``` +### 评论 +* 能用行注释(//在 Sass-land 中)就不用块注释。 + +### OOCSS 和 BEM +出于以下原因,我们鼓励 OOCSS 和 BEM 的某种组合: + +* 它有助于在 CSS 和 HTML 之间创建清晰、严格的关系 +* 它帮助我们创建可重用、可组合的组件 +* 它允许更少的嵌套和更低的特异性 +* 它有助于构建可扩展的样式表 + +**OOCSS** + +OOCSS或“面向对象的 CSS”是一种编写 CSS 的方法,它鼓励您将样式表视为“对象”的集合:可在整个网站中独立使用的可重用、可重复的片段来增强CSS代码的可维护性和重用性的方法论。 + +**BEM** +BEM或“块元素修饰符”是HTML 和 CSS 中类的命名约定,考虑到了大型代码库和可扩展性,并且可以作为实施 OOCSS 的一套可靠指南。 + +推荐使用 PascalCased“块”的 BEM 变体,与组件(例如 React)结合使用时效果特别好。下划线和破折号仍然用于修饰符和子元素。 + +**例子** + +```jsx +// ListingCard.jsx +function ListingCard() { + return ( + + ); +} +``` +```css +/* ListingCard.css */ +.ListingCard { } +.ListingCard--featured { } +.ListingCard__title { } +.ListingCard__content { } +``` + * .ListingCard是“块”并代表更高级别的组件 + * .ListingCard__title是一个“元素”,代表它的后代,.ListingCard有助于将块组成一个整体。 + * .ListingCard--featured是一个“修饰符”,代表块上的不同状态或变化.ListingCard。 + +## ID选择器 +尽量避免在css中使用ID选择器:虽然可以在 CSS 中通过 ID 选择元素,但它通常应被视为反模式。ID 选择器为您的规则声明引入了不必要的高水平特异性,并且它们不可重用。 + +## JavaScript 钩子 +避免在 CSS 和 JavaScript 中绑定到同一个类。将两者混为一谈通常会导致开发人员在重构过程中浪费时间,因为开发人员必须交叉引用他们正在更改的每个类,而最糟糕的是,开发人员因担心破坏功能而不敢进行更改。 + +我们建议创建要绑定的特定于 JavaScript 的类,前缀为.js-: + + + +### 变量 +优先使用破折号命名的变量名称(例如$my-variable),而不是驼峰命名或蛇形命名的变量名称。可以在仅在同一文件中使用的变量名前添加下划线(例如$_my-variable)。 + +### 混入 +Mixins 应该用于干燥代码、增加清晰度或抽象复杂性——就像命名良好的函数一样。不接受参数的 Mixin 对此很有用,但请注意,如果您不压缩有效负载(例如 gzip),这可能会导致结果样式中出现不必要的代码重复。 +--todo +### 扩展指令 +@extend应避免使用,因为它具有不直观且潜在危险的行为,尤其是与嵌套选择器一起使用时。如果选择器的顺序后来发生变化(例如,如果它们位于其他文件中并且文件加载的顺序发生变化),即使扩展顶级占位符选择器也可能会导致问题。Gzipping 应该可以处理您通过使用 获得的大部分节省@extend,并且您可以使用 mixin 很好地干燥您的样式表。 +--todo + +### 嵌套选择器 +选择器的嵌套深度不要超过三层! +```css +.page-container { + .content { + .profile { + // STOP! + } + } +} +``` +永远不要嵌套 ID 选择器:如果您发现自己这样做,则需要重新审视您的标记,或者弄清楚为什么需要如此强的特异性。如果您正在编写格式良好的 HTML 和 CSS,则永远不需要这样做。 + +### less +--todo \ No newline at end of file diff --git a/README copy.md b/README copy.md new file mode 100644 index 0000000..cfdeafa --- /dev/null +++ b/README copy.md @@ -0,0 +1,151 @@ +# CSS规则 + +## 术语 + +### 规则声明 +“规则声明”是赋予一个选择器(或一组选择器)以及一组属性的名称。这是一个例子: +```css +.listing { + font-size: 18px; + line-height: 1.2; +} +``` +### 选择器 +在规则声明中,“选择器”是确定 DOM 树中哪些元素将由定义的属性设置样式的位。选择器可以匹配 HTML 元素,以及元素的类、ID 或其任何属性。以下是选择器的一些示例: +```css +.my-element-class { + /* ... */ +} +[aria-hidden] { + /* ... */ +} +``` +### 特性 +最后,属性赋予规则声明中选定的元素其样式。属性是键值对,规则声明可以包含一个或多个属性声明。属性声明如下所示: + +```css +/* some selector */ { + background: #f1f1f1; + color: #333; +} +``` + +## CSS + +### 格式化 +* 使用软制表符(2 个空格)进行缩进。 +* 在类名中优先使用破折号而不是驼峰式命名。 + 如果您使用 BEM,下划线和 PascalCasing 是可以的(请参阅下面的OOCSS 和 BEM)。 +* 不要使用 ID 选择器。 +* 在规则声明中使用多个选择器时,为每个选择器指定自己的行。 +* 在规则声明中的左大括号之前放置一个空格{。 +* 在属性中,在字符后面而不是前面放置一个空格:。 +* 将规则声明的右大括号放在}新行上。 +* 在规则声明之间放置空行。 + +**坏的** +```css +.avatar{ + border-radius:50%; + border:2px solid white; } +.no, .nope, .not_good { + // ... +} +#lol-no { + // ... +} +``` +**好的** +```css +.avatar { + border-radius: 50%; + border: 2px solid white; +} + +.one, +.selector, +.per-line { + // ... +} +``` +### 评论 +* 能用行注释(//在 Sass-land 中)就不用块注释。 + +### OOCSS 和 BEM +出于以下原因,我们鼓励 OOCSS 和 BEM 的某种组合: + +* 它有助于在 CSS 和 HTML 之间创建清晰、严格的关系 +* 它帮助我们创建可重用、可组合的组件 +* 它允许更少的嵌套和更低的特异性 +* 它有助于构建可扩展的样式表 + +**OOCSS** + +OOCSS或“面向对象的 CSS”是一种编写 CSS 的方法,它鼓励您将样式表视为“对象”的集合:可在整个网站中独立使用的可重用、可重复的片段来增强CSS代码的可维护性和重用性的方法论。 + +**BEM** +BEM或“块元素修饰符”是HTML 和 CSS 中类的命名约定,考虑到了大型代码库和可扩展性,并且可以作为实施 OOCSS 的一套可靠指南。 + +推荐使用 PascalCased“块”的 BEM 变体,与组件(例如 React)结合使用时效果特别好。下划线和破折号仍然用于修饰符和子元素。 + +**例子** + +```jsx +// ListingCard.jsx +function ListingCard() { + return ( + + ); +} +``` +```css +/* ListingCard.css */ +.ListingCard { } +.ListingCard--featured { } +.ListingCard__title { } +.ListingCard__content { } +``` + * .ListingCard是“块”并代表更高级别的组件 + * .ListingCard__title是一个“元素”,代表它的后代,.ListingCard有助于将块组成一个整体。 + * .ListingCard--featured是一个“修饰符”,代表块上的不同状态或变化.ListingCard。 + +## ID选择器 +尽量避免在css中使用ID选择器:虽然可以在 CSS 中通过 ID 选择元素,但它通常应被视为反模式。ID 选择器为您的规则声明引入了不必要的高水平特异性,并且它们不可重用。 + +## JavaScript 钩子 +避免在 CSS 和 JavaScript 中绑定到同一个类。将两者混为一谈通常会导致开发人员在重构过程中浪费时间,因为开发人员必须交叉引用他们正在更改的每个类,而最糟糕的是,开发人员因担心破坏功能而不敢进行更改。 + +我们建议创建要绑定的特定于 JavaScript 的类,前缀为.js-: + + + +### 变量 +优先使用破折号命名的变量名称(例如$my-variable),而不是驼峰命名或蛇形命名的变量名称。可以在仅在同一文件中使用的变量名前添加下划线(例如$_my-variable)。 + +### 混入 +Mixins 应该用于干燥代码、增加清晰度或抽象复杂性——就像命名良好的函数一样。不接受参数的 Mixin 对此很有用,但请注意,如果您不压缩有效负载(例如 gzip),这可能会导致结果样式中出现不必要的代码重复。 +--todo +### 扩展指令 +@extend应避免使用,因为它具有不直观且潜在危险的行为,尤其是与嵌套选择器一起使用时。如果选择器的顺序后来发生变化(例如,如果它们位于其他文件中并且文件加载的顺序发生变化),即使扩展顶级占位符选择器也可能会导致问题。Gzipping 应该可以处理您通过使用 获得的大部分节省@extend,并且您可以使用 mixin 很好地干燥您的样式表。 +--todo + +### 嵌套选择器 +选择器的嵌套深度不要超过三层! +```css +.page-container { + .content { + .profile { + // STOP! + } + } +} +``` +永远不要嵌套 ID 选择器:如果您发现自己这样做,则需要重新审视您的标记,或者弄清楚为什么需要如此强的特异性。如果您正在编写格式良好的 HTML 和 CSS,则永远不需要这样做。 diff --git a/README-old.md b/README-old.md new file mode 100644 index 0000000..6657a5e --- /dev/null +++ b/README-old.md @@ -0,0 +1,330 @@ +# Airbnb CSS / Sass Styleguide + +*A mostly reasonable approach to CSS and Sass* + +## Table of Contents + +1. [Terminology](#terminology) + - [Rule Declaration](#rule-declaration) + - [Selectors](#selectors) + - [Properties](#properties) +1. [CSS](#css) + - [Formatting](#formatting) + - [Comments](#comments) + - [OOCSS and BEM](#oocss-and-bem) + - [ID Selectors](#id-selectors) + - [JavaScript hooks](#javascript-hooks) + - [Border](#border) +1. [Sass](#sass) + - [Syntax](#syntax) + - [Ordering](#ordering-of-property-declarations) + - [Variables](#variables) + - [Mixins](#mixins) + - [Extend directive](#extend-directive) + - [Nested selectors](#nested-selectors) +1. [Translation](#translation) +1. [License](#license) + +## Terminology + +### Rule declaration + +A “rule declaration” is the name given to a selector (or a group of selectors) with an accompanying group of properties. Here's an example: + +```css +.listing { + font-size: 18px; + line-height: 1.2; +} +``` + +### Selectors + +In a rule declaration, “selectors” are the bits that determine which elements in the DOM tree will be styled by the defined properties. Selectors can match HTML elements, as well as an element's class, ID, or any of its attributes. Here are some examples of selectors: + +```css +.my-element-class { + /* ... */ +} + +[aria-hidden] { + /* ... */ +} +``` + +### Properties + +Finally, properties are what give the selected elements of a rule declaration their style. Properties are key-value pairs, and a rule declaration can contain one or more property declarations. Property declarations look like this: + +```css +/* some selector */ { + background: #f1f1f1; + color: #333; +} +``` + +**[⬆ back to top](#table-of-contents)** + +## CSS + +### Formatting + +* Use soft tabs (2 spaces) for indentation. +* Prefer dashes over camelCasing in class names. + - Underscores and PascalCasing are okay if you are using BEM (see [OOCSS and BEM](#oocss-and-bem) below). +* Do not use ID selectors. +* When using multiple selectors in a rule declaration, give each selector its own line. +* Put a space before the opening brace `{` in rule declarations. +* In properties, put a space after, but not before, the `:` character. +* Put closing braces `}` of rule declarations on a new line. +* Put blank lines between rule declarations. + +**Bad** + +```css +.avatar{ + border-radius:50%; + border:2px solid white; } +.no, .nope, .not_good { + // ... +} +#lol-no { + // ... +} +``` + +**Good** + +```css +.avatar { + border-radius: 50%; + border: 2px solid white; +} + +.one, +.selector, +.per-line { + // ... +} +``` + +### Comments + +* Prefer line comments (`//` in Sass-land) to block comments. +* Prefer comments on their own line. Avoid end-of-line comments. +* Write detailed comments for code that isn't self-documenting: + - Uses of z-index + - Compatibility or browser-specific hacks + +### OOCSS and BEM + +We encourage some combination of OOCSS and BEM for these reasons: + + * It helps create clear, strict relationships between CSS and HTML + * It helps us create reusable, composable components + * It allows for less nesting and lower specificity + * It helps in building scalable stylesheets + +**OOCSS**, or “Object Oriented CSS”, is an approach for writing CSS that encourages you to think about your stylesheets as a collection of “objects”: reusable, repeatable snippets that can be used independently throughout a website. + + * Nicole Sullivan's [OOCSS wiki](https://github.com/stubbornella/oocss/wiki) + * Smashing Magazine's [Introduction to OOCSS](http://www.smashingmagazine.com/2011/12/12/an-introduction-to-object-oriented-css-oocss/) + +**BEM**, or “Block-Element-Modifier”, is a _naming convention_ for classes in HTML and CSS. It was originally developed by Yandex with large codebases and scalability in mind, and can serve as a solid set of guidelines for implementing OOCSS. + + * CSS Trick's [BEM 101](https://css-tricks.com/bem-101/) + * Harry Roberts' [introduction to BEM](http://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/) + +We recommend a variant of BEM with PascalCased “blocks”, which works particularly well when combined with components (e.g. React). Underscores and dashes are still used for modifiers and children. + +**Example** + +```jsx +// ListingCard.jsx +function ListingCard() { + return ( + + ); +} +``` + +```css +/* ListingCard.css */ +.ListingCard { } +.ListingCard--featured { } +.ListingCard__title { } +.ListingCard__content { } +``` + + * `.ListingCard` is the “block” and represents the higher-level component + * `.ListingCard__title` is an “element” and represents a descendant of `.ListingCard` that helps compose the block as a whole. + * `.ListingCard--featured` is a “modifier” and represents a different state or variation on the `.ListingCard` block. + +### ID selectors + +While it is possible to select elements by ID in CSS, it should generally be considered an anti-pattern. ID selectors introduce an unnecessarily high level of [specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity) to your rule declarations, and they are not reusable. + +For more on this subject, read [CSS Wizardry's article](http://csswizardry.com/2014/07/hacks-for-dealing-with-specificity/) on dealing with specificity. + +### JavaScript hooks + +Avoid binding to the same class in both your CSS and JavaScript. Conflating the two often leads to, at a minimum, time wasted during refactoring when a developer must cross-reference each class they are changing, and at its worst, developers being afraid to make changes for fear of breaking functionality. + +We recommend creating JavaScript-specific classes to bind to, prefixed with `.js-`: + +```html + +``` + +### Border + +Use `0` instead of `none` to specify that a style has no border. + +**Bad** + +```css +.foo { + border: none; +} +``` + +**Good** + +```css +.foo { + border: 0; +} +``` +**[⬆ back to top](#table-of-contents)** + +## Sass + +### Syntax + +* Use the `.scss` syntax, never the original `.sass` syntax +* Order your regular CSS and `@include` declarations logically (see below) + +### Ordering of property declarations + +1. Property declarations + + List all standard property declarations, anything that isn't an `@include` or a nested selector. + + ```scss + .btn-green { + background: green; + font-weight: bold; + // ... + } + ``` + +2. `@include` declarations + + Grouping `@include`s at the end makes it easier to read the entire selector. + + ```scss + .btn-green { + background: green; + font-weight: bold; + @include transition(background 0.5s ease); + // ... + } + ``` + +3. Nested selectors + + Nested selectors, _if necessary_, go last, and nothing goes after them. Add whitespace between your rule declarations and nested selectors, as well as between adjacent nested selectors. Apply the same guidelines as above to your nested selectors. + + ```scss + .btn { + background: green; + font-weight: bold; + @include transition(background 0.5s ease); + + .icon { + margin-right: 10px; + } + } + ``` + +### Variables + +Prefer dash-cased variable names (e.g. `$my-variable`) over camelCased or snake_cased variable names. It is acceptable to prefix variable names that are intended to be used only within the same file with an underscore (e.g. `$_my-variable`). + +### Mixins + +Mixins should be used to DRY up your code, add clarity, or abstract complexity--in much the same way as well-named functions. Mixins that accept no arguments can be useful for this, but note that if you are not compressing your payload (e.g. gzip), this may contribute to unnecessary code duplication in the resulting styles. + +### Extend directive + +`@extend` should be avoided because it has unintuitive and potentially dangerous behavior, especially when used with nested selectors. Even extending top-level placeholder selectors can cause problems if the order of selectors ends up changing later (e.g. if they are in other files and the order the files are loaded shifts). Gzipping should handle most of the savings you would have gained by using `@extend`, and you can DRY up your stylesheets nicely with mixins. + +### Nested selectors + +**Do not nest selectors more than three levels deep!** + +```scss +.page-container { + .content { + .profile { + // STOP! + } + } +} +``` + +When selectors become this long, you're likely writing CSS that is: + +* Strongly coupled to the HTML (fragile) *—OR—* +* Overly specific (powerful) *—OR—* +* Not reusable + + +Again: **never nest ID selectors!** + +If you must use an ID selector in the first place (and you should really try not to), they should never be nested. If you find yourself doing this, you need to revisit your markup, or figure out why such strong specificity is needed. If you are writing well formed HTML and CSS, you should **never** need to do this. + +**[⬆ back to top](#table-of-contents)** + +## Translation + + This style guide is also available in other languages: + + - ![id](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Indonesia.png) **Bahasa Indonesia**: [mazipan/css-style-guide](https://github.com/mazipan/css-style-guide) + - ![tw](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Taiwan.png) **Chinese (Traditional)**: [ArvinH/css-style-guide](https://github.com/ArvinH/css-style-guide) + - ![cn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/China.png) **Chinese (Simplified)**: [Zhangjd/css-style-guide](https://github.com/Zhangjd/css-style-guide) + - ![fr](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/France.png) **French**: [mat-u/css-style-guide](https://github.com/mat-u/css-style-guide) + - ![ka](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Georgia.png) **Georgian**: [DavidKadaria/css-style-guide](https://github.com/davidkadaria/css-style-guide) + - ![ja](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Japan.png) **Japanese**: [nao215/css-style-guide](https://github.com/nao215/css-style-guide) + - ![ko](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/South-Korea.png) **Korean**: [CodeMakeBros/css-style-guide](https://github.com/CodeMakeBros/css-style-guide) + - ![PT-BR](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Brazil.png) **Portuguese (Brazil)**: [felipevolpatto/css-style-guide](https://github.com/felipevolpatto/css-style-guide) + - ![pt-PT](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Portugal.png) **Portuguese (Portugal)**: [SandroMiguel/airbnb-css-style-guide](https://github.com/SandroMiguel/airbnb-css-style-guide) + - ![ru](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Russia.png) **Russian**: [rtplv/airbnb-css-ru](https://github.com/rtplv/airbnb-css-ru) + - ![es](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Spain.png) **Spanish**: [ismamz/guia-de-estilo-css](https://github.com/ismamz/guia-de-estilo-css) + - ![vn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Vietnam.png) **Vietnamese**: [trungk18/css-style-guide](https://github.com/trungk18/css-style-guide) + - ![vn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Italy.png) **Italian**: [antoniofull/linee-guida-css](https://github.com/antoniofull/linee-guida-css) + - ![de](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Germany.png) **German**: [tderflinger/css-styleguide](https://github.com/tderflinger/css-styleguide) + +**[⬆ back to top](#table-of-contents)** + +## License + +(The MIT License) + +Copyright (c) 2015 Airbnb + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + +**[⬆ back to top](#table-of-contents)** diff --git a/README.md b/README.md index 6657a5e..9e49a2c 100644 --- a/README.md +++ b/README.md @@ -1,60 +1,27 @@ -# Airbnb CSS / Sass Styleguide +# CSS规则 -*A mostly reasonable approach to CSS and Sass* - -## Table of Contents - -1. [Terminology](#terminology) - - [Rule Declaration](#rule-declaration) - - [Selectors](#selectors) - - [Properties](#properties) -1. [CSS](#css) - - [Formatting](#formatting) - - [Comments](#comments) - - [OOCSS and BEM](#oocss-and-bem) - - [ID Selectors](#id-selectors) - - [JavaScript hooks](#javascript-hooks) - - [Border](#border) -1. [Sass](#sass) - - [Syntax](#syntax) - - [Ordering](#ordering-of-property-declarations) - - [Variables](#variables) - - [Mixins](#mixins) - - [Extend directive](#extend-directive) - - [Nested selectors](#nested-selectors) -1. [Translation](#translation) -1. [License](#license) - -## Terminology - -### Rule declaration - -A “rule declaration” is the name given to a selector (or a group of selectors) with an accompanying group of properties. Here's an example: +## 术语 +### 规则声明 +“规则声明”是赋予一个选择器(或一组选择器)以及一组属性的名称。这是一个例子: ```css .listing { font-size: 18px; line-height: 1.2; } ``` - -### Selectors - -In a rule declaration, “selectors” are the bits that determine which elements in the DOM tree will be styled by the defined properties. Selectors can match HTML elements, as well as an element's class, ID, or any of its attributes. Here are some examples of selectors: - +### 选择器 +在规则声明中,“选择器”是确定 DOM 树中哪些元素将由定义的属性设置样式的位。选择器可以匹配 HTML 元素,以及元素的类、ID 或其任何属性。以下是选择器的一些示例: ```css .my-element-class { /* ... */ } - [aria-hidden] { /* ... */ } ``` - -### Properties - -Finally, properties are what give the selected elements of a rule declaration their style. Properties are key-value pairs, and a rule declaration can contain one or more property declarations. Property declarations look like this: +### 特性 +最后,属性赋予规则声明中选定的元素其样式。属性是键值对,规则声明可以包含一个或多个属性声明。属性声明如下所示: ```css /* some selector */ { @@ -63,24 +30,20 @@ Finally, properties are what give the selected elements of a rule declaration th } ``` -**[⬆ back to top](#table-of-contents)** - ## CSS -### Formatting - -* Use soft tabs (2 spaces) for indentation. -* Prefer dashes over camelCasing in class names. - - Underscores and PascalCasing are okay if you are using BEM (see [OOCSS and BEM](#oocss-and-bem) below). -* Do not use ID selectors. -* When using multiple selectors in a rule declaration, give each selector its own line. -* Put a space before the opening brace `{` in rule declarations. -* In properties, put a space after, but not before, the `:` character. -* Put closing braces `}` of rule declarations on a new line. -* Put blank lines between rule declarations. - -**Bad** - +### 格式化 +* 使用软制表符(2 个空格)进行缩进。 +* 在类名中优先使用破折号而不是驼峰式命名。 + 如果您使用 BEM,下划线和 PascalCasing 是可以的(请参阅下面的OOCSS 和 BEM)。 +* 不要使用 ID 选择器。 +* 在规则声明中使用多个选择器时,为每个选择器指定自己的行。 +* 在规则声明中的左大括号之前放置一个空格{。 +* 在属性中,在字符后面而不是前面放置一个空格:。 +* 将规则声明的右大括号放在}新行上。 +* 在规则声明之间放置空行。 + +**坏的** ```css .avatar{ border-radius:50%; @@ -92,9 +55,7 @@ Finally, properties are what give the selected elements of a rule declaration th // ... } ``` - -**Good** - +**好的** ```css .avatar { border-radius: 50%; @@ -107,37 +68,27 @@ Finally, properties are what give the selected elements of a rule declaration th // ... } ``` +### 评论 +* 能用行注释(//在 Sass-land 中)就不用块注释。 -### Comments - -* Prefer line comments (`//` in Sass-land) to block comments. -* Prefer comments on their own line. Avoid end-of-line comments. -* Write detailed comments for code that isn't self-documenting: - - Uses of z-index - - Compatibility or browser-specific hacks - -### OOCSS and BEM - -We encourage some combination of OOCSS and BEM for these reasons: +### OOCSS 和 BEM +出于以下原因,我们鼓励 OOCSS 和 BEM 的某种组合: - * It helps create clear, strict relationships between CSS and HTML - * It helps us create reusable, composable components - * It allows for less nesting and lower specificity - * It helps in building scalable stylesheets +* 它有助于在 CSS 和 HTML 之间创建清晰、严格的关系 +* 它帮助我们创建可重用、可组合的组件 +* 它允许更少的嵌套和更低的特异性 +* 它有助于构建可扩展的样式表 -**OOCSS**, or “Object Oriented CSS”, is an approach for writing CSS that encourages you to think about your stylesheets as a collection of “objects”: reusable, repeatable snippets that can be used independently throughout a website. +**OOCSS** - * Nicole Sullivan's [OOCSS wiki](https://github.com/stubbornella/oocss/wiki) - * Smashing Magazine's [Introduction to OOCSS](http://www.smashingmagazine.com/2011/12/12/an-introduction-to-object-oriented-css-oocss/) +OOCSS或“面向对象的 CSS”是一种编写 CSS 的方法,它鼓励您将样式表视为“对象”的集合:可在整个网站中独立使用的可重用、可重复的片段来增强CSS代码的可维护性和重用性的方法论。 -**BEM**, or “Block-Element-Modifier”, is a _naming convention_ for classes in HTML and CSS. It was originally developed by Yandex with large codebases and scalability in mind, and can serve as a solid set of guidelines for implementing OOCSS. +**BEM** +BEM或“块元素修饰符”是HTML 和 CSS 中类的命名约定,考虑到了大型代码库和可扩展性,并且可以作为实施 OOCSS 的一套可靠指南。 - * CSS Trick's [BEM 101](https://css-tricks.com/bem-101/) - * Harry Roberts' [introduction to BEM](http://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/) +推荐使用 PascalCased“块”的 BEM 变体,与组件(例如 React)结合使用时效果特别好。下划线和破折号仍然用于修饰符和子元素。 -We recommend a variant of BEM with PascalCased “blocks”, which works particularly well when combined with components (e.g. React). Underscores and dashes are still used for modifiers and children. - -**Example** +**例子** ```jsx // ListingCard.jsx @@ -155,7 +106,6 @@ function ListingCard() { ); } ``` - ```css /* ListingCard.css */ .ListingCard { } @@ -163,115 +113,33 @@ function ListingCard() { .ListingCard__title { } .ListingCard__content { } ``` + * .ListingCard是“块”并代表更高级别的组件 + * .ListingCard__title是一个“元素”,代表它的后代,.ListingCard有助于将块组成一个整体。 + * .ListingCard--featured是一个“修饰符”,代表块上的不同状态或变化.ListingCard。 - * `.ListingCard` is the “block” and represents the higher-level component - * `.ListingCard__title` is an “element” and represents a descendant of `.ListingCard` that helps compose the block as a whole. - * `.ListingCard--featured` is a “modifier” and represents a different state or variation on the `.ListingCard` block. - -### ID selectors - -While it is possible to select elements by ID in CSS, it should generally be considered an anti-pattern. ID selectors introduce an unnecessarily high level of [specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity) to your rule declarations, and they are not reusable. - -For more on this subject, read [CSS Wizardry's article](http://csswizardry.com/2014/07/hacks-for-dealing-with-specificity/) on dealing with specificity. +## ID选择器 +尽量避免在css中使用ID选择器:虽然可以在 CSS 中通过 ID 选择元素,但它通常应被视为反模式。ID 选择器为您的规则声明引入了不必要的高水平特异性,并且它们不可重用。 -### JavaScript hooks +## JavaScript 钩子 +避免在 CSS 和 JavaScript 中绑定到同一个类。将两者混为一谈通常会导致开发人员在重构过程中浪费时间,因为开发人员必须交叉引用他们正在更改的每个类,而最糟糕的是,开发人员因担心破坏功能而不敢进行更改。 -Avoid binding to the same class in both your CSS and JavaScript. Conflating the two often leads to, at a minimum, time wasted during refactoring when a developer must cross-reference each class they are changing, and at its worst, developers being afraid to make changes for fear of breaking functionality. +我们建议创建要绑定的特定于 JavaScript 的类,前缀为.js-: -We recommend creating JavaScript-specific classes to bind to, prefixed with `.js-`: - -```html -``` - -### Border -Use `0` instead of `none` to specify that a style has no border. +### 变量 +优先使用破折号命名的变量名称(例如$my-variable),而不是驼峰命名或蛇形命名的变量名称。可以在仅在同一文件中使用的变量名前添加下划线(例如$_my-variable)。 -**Bad** +### 混入 +Mixins 应该用于干燥代码、增加清晰度或抽象复杂性——就像命名良好的函数一样。不接受参数的 Mixin 对此很有用,但请注意,如果您不压缩有效负载(例如 gzip),这可能会导致结果样式中出现不必要的代码重复。 +--todo +### 扩展指令 +@extend应避免使用,因为它具有不直观且潜在危险的行为,尤其是与嵌套选择器一起使用时。如果选择器的顺序后来发生变化(例如,如果它们位于其他文件中并且文件加载的顺序发生变化),即使扩展顶级占位符选择器也可能会导致问题。Gzipping 应该可以处理您通过使用 获得的大部分节省@extend,并且您可以使用 mixin 很好地干燥您的样式表。 +--todo +### 嵌套选择器 +选择器的嵌套深度不要超过三层! ```css -.foo { - border: none; -} -``` - -**Good** - -```css -.foo { - border: 0; -} -``` -**[⬆ back to top](#table-of-contents)** - -## Sass - -### Syntax - -* Use the `.scss` syntax, never the original `.sass` syntax -* Order your regular CSS and `@include` declarations logically (see below) - -### Ordering of property declarations - -1. Property declarations - - List all standard property declarations, anything that isn't an `@include` or a nested selector. - - ```scss - .btn-green { - background: green; - font-weight: bold; - // ... - } - ``` - -2. `@include` declarations - - Grouping `@include`s at the end makes it easier to read the entire selector. - - ```scss - .btn-green { - background: green; - font-weight: bold; - @include transition(background 0.5s ease); - // ... - } - ``` - -3. Nested selectors - - Nested selectors, _if necessary_, go last, and nothing goes after them. Add whitespace between your rule declarations and nested selectors, as well as between adjacent nested selectors. Apply the same guidelines as above to your nested selectors. - - ```scss - .btn { - background: green; - font-weight: bold; - @include transition(background 0.5s ease); - - .icon { - margin-right: 10px; - } - } - ``` - -### Variables - -Prefer dash-cased variable names (e.g. `$my-variable`) over camelCased or snake_cased variable names. It is acceptable to prefix variable names that are intended to be used only within the same file with an underscore (e.g. `$_my-variable`). - -### Mixins - -Mixins should be used to DRY up your code, add clarity, or abstract complexity--in much the same way as well-named functions. Mixins that accept no arguments can be useful for this, but note that if you are not compressing your payload (e.g. gzip), this may contribute to unnecessary code duplication in the resulting styles. - -### Extend directive - -`@extend` should be avoided because it has unintuitive and potentially dangerous behavior, especially when used with nested selectors. Even extending top-level placeholder selectors can cause problems if the order of selectors ends up changing later (e.g. if they are in other files and the order the files are loaded shifts). Gzipping should handle most of the savings you would have gained by using `@extend`, and you can DRY up your stylesheets nicely with mixins. - -### Nested selectors - -**Do not nest selectors more than three levels deep!** - -```scss .page-container { .content { .profile { @@ -280,51 +148,7 @@ Mixins should be used to DRY up your code, add clarity, or abstract complexity-- } } ``` +永远不要嵌套 ID 选择器:如果您发现自己这样做,则需要重新审视您的标记,或者弄清楚为什么需要如此强的特异性。如果您正在编写格式良好的 HTML 和 CSS,则永远不需要这样做。 -When selectors become this long, you're likely writing CSS that is: - -* Strongly coupled to the HTML (fragile) *—OR—* -* Overly specific (powerful) *—OR—* -* Not reusable - - -Again: **never nest ID selectors!** - -If you must use an ID selector in the first place (and you should really try not to), they should never be nested. If you find yourself doing this, you need to revisit your markup, or figure out why such strong specificity is needed. If you are writing well formed HTML and CSS, you should **never** need to do this. - -**[⬆ back to top](#table-of-contents)** - -## Translation - - This style guide is also available in other languages: - - - ![id](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Indonesia.png) **Bahasa Indonesia**: [mazipan/css-style-guide](https://github.com/mazipan/css-style-guide) - - ![tw](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Taiwan.png) **Chinese (Traditional)**: [ArvinH/css-style-guide](https://github.com/ArvinH/css-style-guide) - - ![cn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/China.png) **Chinese (Simplified)**: [Zhangjd/css-style-guide](https://github.com/Zhangjd/css-style-guide) - - ![fr](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/France.png) **French**: [mat-u/css-style-guide](https://github.com/mat-u/css-style-guide) - - ![ka](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Georgia.png) **Georgian**: [DavidKadaria/css-style-guide](https://github.com/davidkadaria/css-style-guide) - - ![ja](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Japan.png) **Japanese**: [nao215/css-style-guide](https://github.com/nao215/css-style-guide) - - ![ko](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/South-Korea.png) **Korean**: [CodeMakeBros/css-style-guide](https://github.com/CodeMakeBros/css-style-guide) - - ![PT-BR](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Brazil.png) **Portuguese (Brazil)**: [felipevolpatto/css-style-guide](https://github.com/felipevolpatto/css-style-guide) - - ![pt-PT](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Portugal.png) **Portuguese (Portugal)**: [SandroMiguel/airbnb-css-style-guide](https://github.com/SandroMiguel/airbnb-css-style-guide) - - ![ru](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Russia.png) **Russian**: [rtplv/airbnb-css-ru](https://github.com/rtplv/airbnb-css-ru) - - ![es](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Spain.png) **Spanish**: [ismamz/guia-de-estilo-css](https://github.com/ismamz/guia-de-estilo-css) - - ![vn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Vietnam.png) **Vietnamese**: [trungk18/css-style-guide](https://github.com/trungk18/css-style-guide) - - ![vn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Italy.png) **Italian**: [antoniofull/linee-guida-css](https://github.com/antoniofull/linee-guida-css) - - ![de](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Germany.png) **German**: [tderflinger/css-styleguide](https://github.com/tderflinger/css-styleguide) - -**[⬆ back to top](#table-of-contents)** - -## License - -(The MIT License) - -Copyright (c) 2015 Airbnb - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -**[⬆ back to top](#table-of-contents)** +### less +--todo \ No newline at end of file