Skip to content

Commit d6b8ada

Browse files
committed
Merge branch master from airbnb/css (#1)
* 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. * Add section about mixins In my previous commit, I revised this document to advise against using @extend, which included removing the section about mixins because it only existed to recommend against using argumentless mixins in favor of @extend. However, this document feels a bit incomplete without some guidance on mixin usage, so I am adding a few sentences here. * Prefer `border: 0` over `border: none` Although the scss-lint configuration file here enforced `border: none`, we chatted about it and agreed that we prefer `border: 0`. * Default scss-lint severity to error scss-lint 0.44.0 added a global severity configuration setting. Since warnings are often ignored, we want to set this to error by default. * Add section about using dash-cased for variable names We want people to use a consistent style for variable names. We believe that simple rules are most likely to be followed, and that consistency is good, so we are settling on dash-cased, which is what we use for everything else, such as classes, mixins, functions, and properties. Along with this change, I am explicitly enabling the NameFormat SCSS-Lint rule which enforces this style. Note that this is enabled in SCSS-Lint by default, so this is functionally no different, but it is good to be explicit about things. * Enable PrivateNamingConvention scss-lint rule Since we mention that prefixing names with underscores is an acceptable way to denote that they are private, we want to enforce that they are defined within the same file that they are used. The new PrivateNamingConvention rule in scss-lint is designed for this purpose. * Update README.md Add Chinese(Simplified) Translation.
1 parent bf1e947 commit d6b8ada

File tree

2 files changed

+55
-62
lines changed

2 files changed

+55
-62
lines changed

.scss-lint.yml

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,30 @@
1+
severity: error
2+
13
linters:
24

35
BorderZero:
46
enabled: true
5-
convention: none
7+
convention: zero
68

79
BemDepth:
810
enabled: true
911

1012
DeclarationOrder:
1113
enabled: false
1214

15+
ExtendDirective:
16+
enabled: true
17+
1318
LeadingZero:
1419
enabled: false
1520

21+
NameFormat:
22+
enabled: true
23+
24+
PrivateNamingConvention:
25+
enabled: true
26+
prefix: _
27+
1628
PropertySortOrder:
1729
enabled: false
1830

README.md

Lines changed: 42 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
- [OOCSS and BEM](#oocss-and-bem)
1515
- [ID Selectors](#id-selectors)
1616
- [JavaScript hooks](#javascript-hooks)
17+
- [Border](#border)
1718
1. [Sass](#sass)
1819
- [Syntax](#syntax)
1920
- [Ordering](#ordering-of-property-declarations)
21+
- [Variables](#variables)
2022
- [Mixins](#mixins)
21-
- [Placeholders](#placeholders)
23+
- [Extend directive](#extend-directive)
2224
- [Nested selectors](#nested-selectors)
25+
1. [Translation](#translation)
2326

2427
## Terminology
2528

@@ -169,60 +172,66 @@ We recommend creating JavaScript-specific classes to bind to, prefixed with `.js
169172
<button class="btn btn-primary js-request-to-book">Request to Book</button>
170173
```
171174

175+
### Border
176+
177+
Use `0` instead of `none` to specify that a style has no border.
178+
179+
**Bad**
180+
181+
```css
182+
.foo {
183+
border: none;
184+
}
185+
```
186+
187+
**Good**
188+
189+
```css
190+
.foo {
191+
border: 0;
192+
}
193+
```
194+
172195
## Sass
173196

174197
### Syntax
175198

176199
* Use the `.scss` syntax, never the original `.sass` syntax
177-
* Order your `@extend`, regular CSS and `@include` declarations logically (see below)
200+
* Order your regular CSS and `@include` declarations logically (see below)
178201

179202
### Ordering of property declarations
180203

181-
1. `@extend` declarations
182-
183-
Just as in other OOP languages, it's helpful to know right away that this “class” inherits from another.
184-
185-
```scss
186-
.btn-green {
187-
@extend %btn;
188-
// ...
189-
}
190-
```
191-
192-
2. Property declarations
204+
1. Property declarations
193205

194-
Now list all standard property declarations, anything that isn't an `@extend`, `@include`, or a nested selector.
206+
List all standard property declarations, anything that isn't an `@include` or a nested selector.
195207

196208
```scss
197209
.btn-green {
198-
@extend %btn;
199210
background: green;
200211
font-weight: bold;
201212
// ...
202213
}
203214
```
204215

205-
3. `@include` declarations
216+
2. `@include` declarations
206217

207-
Grouping `@include`s at the end makes it easier to read the entire selector, and it also visually separates them from `@extend`s.
218+
Grouping `@include`s at the end makes it easier to read the entire selector.
208219

209220
```scss
210221
.btn-green {
211-
@extend %btn;
212222
background: green;
213223
font-weight: bold;
214224
@include transition(background 0.5s ease);
215225
// ...
216226
}
217227
```
218228

219-
4. Nested selectors
229+
3. Nested selectors
220230

221231
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.
222232

223233
```scss
224234
.btn {
225-
@extend %btn;
226235
background: green;
227236
font-weight: bold;
228237
@include transition(background 0.5s ease);
@@ -233,51 +242,17 @@ We recommend creating JavaScript-specific classes to bind to, prefixed with `.js
233242
}
234243
```
235244

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. `@mixin hide { 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+
### Variables
245246

246-
**Sass**
247+
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`).
247248

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**
249+
### Mixins
266250

267-
```css
268-
.icon-error,
269-
.icon-success {
270-
font-family: "Airglyphs";
271-
}
251+
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.
272252

273-
.icon-error {
274-
color: red;
275-
}
253+
### Extend directive
276254

277-
.icon-success {
278-
color: green;
279-
}
280-
```
255+
`@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.
281256

282257
### Nested selectors
283258

@@ -303,3 +278,9 @@ When selectors become this long, you're likely writing CSS that is:
303278
Again: **never nest ID selectors!**
304279
305280
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.
281+
282+
## Translation
283+
284+
This style guide is also available in other languages:
285+
286+
- ![cn](https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/China.png) **Chinese (Simplified)**: [Zhangjd/css-style-guide](https://github.com/Zhangjd/css-style-guide)

0 commit comments

Comments
 (0)