Contents
- Hoist style definition when possible for performance and maintainability:
// good
.foo {
ul {
list-style: none;
text-align: center;
}
li {}
}
// bad
.foo {
ul {}
li {
list-style: none;
text-align: center;
}
}
- Avoid excessive nesting and overspecified selectors:
// good
.foo {
heading {
...
}
h1 {
...
}
.title {
...
}
}
// bad
.foo {
heading {
...
h1 {
...
> span.title {
...
}
}
}
}
- Define colors in terms of one another whenever possible:
// good
div {
$color-danger: #f00;
border-color: lighten($color-danger, 3%);
color: $color-danger;
}
// bad
div {
border-color: #ff1515;
color: #f00;
}
- Avoid variables named after the color, use semantic color names instead:
// good
$color-danger: #f00;
$color-subtle: #ccc;
// bad
$red: #f00;
$light-grey: #ccc;
- Use global variables for cursor definitions, so that designers can more easily modify cursor styles:
// good
$CURSOR_POINTER: pointer;
$CURSOR_RESIZE: resize;
.link {
cursor: $CURSOR_POINTER;
&.resize {
cursor: $CURSOR_RESIZE;
}
}
// bad
.link {
cursor: pointer;
&.resize {
cursor: resize;
}
}
- Font families should be defined once as global variables. This is to avoid a proliferation of similar font declarations, and to let designers to easily tweak font faces across the site:
// good
$FONT_SANS_SERIF: Helvetica, Arial, sans-serif;
div {
font-family: $FONT_SANS_SERIF;
}
.foo {
font-family: $FONT_SANS_SERIF;
}
// bad
div {
font-family: Helvetica, Arial, sans-serif;
}
.foo {
font-family: Helvetica;
}
- Variables should pre prefixed with their type, when it makes sense:
// good
$color-danger: red;
$width-container: 800px;
// bad
$danger: red;
$container: 800px;
- Variables names should be SNAKE_CASED globals if they are used across top-level selectors (eg. brand colors or input widths), or as dash-cased local variables if they are used multiple times locally:
// good
$COLOR_DANGER: #f00;
$INPUT_WIDTH: 400px;
div {
color: $COLOR_DANGER;
}
.foo {
$border-color: lighten($COLOR_DANGER, 3%);
border-color: $border-color;
width: $INPUT_WIDTH;
&:hover {
border-color: lighten($border-color, 5%);
}
}
// bad
div {
color: #f00;
}
.foo {
border-color: #ff1515;
width: 400px;
}
- Since we use autoprefixer to add vendor prefixes for us, avoid using vendor prefixing mixins:
// good
.foo {
box-shadow: 1px 1px 1px $shadow;
transition: all .1s ease-in;
}
// bad
.foo {
@include box-shadow(1px 1px 1px $shadow);
@include transition(all .1s ease-in);
}
// bad
.foo {
-webkit-box-shadow: 1px 1px 1px $shadow;
-moz-box-shadow: 1px 1px 1px $shadow;
-ms-box-shadow: 1px 1px 1px $shadow;
-o-box-shadow: 1px 1px 1px $shadow;
box-shadow: 1px 1px 1px $shadow;
-webkit-transition: all .1s ease-in;
-moz-transition: all .1s ease-in;
-ms-transition: all .1s ease-in;
-o-transition: all .1s ease-in;
transition: all .1s ease-in;
}
Local variable may still be worth to exist even it is ONLY used once locally. Because we may use it in higher/lower hierarchy in the future. e.g.
but later we may have this: