The ruleset defined by Airbnb for their CSS is the best one-stop set of rules and should be followed in projects.
Consider the implementation of prettier, and/or stylelint in your project as a way to enforce a consistent style.
property: value
pair with a semicolon and new line.box--selected,.item--selected,.title--selected {}
See this codepen for examples of correct usage.
If declarations are to be consistently ordered, it SHOULD be in accordance with a single, simple principle. Define either clustered ordering or alphabetical ordering, depending on the requirements and preferences of the team. The standard approach MUST be documented. Code reviews should enforce the standard.
Use caution - retrospectively ordering properties alphabetically after a lot of css has been written can be a cause of style regression bugs.
We use SASS (with SCSS syntax) as a preprocessor of choice.
.scss
syntax not .sass
@extend
if possible (use mixins instead), but if you do use it...
@extend
statements on the first line of a declaration%placeholder
classes@include
statements at the top of a
declaration block, after any @extend
statements.block { @extend %base-block; &__element { property: value; } &--modifier { property: value; }}
.5em
not 0.5em
)Generally use of BEM and OO principles should avoid falling into any of the bad practice below.
!important
except in case of SMACSS state rules// BADsection.intro { font-family: 'Open Sans', sans-serif; border: 0px;}.header { .title { .primary { em { font-weight: bold; line-height: 0.5rem; } } }}#nav { display: inline;}
camelCase
is acceptable if a classname is tied to a reactive component
name (see the Airbnb OOCSS/BEM advice for reference)// BAD.valid-class-name {}.invalidClassName,.invalid_classname {}// acceptable.infoBlock { &__element { background: yellow; } &--modifier { color: red; }}
Use SMACSS principles for modular CSS. Establish top level folders with the following categories :
Ideally include a main.scss file at the top of the folder structure.
Import an index files from each of the subsidiary folders, and import
files in each directory by using @import
rules in that index.scss
./app
˪/styles
˫/base
| ˫index.scss
| ˪_colours.scss
˪main.scss