style_guide_scss.md 4.64 KB
Newer Older
1 2 3 4 5 6 7 8 9
# SCSS styleguide

This style guide recommends best practices for SCSS to make styles easy to read,
easy to maintain, and performant for the end-user.

## Rules

### Naming

Simon Knox's avatar
Simon Knox committed
10 11
Filenames should use `snake_case`.

12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
CSS classes should use the `lowercase-hyphenated` format rather than
`snake_case` or `camelCase`.

```scss
// Bad
.class_name {
  color: #fff;
}

// Bad
.className {
  color: #fff;
}

// Good
.class-name {
  color: #fff;
}
```

### Formatting

You should always use a space before a brace, braces should be on the same
line, each property should each get its own line, and there should be a space
between the property and its value.

```scss
// Bad
Filipa Lacerda's avatar
Filipa Lacerda committed
40
.container-item {
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  width: 100px; height: 100px;
  margin-top: 0;
}

// Bad
.container-item
{
  width: 100px;
  height: 100px;
  margin-top: 0;
}

// Bad
.container-item{
  width:100px;
  height:100px;
  margin-top:0;
}

// Good
.container-item {
  width: 100px;
  height: 100px;
  margin-top: 0;
}
```

Filipa Lacerda's avatar
Filipa Lacerda committed
68
Note that there is an exception for single-line rulesets, although these are
69 70 71 72 73 74 75 76
not typically recommended.

```scss
p { margin: 0; padding: 0; }
```

### Colors

Filipa Lacerda's avatar
Filipa Lacerda committed
77
HEX (hexadecimal) colors should use shorthand where possible, and should use
connorshea's avatar
connorshea committed
78 79
lower case letters to differentiate between letters and numbers, e.g. `#E3E3E3`
vs. `#e3e3e3`.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

```scss
// Bad
p {
  color: #ffffff;
}

// Bad
p {
  color: #FFFFFF;
}

// Good
p {
  color: #fff;
}
```

### Indentation

Indentation should always use two spaces for each indentation level.

```scss
// Bad, four spaces
p {
    color: #f00;
}

// Good
p {
  color: #f00;
}
```

### Semicolons

Filipa Lacerda's avatar
Filipa Lacerda committed
116
Always include semicolons after every property. When the stylesheets are
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
minified, the semicolons will be removed automatically.

```scss
// Bad
.container-item {
  width: 100px;
  height: 100px
}

// Good
.container-item {
  width: 100px;
  height: 100px;
}
```

### Shorthand

The shorthand form should be used for properties that support it.

```scss
// Bad
margin: 10px 15px 10px 15px;
padding: 10px 10px 10px 10px;

// Good
margin: 10px 15px;
padding: 10px;
```

### Zero Units

Filipa Lacerda's avatar
Filipa Lacerda committed
149
Omit length units on zero values, they're unnecessary and not including them
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
is slightly more performant.

```scss
// Bad
.item-with-padding {
  padding: 0px;
}

// Good
.item-with-padding {
  padding: 0;
}
```

### Selectors with a `js-` Prefix
connorshea's avatar
connorshea committed
165

Filipa Lacerda's avatar
Filipa Lacerda committed
166 167
Do not use any selector prefixed with `js-` for styling purposes. These
selectors are intended for use only with JavaScript to allow for removal or
168 169
renaming without breaking styling.

Filipa Lacerda's avatar
Filipa Lacerda committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
### IDs
Don't use ID selectors in CSS.

```scss
// Bad
#my-element {
  padding: 0;
}

// Good
.my-element {
  padding: 0;
}
```

### Variables
Before adding a new variable for a color or a size, guarantee:
1. There isn't already one
2. There isn't a similar one we can use instead.

190 191
## Linting

Filipa Lacerda's avatar
Filipa Lacerda committed
192 193
We use [SCSS Lint][scss-lint] to check for style guide conformity. It uses the
ruleset in `.scss-lint.yml`, which is located in the home directory of the
194 195
project.

Filipa Lacerda's avatar
Filipa Lacerda committed
196 197
To check if any warnings will be produced by your changes, you can run `rake
scss_lint` in the GitLab directory. SCSS Lint will also run in GitLab CI to
198 199
catch any warnings.

Filipa Lacerda's avatar
Filipa Lacerda committed
200
If the Rake task is throwing warnings you don't understand, SCSS Lint's
201 202 203 204
documentation includes [a full list of their linters][scss-lint-documentation].

### Fixing issues

Filipa Lacerda's avatar
Filipa Lacerda committed
205
If you want to automate changing a large portion of the codebase to conform to
206
the SCSS style guide, you can use [CSSComb][csscomb]. First install
Filipa Lacerda's avatar
Filipa Lacerda committed
207 208
[Node][node] and [NPM][npm], then run `npm install csscomb -g` to install
CSSComb globally (system-wide). Run it in the GitLab directory with
209 210 211 212
`csscomb app/assets/stylesheets` to automatically fix issues with CSS/SCSS.

Note that this won't fix every problem, but it should fix a majority.

connorshea's avatar
connorshea committed
213 214
### Ignoring issues

Filipa Lacerda's avatar
Filipa Lacerda committed
215
If you want a line or set of lines to be ignored by the linter, you can use
connorshea's avatar
connorshea committed
216 217 218
`// scss-lint:disable RuleName` ([more info][disabling-linters]):

```scss
219 220 221 222
// This lint rule is disabled because it is supported only in Chrome/Safari
// scss-lint:disable PropertySpelling
body {
  text-decoration-skip: ink;
connorshea's avatar
connorshea committed
223
}
224
// scss-lint:enable PropertySpelling
connorshea's avatar
connorshea committed
225 226 227
```

Make sure a comment is added on the line above the `disable` rule, otherwise the
Filipa Lacerda's avatar
Filipa Lacerda committed
228 229
linter will throw a warning. `DisableLinterReason` is enabled to make sure the
style guide isn't being ignored, and to communicate to others why the style
connorshea's avatar
connorshea committed
230 231
guide is ignored in this instance.

232 233 234 235 236
[csscomb]: https://github.com/csscomb/csscomb.js
[node]: https://github.com/nodejs/node
[npm]: https://www.npmjs.com/
[scss-lint]: https://github.com/brigade/scss-lint
[scss-lint-documentation]: https://github.com/brigade/scss-lint/blob/master/lib/scss_lint/linter/README.md
connorshea's avatar
connorshea committed
237
[disabling-linters]: https://github.com/brigade/scss-lint#disabling-linters-via-source