Skip to content

Commit 20c09ce

Browse files
committed
add translation
1 parent 3366719 commit 20c09ce

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
*A mostly reasonable approach to CSS and Sass*
44

5+
*CSS and Sass 最佳实践*
6+
57
## Table of Contents
68

79
1. [Terminology](#terminology)
@@ -31,6 +33,8 @@
3133

3234
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:
3335

36+
"规则声明"是指伴随一组属性的一个(或一组)选择器。举个例子:
37+
3438
```css
3539
.listing {
3640
font-size: 18px;
@@ -42,6 +46,8 @@ A “rule declaration” is the name given to a selector (or a group of selector
4246

4347
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:
4448

49+
在规则声明中,“选择器” 决定 DOM 树中的元素如何被定义的属性所渲染。选择器可以匹配 HTML 元素,也可以匹配其 class、ID、属性。下面是一些选择器的例子
50+
4551
```css
4652
.my-element-class {
4753
/* ... */
@@ -56,6 +62,8 @@ In a rule declaration, “selectors” are the bits that determine which element
5662

5763
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:
5864

65+
最后,属性是给被选择的元素声明样式。属性是 key-value 成对存在的,一个规则声明中可以包含一个或多个属性声明。属性声明就像这样:
66+
5967
```css
6068
/* some selector */ {
6169
background: #f1f1f1;
@@ -79,6 +87,17 @@ Finally, properties are what give the selected elements of a rule declaration th
7987
* Put closing braces `}` of rule declarations on a new line.
8088
* Put blank lines between rule declarations.
8189

90+
91+
* 缩进使用 soft tabs ( 两个空格 )。
92+
* 类名用 破折号而不是驼峰
93+
- 如果使用 BEM (参见下面的 [OOCSS and BEM](#oocss-and-bem) ),下划线和大驼峰也是可以的
94+
* 不要使用 ID 选择器
95+
* 一个规则声明中,使用多个选择器时,每个选择器各占一行
96+
* 左大括号 `{` 前面放一个空格
97+
* 属性的冒号 `:` 后面加一个空格,前面不加
98+
* 右大括号 `}` 放在新的一行
99+
* 规则声明之间放一些空行
100+
82101
**Bad**
83102

84103
```css
@@ -116,27 +135,48 @@ Finally, properties are what give the selected elements of a rule declaration th
116135
- Uses of z-index
117136
- Compatibility or browser-specific hacks
118137

138+
139+
* 用行注释 ( Sass 中是 `//` ) 代替块注释
140+
* 独自一行注释。避免行末注释。
141+
* 给不是自解释的代码添加详细的注释
142+
- z-index 的使用
143+
- 兼容或者特定浏览器的 hacks
144+
119145
### OOCSS and BEM
120146

121147
We encourage some combination of OOCSS and BEM for these reasons:
122148

149+
鼓励使用 OOCSS 和 BEM 的组合,因为:
150+
123151
* It helps create clear, strict relationships between CSS and HTML
124152
* It helps us create reusable, composable components
125153
* It allows for less nesting and lower specificity
126154
* It helps in building scalable stylesheets
127155

156+
157+
* 可以创建 CSS 和 HTML 之前清晰严谨的关系
158+
* 可以创建可复用的、易重组的组件
159+
* 减少嵌套和降低耦合
160+
* 构建可扩展的样式表
161+
128162
**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.
129163

164+
**OOCSS**, or “Object Oriented CSS”, (面向对象的 CSS)是写 CSS 的一种方式,其鼓励你将你的样式表当成对象的集合: 在整个网站中可以被独立使用的可复用,可重用的代码段
165+
130166
* Nicole Sullivan's [OOCSS wiki](https://github.com/stubbornella/oocss/wiki)
131167
* Smashing Magazine's [Introduction to OOCSS](http://www.smashingmagazine.com/2011/12/12/an-introduction-to-object-oriented-css-oocss/)
132168

133169
**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.
134170

171+
**BEM**, or “Block-Element-Modifier”, 是一种 HTML 和 CSS 类名的_命名约定_. 它最初由 Yandex 提出,拥有巨大的代码库和可扩展性, 被用来作为一系列遵循 OOCSS 的参考.
172+
135173
* CSS Trick's [BEM 101](https://css-tricks.com/bem-101/)
136174
* Harry Roberts' [introduction to BEM](http://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/)
137175

138176
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.
139177

178+
建议搭配 PascalCased “blocks” 的 BEM 变体 , 与组件(如 React)结合时特别好用。 Underscores 和 dashes 依然被用作修改符和子节点。
179+
140180
**Example**
141181

142182
```jsx
@@ -168,18 +208,30 @@ function ListingCard() {
168208
* `.ListingCard__title` is an “element” and represents a descendant of `.ListingCard` that helps compose the block as a whole.
169209
* `.ListingCard--featured` is a “modifier” and represents a different state or variation on the `.ListingCard` block.
170210

211+
* `.ListingCard` 是“块”,表示更高级的组件
212+
* `.ListingCard__title` 是一个“元素”,表示 `.ListingCard` 的后代, 用来组成“块”。
213+
* `.ListingCard--featured` 一个“修饰符”, 表示`.ListingCard`块的一个不同的状态或变化。
214+
171215
### ID selectors
172216

173217
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.
174218

219+
CSS 中按 ID 选择元素, 通常被认为是反模式的. ID 选择器为规则定义带来了不必须的高优先级的[特性](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity) , 而且是不可复用的.
220+
175221
For more on this subject, read [CSS Wizardry's article](http://csswizardry.com/2014/07/hacks-for-dealing-with-specificity/) on dealing with specificity.
176222

223+
想了解更多, 阅读 [CSS Wizardry's article](http://csswizardry.com/2014/07/hacks-for-dealing-with-specificity/)
224+
177225
### JavaScript hooks
178226

179227
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.
180228

229+
避免同时使用 CSS 和 JavaScript 绑定相同的类。合并两者在重构时容易导致问题, 轻者浪费时间对照寻找要修改的类名, 更糟糕的是,开发因害怕破坏功能而不敢修改代码。
230+
181231
We recommend creating JavaScript-specific classes to bind to, prefixed with `.js-`:
182232

233+
建议在创建用于 JavaScript 的类名时,以 `.js-` 开头:
234+
183235
```html
184236
<button class="btn btn-primary js-request-to-book">Request to Book</button>
185237
```
@@ -188,6 +240,8 @@ We recommend creating JavaScript-specific classes to bind to, prefixed with `.js
188240

189241
Use `0` instead of `none` to specify that a style has no border.
190242

243+
指定没有边框时,用 `0`代替 `none`
244+
191245
**Bad**
192246

193247
```css
@@ -212,11 +266,15 @@ Use `0` instead of `none` to specify that a style has no border.
212266
* Use the `.scss` syntax, never the original `.sass` syntax
213267
* Order your regular CSS and `@include` declarations logically (see below)
214268

269+
*`.scss` 语法,不要使用原始的 `.sass` 语法
270+
* CSS 和 `@include`逻辑顺序 (见下文)
271+
215272
### Ordering of property declarations
216273

217274
1. Property declarations
218275

219276
List all standard property declarations, anything that isn't an `@include` or a nested selector.
277+
列出所有不是 `@include` 或嵌套选择器的属性声明。
220278

221279
```scss
222280
.btn-green {
@@ -229,6 +287,7 @@ Use `0` instead of `none` to specify that a style has no border.
229287
2. `@include` declarations
230288

231289
Grouping `@include`s at the end makes it easier to read the entire selector.
290+
分组 `@include` 在行末,使整个选择器更容易阅读
232291

233292
```scss
234293
.btn-green {
@@ -242,6 +301,8 @@ Use `0` instead of `none` to specify that a style has no border.
242301
3. Nested selectors
243302

244303
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.
304+
305+
_如果一定要用_, 嵌套选择器放在最后,在规则声明和嵌套选择器之前添加空白。嵌套选择器也遵循此指引。
245306

246307
```scss
247308
.btn {
@@ -259,18 +320,25 @@ Use `0` instead of `none` to specify that a style has no border.
259320

260321
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`).
261322

323+
推荐破折号命名(如`$my-variable`)而不是驼峰或者蛇形。可以在变量名前添加下划线前缀(如`$_my-variable`),用以表示此变量只用于此文件。
324+
262325
### Mixins
263326

264327
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.
265328

329+
Mixins 可以用来 DRY 你的代码、增加清晰度或者抽象复杂度,和命名恰当的方法有异曲同工之妙。Mixins 不接收参数,但请注意,如果你不压缩 plyload(如 gzip), 可能会导致在结果样式中出现不必要的重复代码。
330+
266331
### Extend directive
267332

268333
`@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.
269334

335+
应该避免使用 `@extend`,因为它不直观并且可能有危险行为,特别是用在嵌套选择器上。即便是在顶层占位符选择器使用扩展,如果选择器的顺序最终会改变,也可能会导致问题。(比如,如果它们存在于其他文件,而加载顺序发生了变化)。其实,使用 @extend 所获得的大部分优化效果,gzip 压缩已经帮你做到了,因此你只需要通过 mixin 让样式表更符合 DRY 原则就足够了。
270336
### Nested selectors
271337

272338
**Do not nest selectors more than three levels deep!**
273339

340+
**不要让嵌套选择器超过三层!**
341+
274342
```scss
275343
.page-container {
276344
.content {
@@ -282,16 +350,24 @@ Mixins should be used to DRY up your code, add clarity, or abstract complexity--
282350
```
283351

284352
When selectors become this long, you're likely writing CSS that is:
353+
当选择器变得太长,可能是因为:
285354
286355
* Strongly coupled to the HTML (fragile) *—OR—*
287356
* Overly specific (powerful) *—OR—*
288357
* Not reusable
289358
290359
360+
* 与 HTML 强耦合(易碎的)*—或者—*
361+
* 过于具体(强大)*—或者—*
362+
* 没有复用性
363+
291364
Again: **never nest ID selectors!**
365+
再次: **不要嵌套 ID 选择器!**
292366
293367
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.
294368
369+
如果你必须使用 ID 选择器(尝试不要),它们不应该嵌套。如果你正这样做,你需要检查你的HTML 或者指明原因。如果你要写出良好的 HTML 和 CSS,你应该 **从不** 这样做。
370+
295371
**[⬆ back to top](#table-of-contents)**
296372
297373
## Translation

0 commit comments

Comments
 (0)