You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+76Lines changed: 76 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
3
3
*A mostly reasonable approach to CSS and Sass*
4
4
5
+
*CSS and Sass 最佳实践*
6
+
5
7
## Table of Contents
6
8
7
9
1.[Terminology](#terminology)
@@ -31,6 +33,8 @@
31
33
32
34
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:
33
35
36
+
"规则声明"是指伴随一组属性的一个(或一组)选择器。举个例子:
37
+
34
38
```css
35
39
.listing {
36
40
font-size: 18px;
@@ -42,6 +46,8 @@ A “rule declaration” is the name given to a selector (or a group of selector
42
46
43
47
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:
44
48
49
+
在规则声明中,“选择器” 决定 DOM 树中的元素如何被定义的属性所渲染。选择器可以匹配 HTML 元素,也可以匹配其 class、ID、属性。下面是一些选择器的例子
50
+
45
51
```css
46
52
.my-element-class {
47
53
/* ... */
@@ -56,6 +62,8 @@ In a rule declaration, “selectors” are the bits that determine which element
56
62
57
63
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:
@@ -79,6 +87,17 @@ Finally, properties are what give the selected elements of a rule declaration th
79
87
* Put closing braces `}` of rule declarations on a new line.
80
88
* Put blank lines between rule declarations.
81
89
90
+
91
+
* 缩进使用 soft tabs ( 两个空格 )。
92
+
* 类名用 破折号而不是驼峰
93
+
- 如果使用 BEM (参见下面的 [OOCSS and BEM](#oocss-and-bem) ),下划线和大驼峰也是可以的
94
+
* 不要使用 ID 选择器
95
+
* 一个规则声明中,使用多个选择器时,每个选择器各占一行
96
+
* 左大括号 `{` 前面放一个空格
97
+
* 属性的冒号 `:` 后面加一个空格,前面不加
98
+
* 右大括号 `}` 放在新的一行
99
+
* 规则声明之间放一些空行
100
+
82
101
**Bad**
83
102
84
103
```css
@@ -116,27 +135,48 @@ Finally, properties are what give the selected elements of a rule declaration th
116
135
- Uses of z-index
117
136
- Compatibility or browser-specific hacks
118
137
138
+
139
+
* 用行注释 ( Sass 中是 `//` ) 代替块注释
140
+
* 独自一行注释。避免行末注释。
141
+
* 给不是自解释的代码添加详细的注释
142
+
- z-index 的使用
143
+
- 兼容或者特定浏览器的 hacks
144
+
119
145
### OOCSS and BEM
120
146
121
147
We encourage some combination of OOCSS and BEM for these reasons:
122
148
149
+
鼓励使用 OOCSS 和 BEM 的组合,因为:
150
+
123
151
* It helps create clear, strict relationships between CSS and HTML
124
152
* It helps us create reusable, composable components
125
153
* It allows for less nesting and lower specificity
126
154
* It helps in building scalable stylesheets
127
155
156
+
157
+
* 可以创建 CSS 和 HTML 之前清晰严谨的关系
158
+
* 可以创建可复用的、易重组的组件
159
+
* 减少嵌套和降低耦合
160
+
* 构建可扩展的样式表
161
+
128
162
**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.
129
163
164
+
**OOCSS**, or “Object Oriented CSS”, (面向对象的 CSS)是写 CSS 的一种方式,其鼓励你将你的样式表当成对象的集合: 在整个网站中可以被独立使用的可复用,可重用的代码段
* Smashing Magazine's [Introduction to OOCSS](http://www.smashingmagazine.com/2011/12/12/an-introduction-to-object-oriented-css-oocss/)
132
168
133
169
**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.
134
170
171
+
**BEM**, or “Block-Element-Modifier”, 是一种 HTML 和 CSS 类名的_命名约定_. 它最初由 Yandex 提出,拥有巨大的代码库和可扩展性, 被用来作为一系列遵循 OOCSS 的参考.
* Harry Roberts' [introduction to BEM](http://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/)
137
175
138
176
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.
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.
174
218
219
+
CSS 中按 ID 选择元素, 通常被认为是反模式的. ID 选择器为规则定义带来了不必须的高优先级的[特性](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity) , 而且是不可复用的.
220
+
175
221
For more on this subject, read [CSS Wizardry's article](http://csswizardry.com/2014/07/hacks-for-dealing-with-specificity/) on dealing with specificity.
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-`:
182
232
233
+
建议在创建用于 JavaScript 的类名时,以 `.js-` 开头:
234
+
183
235
```html
184
236
<buttonclass="btn btn-primary js-request-to-book">Request to Book</button>
185
237
```
@@ -188,6 +240,8 @@ We recommend creating JavaScript-specific classes to bind to, prefixed with `.js
188
240
189
241
Use `0` instead of `none` to specify that a style has no border.
190
242
243
+
指定没有边框时,用 `0`代替 `none`。
244
+
191
245
**Bad**
192
246
193
247
```css
@@ -212,11 +266,15 @@ Use `0` instead of `none` to specify that a style has no border.
212
266
* Use the `.scss` syntax, never the original `.sass` syntax
213
267
* Order your regular CSS and `@include` declarations logically (see below)
214
268
269
+
* 用 `.scss` 语法,不要使用原始的 `.sass` 语法
270
+
* CSS 和 `@include`逻辑顺序 (见下文)
271
+
215
272
### Ordering of property declarations
216
273
217
274
1. Property declarations
218
275
219
276
List all standard property declarations, anything that isn't an `@include` or a nested selector.
277
+
列出所有不是 `@include` 或嵌套选择器的属性声明。
220
278
221
279
```scss
222
280
.btn-green {
@@ -229,6 +287,7 @@ Use `0` instead of `none` to specify that a style has no border.
229
287
2. `@include` declarations
230
288
231
289
Grouping `@include`s at the end makes it easier to read the entire selector.
290
+
分组 `@include` 在行末,使整个选择器更容易阅读
232
291
233
292
```scss
234
293
.btn-green {
@@ -242,6 +301,8 @@ Use `0` instead of `none` to specify that a style has no border.
242
301
3. Nested selectors
243
302
244
303
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.
@@ -259,18 +320,25 @@ Use `0` instead of `none` to specify that a style has no border.
259
320
260
321
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 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` 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.
**Do not nest selectors more than three levels deep!**
273
339
340
+
**不要让嵌套选择器超过三层!**
341
+
274
342
```scss
275
343
.page-container {
276
344
.content {
@@ -282,16 +350,24 @@ Mixins should be used to DRY up your code, add clarity, or abstract complexity--
282
350
```
283
351
284
352
When selectors become this long, you're likely writing CSS that is:
353
+
当选择器变得太长,可能是因为:
285
354
286
355
* Strongly coupled to the HTML (fragile) *—OR—*
287
356
* Overly specific (powerful) *—OR—*
288
357
* Not reusable
289
358
290
359
360
+
* 与 HTML 强耦合(易碎的)*—或者—*
361
+
* 过于具体(强大)*—或者—*
362
+
* 没有复用性
363
+
291
364
Again: **never nest ID selectors!**
365
+
再次: **不要嵌套 ID 选择器!**
292
366
293
367
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.
294
368
369
+
如果你必须使用 ID 选择器(尝试不要),它们不应该嵌套。如果你正这样做,你需要检查你的HTML 或者指明原因。如果你要写出良好的 HTML 和 CSS,你应该 **从不** 这样做。
0 commit comments