Introduction
All CSS in a codebase should be consistent and give the impression it was written by a sole developer. It should be understandable and simple to work with.
Syntax
Consistency is the most important factor when contributing to existing CSS. Generally the following structure should be used:
- 4 space indentation (no tabs)
- 1 declaration per line
- A space after the colon separating the property and value pair
- Lowercase strings
- Always apply a semicolon to the last declaration
- Each selector should have it’s own line
- Include a space before the opening brace of a declaration block
- Closing braces of a declaration block should be on a separate line
- Quote attribute selectors with single quotes
- Separate rules by new lines
###Strings Prefer single quotes over double quotes. Once a standard has been set, stick with it.
###Declaration Order Alphabetical ordering is preferred. Although a very debatable subject, this style suits an unknown team size because of the following:
- Consistency is the most important factor
- Everyone knows the alphabet
- Not all css contributors can distinguish between box-model properties, positioning, visual and typography
- No learning curve required for custom ordering preferences
###Units Values of 0 should be unitless. Favour rem over px. Favour seconds over milliseconds. Use relative units whenever possible such as on line-height. Avoid unnecessary leading zero’s.
If fallback values are required use a mixin when using Sass, otherwise add a fallback in a browser specific file.
###Colours Colours should be written as lowercase hexademical values and in shorthand when possible. If transparency is required favour rgba. Sass can parse hex values for rgba so colour variables and map values can always be stored in hex.
###Shorthand Group properties into shorthand use when appropriate to avoid adding multiple properties:
Ensure that shorthand is not used repeatedly when it is not needed. If only one value needs changing do not use shorthand:
###Important rule Never use this. Rework selectors instead. You will reap the benefits in the long term.
###Vendor Prefixes Prefixes should never be used in Sass files and should be added via a build tool to ensure only required prefixes are added. IE specific vendor prefixes should be added to an IE only file or through the use of an IE mixin.
Selectors
###Naming Class and variable names should be lowercase and hyphen separated rather than camelcase or snakecase.
###ID’s Never use ID’s for selectors. If you are forced to reference an ID then use an attribute selector instead. This has the same specificity as a class and so can be overwritten without increasing specificity levels.
###Qualifying Never tag-qualify. By qualifying selectors specificity becomes a problem. Always rework your markup and class structure to address inheritance issues instead of qualifying.
###JS-Classes Any classname prefixed with js- should not have any presentational properties attached. These are used as references for JS Classes. Instead is- prefixed classes should be used as state classes.
This allows for the removal of JS without interfering with presentation.
Box Model
A document should use 1 box-model type consistently. Once a box-sizing value has been set there should not be any other elements using an alternate value.
###Float Containment Clearing should be performed through a pseudo-class clearfix. Overflow clearing should not be used.
Inheritance
Inheritance should be used whenever possible to avoid declaration duplications.
Avoid redeclaring values when they aren’t necessary:
Comments
Comments should be added at the top of each component to indicate the purpose and use of the component. They should also be added above a selector when appropriate to explain references to other elements/classes/components. These should use the // syntax. e.g.
Sass mixins and functions should also be marked with annotations providing a description, any arguments and dependencies. Sassdocs annotations are preferred.
For a Sass projects main file a table of contents should be added at the top of the file for reference of all partials within the project. This should be updated whenever a partial is added e.g.