CSS
Best practices for Cascading Style Sheets (CSS).
Table of contents
Selectors
Place each selector on its own line
Good
Bad
Order selectors alphabetically
Good
Bad
Surround braces with whitespace
- Include 1 space before opening brace
{
. - Place closing brace
}
on a new line. - Place a new line after closing brace.
Good
Bad
Group related selectors
Increase organisation and readability by grouping related selectors into main and sub sections.
- Indicate main sections using a triple line comment.
- Indicate sub-sections using a single line comment.
Good
Bad
Prefer class selectors to generic element tags
Class selectors have better rendering performance and are more readable than generic element tags.
Good
Bad
Classes
Namespace class names
Prevent collisions with 3rd-party classes by namespacing all class names.
Good
Bad
Lowercase class names
Good
Bad
Prefer purposeful class names to presentational
Good
Bad
Prefix class names based on the closest parent or base class
Good
Bad
Declarations
Place each declaration on its own line
Good
Bad
Group related properties
Group related properties together in the following order:
- Positioning
- Box model
- Typography
- Visual
- Misc
Good
Bad
Order properties alphabetically
Order properties alphabetically within each group.
Good
Bad
Lowercase all values
Good
Bad
Skip leading zero in decimal values
Good
Bad
Skip unit in values where length is 0
Good
Bad