Skip to content

Commit 0ae3ce6

Browse files
committed
Update README.md
Translate into Chinese(Simplified).
1 parent 5bc0cbc commit 0ae3ce6

File tree

1 file changed

+109
-87
lines changed

1 file changed

+109
-87
lines changed

README.md

Lines changed: 109 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,37 @@
1-
# Airbnb CSS / Sass Styleguide
1+
# Airbnb CSS / Sass 风格指南
22

3-
*A mostly reasonable approach to CSS and Sass*
3+
*用更合理的方式写 CSS Sass*
44

5-
## Table of Contents
5+
翻译自 [Airbnb CSS / Sass Styleguide](https://github.com/airbnb/css)
66

7-
1. [Terminology](#terminology)
8-
- [Rule Declaration](#rule-declaration)
9-
- [Selectors](#selectors)
10-
- [Properties](#properties)
7+
## 目录
8+
9+
1. [术语](#terminology)
10+
- [规则声明](#rule-declaration)
11+
- [选择器](#selectors)
12+
- [属性](#properties)
1113
1. [CSS](#css)
12-
- [Formatting](#formatting)
13-
- [Comments](#comments)
14-
- [OOCSS and BEM](#oocss-and-bem)
15-
- [ID Selectors](#id-selectors)
16-
- [JavaScript hooks](#javascript-hooks)
17-
- [Border](#border)
14+
- [格式](#formatting)
15+
- [注释](#comments)
16+
- [OOCSS BEM](#oocss-and-bem)
17+
- [ID 选择器](#id-selectors)
18+
- [JavaScript 钩子](#javascript-hooks)
19+
- [边框](#border)
1820
1. [Sass](#sass)
19-
- [Syntax](#syntax)
20-
- [Ordering](#ordering-of-property-declarations)
21-
- [Variables](#variables)
21+
- [语法](#syntax)
22+
- [排序](#ordering-of-property-declarations)
23+
- [变量](#variables)
2224
- [Mixins](#mixins)
23-
- [Extend directive](#extend-directive)
24-
- [Nested selectors](#nested-selectors)
25+
- [扩展指令](#extend-directive)
26+
- [嵌套的选择器](#nested-selectors)
2527

26-
## Terminology
28+
<a name="terminology"></a>
29+
## 术语
2730

28-
### Rule declaration
31+
<a name="rule-declaration"></a>
32+
### 规则声明
2933

30-
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:
34+
我们把一个(或一组)选择器与其伴随的属性集合称之为“规则声明”。举个例子:
3135

3236
```css
3337
.listing {
@@ -36,9 +40,10 @@ A “rule declaration” is the name given to a selector (or a group of selector
3640
}
3741
```
3842

39-
### Selectors
43+
<a name="selectors"></a>
44+
### 选择器
4045

41-
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:
46+
在规则声明中,“选择器”部分将决定样式属性会被应用到 DOM 树中哪些元素。选择器可以匹配 HTML 元素,也可以匹配一个元素的类名、ID, 或者元素拥有的属性。以下是选择器的例子:
4247

4348
```css
4449
.my-element-class {
@@ -50,9 +55,10 @@ In a rule declaration, “selectors” are the bits that determine which element
5055
}
5156
```
5257

53-
### Properties
58+
<a name="properties"></a>
59+
### 属性
5460

55-
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:
61+
最后,属性决定了规则声明里被选择的元素将得到何种样式。属性以键值对形式存在,一个规则声明可以包含一或多个属性定义。以下是属性定义的例子:
5662

5763
```css
5864
/* some selector */ {
@@ -61,18 +67,20 @@ Finally, properties are what give the selected elements of a rule declaration th
6167
}
6268
```
6369

70+
<a name="css"></a>
6471
## CSS
6572

66-
### Formatting
73+
<a name="formatting"></a>
74+
### 格式
6775

68-
* Use soft tabs (2 spaces) for indentation
69-
* Prefer dashes over camelCasing in class names. Underscores are OK if you're using BEM (see [OOCSS and BEM](#oocss-and-bem) below).
70-
* Do not use ID selectors
71-
* When using multiple selectors in a rule declaration, give each selector its own line.
72-
* Put a space before the opening brace `{` in rule declarations
73-
* In properties, put a space after, but not before, the `:` character.
74-
* Put closing braces `}` of rule declarations on a new line
75-
* Put blank lines between rule declarations
76+
* 使用 2 个空格作为缩进。
77+
* 类名建议使用破折号代替驼峰法。如果你使用 BEM,也可以使用下划线(参见下面的的 [OOCSS BEM](#oocss-and-bem))。
78+
* 不要使用 ID 选择器。
79+
* 在一个规则声明中应用了多个选择器时,每个选择器独占一行。
80+
* 在规则声明的左大括号 `{` 前加上一个空格。
81+
* 在属性的冒号 `:` 后面加上一个空格,前面不加空格。
82+
* 规则声明的右大括号 `}` 独占一行。
83+
* 规则声明之间用空行分隔开。
7684

7785
**Bad**
7886

@@ -103,34 +111,38 @@ Finally, properties are what give the selected elements of a rule declaration th
103111
}
104112
```
105113

106-
### Comments
114+
<a name="comments"></a>
115+
### 注释
116+
117+
* 建议使用行注释 (在 Sass 中是 `//`) 代替块注释。
118+
* 建议在当行给代码写注释。避免行末注释。
119+
* 为代码作详细的注释,而不是写自述文档,比如:
120+
- z-index 的用处
121+
- 兼容性处理或者针对特定浏览器的 hack
107122

108-
* Prefer line comments (`//` in Sass-land) to block comments.
109-
* Prefer comments on their own line. Avoid end-of-line comments.
110-
* Write detailed comments for code that isn't self-documenting:
111-
- Uses of z-index
112-
- Compatibility or browser-specific hacks
123+
<a name="oocss-and-bem"></a>
124+
### OOCSS 和 BEM
113125

114-
### OOCSS and BEM
126+
出于以下原因,我们鼓励使用 OOCSS BEM 的组合:
115127

116-
We encourage some combination of OOCSS and BEM for these reasons:
128+
* 可以帮助我们理清 CSS 和 HTML 之间清晰且严谨的关系。
129+
* 可以帮助我们创建出可重用、易装配的组件。
130+
* 可以减少嵌套,降低特定性。
131+
* 可以帮助我们创建出可扩展的样式表。
117132

118-
* It helps create clear, strict relationships between CSS and HTML
119-
* It helps us create reusable, composable components
120-
* It allows for less nesting and lower specificity
121-
* It helps in building scalable stylesheets
133+
**OOCSS**,也就是 “Object Oriented CSS(面向对象的CSS)”,是一种写 CSS 的方法,其思想就是鼓励你把样式表看作“对象”的集合:可重用性、可重复性的代码段让你可以在整个网站中多次使用。
122134

123-
**OOCSS**, or “Object Oriented CSS”, is an approach for writing CSS that encourages you to think about your stylesheets as a collection of “objects”: reusuable, repeatable snippets that can be used independently throughout a website.
135+
参考资料:
124136

125-
* Nicole Sullivan's [OOCSS wiki](https://github.com/stubbornella/oocss/wiki)
126-
* Smashing Magazine's [Introduction to OOCSS](http://www.smashingmagazine.com/2011/12/12/an-introduction-to-object-oriented-css-oocss/)
137+
* Nicole Sullivan [OOCSS wiki](https://github.com/stubbornella/oocss/wiki)
138+
* Smashing Magazine [Introduction to OOCSS](http://www.smashingmagazine.com/2011/12/12/an-introduction-to-object-oriented-css-oocss/)
127139

128-
**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.
140+
**BEM**,也就是 “Block-Element-Modifier”,是一种用于 HTML CSS 类名的_命名约定_。BEM 最初是由 Yandex 提出的,要知道他们拥有巨大的代码库和可伸缩性,并且可以作为一套贯彻 OOCSS 的参考指导规范。
129141

130-
* CSS Trick's [BEM 101](https://css-tricks.com/bem-101/)
131-
* Harry Roberts' [introduction to BEM](http://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/)
142+
* CSS Trick [BEM 101](https://css-tricks.com/bem-101/)
143+
* Harry Roberts [introduction to BEM](http://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/)
132144

133-
**Example**
145+
**示例**
134146

135147
```html
136148
<article class="listing-card listing-card--featured">
@@ -151,29 +163,32 @@ We encourage some combination of OOCSS and BEM for these reasons:
151163
.listing-card__content { }
152164
```
153165

154-
* `.listing-card` is the “block” and represents the higher-level component
155-
* `.listing-card__title` is an “element” and represents a descendant of `.listing-card` that helps compose the block as a whole.
156-
* `.listing-card--featured` is a “modifier” and represents a different state or variation on the `.listing-card` block.
166+
* `.listing-card` 是一个块(block),表示高层次的组件。
167+
* `.listing-card__title` 是一个元素(element),它属于 `.listing-card` 的一部分,因此块是由元素组成的。
168+
* `.listing-card--featured` 是一个修饰符(modifier),表示这个块与 `.listing-card` 有着不同的状态或者变化。
157169

158-
### ID selectors
170+
<a name="id-selectors"></a>
171+
### ID 选择器
159172

160-
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.
173+
在 CSS 中,虽然可以通过 ID 选择元素,但通常都会把这种方式列为反面教材。ID 选择器给你的规则声明带来了不必要的高[优先级](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity),而且 ID 选择器是不可重用的。
161174

162-
For more on this subject, read [CSS Wizardry's article](http://csswizardry.com/2014/07/hacks-for-dealing-with-specificity/) on dealing with specificity.
175+
想要了解关于这个主题的更多内容,参见 [CSS Wizardry 的文章](http://csswizardry.com/2014/07/hacks-for-dealing-with-specificity/),文章中有关于如何处理优先级的内容。
163176

164-
### JavaScript hooks
177+
<a name="javascript-hooks"></a>
178+
### JavaScript 钩子
165179

166-
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.
180+
避免在 CSS JavaScript 中绑定相同的类。否则通常会导致以下两种后果:起码在重构的时候,开发者不得不把时间花费在两边对照每个要改变的类;更糟糕的是,开发者甚至会因为害怕破坏功能而不敢作出更改。
167181

168-
We recommend creating JavaScript-specific classes to bind to, prefixed with `.js-`:
182+
我们推荐在创建用于特定 JavaScript 的类名时,添加 `.js-` 前缀:
169183

170184
```html
171185
<button class="btn btn-primary js-request-to-book">Request to Book</button>
172186
```
173187

174-
### Border
188+
<a name="border"></a>
189+
### 边框
175190

176-
Use `0` instead of `none` to specify that a style has no border.
191+
在定义无边框样式时,使用 `0` 代替 `none`
177192

178193
**Bad**
179194

@@ -191,18 +206,21 @@ Use `0` instead of `none` to specify that a style has no border.
191206
}
192207
```
193208

209+
<a name="sass"></a>
194210
## Sass
195211

196-
### Syntax
212+
<a name="syntax"></a>
213+
### 语法
197214

198-
* Use the `.scss` syntax, never the original `.sass` syntax
199-
* Order your regular CSS and `@include` declarations logically (see below)
215+
* 使用 `.scss` 的语法,绝不使用 `.sass` 语法。
216+
* 给你的 CSS `@include` 声明按照以下逻辑排序(参见下文)
200217

201-
### Ordering of property declarations
218+
<a name="ordering-of-property-declarations"></a>
219+
### 属性声明的排序
202220

203-
1. Property declarations
221+
1. 属性声明
204222

205-
List all standard property declarations, anything that isn't an `@include` or a nested selector.
223+
首先列出除去 `@include` 和嵌套选择器之外的所有属性声明。
206224

207225
```scss
208226
.btn-green {
@@ -212,9 +230,9 @@ Use `0` instead of `none` to specify that a style has no border.
212230
}
213231
```
214232

215-
2. `@include` declarations
233+
2. `@include` 声明
216234

217-
Grouping `@include`s at the end makes it easier to read the entire selector.
235+
紧随后面的是 `@include`,这样可以使得整个选择器的可读性更高。
218236

219237
```scss
220238
.btn-green {
@@ -225,9 +243,9 @@ Use `0` instead of `none` to specify that a style has no border.
225243
}
226244
```
227245

228-
3. Nested selectors
246+
3. 嵌套选择器
229247

230-
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.
248+
_如果有必要_用到嵌套选择器,把它们放到最后,在规则声明和嵌套选择器之间要加上空白,相邻嵌套选择器之间也要加上空白。嵌套选择器中的内容也要遵循上述指引。
231249

232250
```scss
233251
.btn {
@@ -241,21 +259,25 @@ Use `0` instead of `none` to specify that a style has no border.
241259
}
242260
```
243261

244-
### Variables
262+
<a name="variables"></a>
263+
### 变量
245264

246-
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`).
265+
变量名应使用破折号 (例如 `$my-variable`) 代替 camelCased snake_cased 风格。对于仅用在当前文件的变量,可以在变量名之前添加下划线前缀 (例如 `$_my-variable`)
247266

267+
<a name="mixins"></a>
248268
### Mixins
249269

250-
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.
270+
为了让代码遵循 DRY 原则(Don't Repeat Yourself)、增强清晰性、或者抽象化复杂性,应该使用 mixin,这与那些命名良好的函数的作用是异曲同工的。虽然 mixin 可以不接收参数,但要注意,假如你不压缩负载(比如通过 gzip),这样会导致最终的样式包含不必要的代码重复。
251271

252-
### Extend directive
272+
<a name="extend-directive"></a>
273+
### 扩展指令
253274

254-
`@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.
275+
应避免使用 `@extend` 指令,因为它并不直观,而且具有潜在风险,特别是用在嵌套选择器的时候。即使是在扩展顶层占位符选择器,如果选择器的顺序最终会改变,也可能会导致问题。(比如,如果它们存在于其他文件,而加载顺序发生了变化)。其实 gzip 已经帮助你取得了使用 `@extend` 所获得的大部分优化效果,因此你只需要通过 mixin 让样式表更加符合 DRY 就足够了。
255276

256-
### Nested selectors
277+
<a name="nested-selectors"></a>
278+
### 嵌套选择器
257279

258-
**Do not nest selectors more than three levels deep!**
280+
**请不要让嵌套选择器的深度超过 3 层!**
259281

260282
```scss
261283
.page-container {
@@ -267,13 +289,13 @@ Mixins should be used to DRY up your code, add clarity, or abstract complexity--
267289
}
268290
```
269291

270-
When selectors become this long, you're likely writing CSS that is:
292+
当遇到以上情况的时候,你也许是这样写 CSS 的:
271293

272-
* Strongly coupled to the HTML (fragile) *—OR—*
273-
* Overly specific (powerful) *—OR—*
274-
* Not reusable
294+
* HTML 强耦合的(也是脆弱的) *或者*
295+
* 过于具体 (强大) *或者*
296+
* 没有重用
275297

276298

277-
Again: **never nest ID selectors!**
299+
再说一遍: **不要嵌套 ID 选择器!**
278300

279-
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.
301+
首先,如果你坚持要使用 ID 选择器(其实你真的不应该尝试这么做),那就不应该嵌套它们。如果你发现你正要这么做,你需要重新检查你的标签,或者指明为什么需要这样具体的特征。如果你正打算写出格式良好的 HTML CSS,你是****应该这样做的。

0 commit comments

Comments
 (0)