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
Advise against @extend, enable SCSS-lint ExtendDirective
@extend is unintuitive and dangerous, so we are recommending against its
use. As part of this commit, I enabled the SCSS-lint linter that will
warn against its use. I also revised content that mentioned @extend, to
maintain philosophical consistency.
Copy file name to clipboardExpand all lines: README.md
+9-66Lines changed: 9 additions & 66 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,8 +17,7 @@
17
17
1.[Sass](#sass)
18
18
-[Syntax](#syntax)
19
19
-[Ordering](#ordering-of-property-declarations)
20
-
-[Mixins](#mixins)
21
-
-[Placeholders](#placeholders)
20
+
-[Extend directive](#extend-directive)
22
21
-[Nested selectors](#nested-selectors)
23
22
24
23
## Terminology
@@ -174,55 +173,41 @@ We recommend creating JavaScript-specific classes to bind to, prefixed with `.js
174
173
### Syntax
175
174
176
175
* Use the `.scss` syntax, never the original `.sass` syntax
177
-
* Order your `@extend`, regular CSS and `@include` declarations logically (see below)
176
+
* Order your regular CSS and `@include` declarations logically (see below)
178
177
179
178
### Ordering of property declarations
180
179
181
-
1.`@extend` declarations
180
+
1.Property declarations
182
181
183
-
Just as in other OOP languages, it's helpful to know right away that this “class” inherits from another.
182
+
List all standard property declarations, anything that isn't an `@include` or a nested selector.
184
183
185
184
```scss
186
185
.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
186
background: green;
200
187
font-weight: bold;
201
188
// ...
202
189
}
203
190
```
204
191
205
-
3. `@include` declarations
192
+
2. `@include` declarations
206
193
207
-
Grouping `@include`s at the end makes it easier to read the entire selector, and it also visually separates them from `@extend`s.
194
+
Grouping `@include`s at the end makes it easier to read the entire selector.
208
195
209
196
```scss
210
197
.btn-green {
211
-
@extend %btn;
212
198
background: green;
213
199
font-weight: bold;
214
200
@includetransition(background0.5sease);
215
201
// ...
216
202
}
217
203
```
218
204
219
-
4. Nested selectors
205
+
3. Nested selectors
220
206
221
207
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
208
223
209
```scss
224
210
.btn {
225
-
@extend%btn;
226
211
background: green;
227
212
font-weight: bold;
228
213
@includetransition(background0.5sease);
@@ -233,51 +218,9 @@ We recommend creating JavaScript-specific classes to bind to, prefixed with `.js
233
218
}
234
219
```
235
220
236
-
### Mixins
237
-
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
-
}
253
-
254
-
.icon-error {
255
-
@extend%icon;
256
-
color: red;
257
-
}
258
-
259
-
.icon-success {
260
-
@extend%icon;
261
-
color: green;
262
-
}
263
-
```
264
-
265
-
**CSS**
221
+
### Extend directive
266
222
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
-
```
223
+
`@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