Repository navigation
CSS & SASS Standards
Bad
.componentTopHeroPanel {
// code here
}
.component_top_hero_panel {
// code here
}Good
.component--top-hero-panel {
// code here
}While it is possible to select elements by ID in CSS, it should generally be considered an anti-pattern. ID selectors introduce an unnecessarily high level of specificity to your rule declarations, and they are not reusable.
For more details, please read CSS Wizardry's article
Bad
#btn-orange {
//
}When using multiple selectors in a rule declaration, give each selector its own line.
Bad
.class-no, .class-not-good, .class-not-good-at-all {
// ...
}Good
.class-good,
.class-one,
.class-one-per-line {
// ...
}- Put a space before the opening brace { in rule declarations
- In properties, put a space after, but not before, the : character.
- Put closing braces } of rule declarations on a new line
- Put blank lines between rule declarations
Bad
.bad-spacing-1{ // bad
font-size :18px; // bad
color: white;} // bad
.bad-spacing-2{ // bad
font-size :18px; //bad
}Good
.good-spacing-1 {
font-size: 18px;
color: white;
}
.good-spacing-2 {
font-size :18px;
}- Prefer line comments (// in Sass-land) to block comments.
- Prefer comments on their own line. Avoid end-of-line comments.
- Write detailed comments for code that isn't self-documenting:
- Uses of z-index
- Compatibility or browser-specific hacks
OOCSS, or “Object Oriented CSS”, is an approach for writing CSS that encourages you to think about your stylesheets as a collection of “objects”: reusable, repeatable snippets that can be used independently throughout a website.
- It helps create clear, strict relationships between CSS and HTML
- It helps us create reusable, composable components
- It allows for less nesting and lower specificity
- It helps in building scalable stylesheets
.btn-red {
background: red;
font-size: 20px;
// ...
}.btn-red {
background: red;
font-size: 20px;
@include transition(all .3s ease);
// ...
}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-red {
background: red;
font-size: 20px;
@include transition(all .3s ease);
// ...
.arrow{
padding-left: 10px;
}
}