Generic code conventions for html, css and javascript. Based in some cases on the Airbnb conventions.
- Component templates >
some-name.template.html
- Partials > >
some-name.partial.html
- Other files >
some-name.html
- Sibling elements with children should have a line serving as separator.
- Use semantic html.
- Line breaking when attributes make the line are too long
- 1 soft tab indentation
- html attributes first (id, class, type, name...)
- angular attributes next (*, [], ())
- Id Selectos shouldn't be used as css selectors but can be use for other reasons. Use undersocres for word separation.
- Use soft tabs (2 spaces) for indentation.
- When using multiple selectors in a rule declaration, give each selector its own line.
- Put blank lines between rule declarations.
- Order properties alphabetacaly
Bad
.avatar{
color: black;
border-radius:50%;
border:2px solid white; }
.no, .nope, .not_good {
// ...
}
Good
.avatar {
border-radius: 50%;
border: 2px solid white;
color: black;
}
.one,
.selector,
.per-line {
// ...
}
- Use dashes in class selectors.
- Do not use ID selectors.
- Avoid element selectors, except for top level rules.
- Comments on their own line. Avoid end-of-line comments.
- Use
0
instead ofnone
to specify that a style has no border.
Bad
.foo {
border: none;
}
Good
.foo {
border: 0;
}
- Use the
.scss
syntax, never the original.sass
syntax - Order your regular CSS and
@include
declarations logically (see below)
- For angular components use
some-name.style.scss
- Use baseline size variables whenever possible (
$baseline = 10px
). - Use font-size mixin whenever possible.
- Always use variables for colours. Do not use colour variable directly, use a semantic variable instead.
-
Property declarations
List all standard property declarations, anything that isn't an
@include
or a nested selector..btn-green { background: green; font-weight: bold; // ... }
-
@include
declarationsGrouping
@include
s at the end makes it easier to read the entire selector..btn-green { background: green; font-weight: bold; @include transition(background 0.5s ease); // ... }
-
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. Extra rules for the same element go before nested selectors.
.btn { background: green; font-weight: bold; @include transition(background 0.5s ease); &:after { content: ''; } .icon { margin-right: 10px; } }
Do not nest selectors more than three levels deep!
.page-container {
.content {
.profile {
// STOP!
}
}
}