[ITCSS_PREFIX]-[BLOCK_NAME]__[ELEMENT_NAME]--[MODIFIER_NAME]
.NEVER STYLE USING ID SELECTORS. Just don’t do it thank you :)
.scss
syntax, never the original .sass
syntax@include
declarations logically (see below)Property declarations
List all standard property declarations, anything that isn’t an @include
or a nested selector.
.btn-green {
background: green;
font-weight: bold;
// ...
}
Nested selectors
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.
.btn {
background: green;
font-weight: bold;
@include transition(background 0.5s ease);
.icon {
margin-right: 10px;
}
}
BEM
When ordering BEM classes, group modifiers with their root element:
.c-teaser-card { ... }
.c-teaser-card--black { ... }
.c-teaser-card__header { ... }
.c-teaser-card__header--left { ... }
.c-teaser-card__title { ... }
.c-teaser-card__title--large { ... }
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
).
Color variables should be written with similar name-spacing when appropriate:
Bad
$blue: #4049FF;
$light-blue: #AFBBFF;
$transparent-banana: transparentize(#FFEA16, 0.5);
Good
$blue: #4049FF;
$blue-light: #AFBBFF;
$banana-transparent: transparentize(#FFEA16, 0.5);
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.
Basic font styles should be written and included as mixins. Doing this allows repeat usage of font styles across the project. Base font classes should also be used for headings - Never style H1 - h6 - reset heading styles and style them with classes. Font mixins should not declare margins or padding - just font-based styles.
@mixin title-serif-lg() {
font-size: 7.2rem;
font-weight: 900;
}
.f-title-serif-lg() {
@include title-serif-lg();
margin-bottom: 2rem;
}
Do not use @extend
directives. Gzipping should handle most of the savings you would have gained by using @extend
, and you can DRY up your stylesheets nicely with mixins.
Do not nest selectors more than three levels deep!
.page-container {
.content {
.profile {
// STOP!
}
}
}
When selectors become this long, you’re likely writing CSS that is:
Again: never nest ID selectors!
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.
When writing sass for BEM elements, do not nest element or modifier suffixes. This helps improve searching in an IDE:
Bad
.c-teaser-card {
&--black {
// Stuff
}
&__header {
// Stuff
}
&__title {
// Stuff
}
}
Good
.c-teaser-card {
// Stuff
}
.c-teaser-card--black { ... }
.c-teaser-card__header { ... }
.c-teaser-card__title { ... }
Scss files should be saved in the same directory as the Fractal component, rather than in the assets/scss
directory. Fractal component scss files can then be imported with a glob statement in a base scss file.