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
+10-62Lines changed: 10 additions & 62 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@
18
18
-[Syntax](#syntax)
19
19
-[Ordering](#ordering-of-property-declarations)
20
20
-[Mixins](#mixins)
21
-
-[Placeholders](#placeholders)
21
+
-[Extend directive](#extend-directive)
22
22
-[Nested selectors](#nested-selectors)
23
23
24
24
## Terminology
@@ -174,55 +174,41 @@ We recommend creating JavaScript-specific classes to bind to, prefixed with `.js
174
174
### Syntax
175
175
176
176
* Use the `.scss` syntax, never the original `.sass` syntax
177
-
* Order your `@extend`, regular CSS and `@include` declarations logically (see below)
177
+
* Order your regular CSS and `@include` declarations logically (see below)
178
178
179
179
### Ordering of property declarations
180
180
181
-
1.`@extend` declarations
181
+
1.Property declarations
182
182
183
-
Just as in other OOP languages, it's helpful to know right away that this “class” inherits from another.
183
+
List all standard property declarations, anything that isn't an `@include` or a nested selector.
184
184
185
185
```scss
186
186
.btn-green {
187
-
@extend%btn;
188
-
// ...
189
-
}
190
-
```
191
-
192
-
2. Property declarations
193
-
194
-
Now list all standard property declarations, anything that isn't an `@extend`, `@include`, or a nested selector.
195
-
196
-
```scss
197
-
.btn-green {
198
-
@extend %btn;
199
187
background: green;
200
188
font-weight: bold;
201
189
// ...
202
190
}
203
191
```
204
192
205
-
3. `@include` declarations
193
+
2. `@include` declarations
206
194
207
-
Grouping `@include`s at the end makes it easier to read the entire selector, and it also visually separates them from `@extend`s.
195
+
Grouping `@include`s at the end makes it easier to read the entire selector.
208
196
209
197
```scss
210
198
.btn-green {
211
-
@extend %btn;
212
199
background: green;
213
200
font-weight: bold;
214
201
@includetransition(background0.5sease);
215
202
// ...
216
203
}
217
204
```
218
205
219
-
4. Nested selectors
206
+
3. Nested selectors
220
207
221
208
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.
222
209
223
210
```scss
224
211
.btn {
225
-
@extend%btn;
226
212
background: green;
227
213
font-weight: bold;
228
214
@includetransition(background0.5sease);
@@ -235,49 +221,11 @@ We recommend creating JavaScript-specific classes to bind to, prefixed with `.js
235
221
236
222
### Mixins
237
223
238
-
Mixins, defined via `@mixin` and called with `@include`, should be used sparingly and only when function arguments are necessary. A mixin without function arguments (i.e. `@mixinhide { display: none; }`) is better accomplished using a placeholder selector (see below) in order to prevent code duplication.
239
-
240
-
### Placeholders
241
-
242
-
Placeholders in Sass, defined via `%selector` and used with `@extend`, are a way of defining rule declarations that aren't automatically output in your compiled stylesheet. Instead, other selectors “inherit” from the placeholder, and the relevant selectors are copied to the point in the stylesheet where the placeholder is defined. This is best illustrated with the example below.
243
-
244
-
Placeholders are powerful but easy to abuse, especially when combined with nested selectors. **As a rule of thumb, avoid creating placeholders with nested rule declarations, or calling `@extend` inside nested selectors.** Placeholders are great for simple inheritance, but can easily result in the accidental creation of additional selectors without paying close attention to how and where they are used.
245
-
246
-
**Sass**
247
-
248
-
```sass
249
-
// Unless we call `@extend %icon` these properties won't be compiled!
250
-
%icon {
251
-
font-family: "Airglyphs";
252
-
}
224
+
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.
253
225
254
-
.icon-error {
255
-
@extend%icon;
256
-
color: red;
257
-
}
226
+
### Extend directive
258
227
259
-
.icon-success {
260
-
@extend%icon;
261
-
color: green;
262
-
}
263
-
```
264
-
265
-
**CSS**
266
-
267
-
```css
268
-
.icon-error,
269
-
.icon-success {
270
-
font-family: "Airglyphs";
271
-
}
272
-
273
-
.icon-error {
274
-
color: red;
275
-
}
276
-
277
-
.icon-success {
278
-
color: green;
279
-
}
280
-
```
228
+
`@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.
0 commit comments