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
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
+
我们把一个(或一组)选择器与其伴随的属性集合称之为“规则声明”。举个例子:
31
35
32
36
```css
33
37
.listing {
@@ -36,9 +40,10 @@ A “rule declaration” is the name given to a selector (or a group of selector
36
40
}
37
41
```
38
42
39
-
### Selectors
43
+
<aname="selectors"></a>
44
+
### 选择器
40
45
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, 或者元素拥有的属性。以下是选择器的例子:
42
47
43
48
```css
44
49
.my-element-class {
@@ -50,9 +55,10 @@ In a rule declaration, “selectors” are the bits that determine which element
50
55
}
51
56
```
52
57
53
-
### Properties
58
+
<aname="properties"></a>
59
+
### 属性
54
60
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:
**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.
* Smashing Magazine 的[Introduction to OOCSS](http://www.smashingmagazine.com/2011/12/12/an-introduction-to-object-oriented-css-oocss/)
127
139
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.
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 选择器是不可重用的。
161
174
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.
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
+
我们推荐在创建用于特定 JavaScript 的类名时,添加 `.js-` 前缀:
169
183
170
184
```html
171
185
<buttonclass="btn btn-primary js-request-to-book">Request to Book</button>
172
186
```
173
187
174
-
### Border
188
+
<aname="border"></a>
189
+
### 边框
175
190
176
-
Use`0`instead of `none` to specify that a style has no border.
191
+
在定义无边框样式时,使用`0`代替 `none`。
177
192
178
193
**Bad**
179
194
@@ -191,18 +206,21 @@ Use `0` instead of `none` to specify that a style has no border.
191
206
}
192
207
```
193
208
209
+
<aname="sass"></a>
194
210
## Sass
195
211
196
-
### Syntax
212
+
<aname="syntax"></a>
213
+
### 语法
197
214
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`声明按照以下逻辑排序(参见下文)
200
217
201
-
### Ordering of property declarations
218
+
<aname="ordering-of-property-declarations"></a>
219
+
### 属性声明的排序
202
220
203
-
1.Property declarations
221
+
1.属性声明
204
222
205
-
List all standard property declarations, anything that isn't an `@include`or a nested selector.
223
+
首先列出除去 `@include`和嵌套选择器之外的所有属性声明。
206
224
207
225
```scss
208
226
.btn-green {
@@ -212,9 +230,9 @@ Use `0` instead of `none` to specify that a style has no border.
212
230
}
213
231
```
214
232
215
-
2. `@include` declarations
233
+
2. `@include` 声明
216
234
217
-
Grouping `@include`s at the end makes it easier to read the entire selector.
235
+
紧随后面的是 `@include`,这样可以使得整个选择器的可读性更高。
218
236
219
237
```scss
220
238
.btn-green {
@@ -225,9 +243,9 @@ Use `0` instead of `none` to specify that a style has no border.
225
243
}
226
244
```
227
245
228
-
3. Nested selectors
246
+
3. 嵌套选择器
229
247
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.
@@ -241,21 +259,25 @@ Use `0` instead of `none` to specify that a style has no border.
241
259
}
242
260
```
243
261
244
-
### Variables
262
+
<a name="variables"></a>
263
+
### 变量
245
264
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`).
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!**
280
+
**请不要让嵌套选择器的深度超过 3 层!**
259
281
260
282
```scss
261
283
.page-container {
@@ -267,13 +289,13 @@ Mixins should be used to DRY up your code, add clarity, or abstract complexity--
267
289
}
268
290
```
269
291
270
-
When selectors become this long, you're likely writing CSS that is:
292
+
当遇到以上情况的时候,你也许是这样写 CSS 的:
271
293
272
-
* Strongly coupled to the HTML (fragile) *—OR—*
273
-
* Overly specific (powerful) *—OR—*
274
-
* Not reusable
294
+
*与 HTML强耦合的(也是脆弱的) *—或者—*
295
+
*过于具体 (强大) *—或者—*
296
+
*没有重用
275
297
276
298
277
-
Again: **never nest ID selectors!**
299
+
再说一遍: **不要嵌套 ID 选择器!**
278
300
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