CSS Class Names
- Keep classes lowercase and use dashes (not underscores or camelCase). Dashes serve as natural breaks in related class (e.g.,
.btn-primary
and.btn-secondary
). - Avoid excessive and arbitrary shorthand notation.
.btn
could be used for button, but.s
doesn’t mean anything. - Keep classes as short and succinct as possible.
- Do not be afraid to use longer names when nessasary.
- Use meaningful names; use structural or purposeful names over presentational.
- Prefix classes based on the closest parent or base class.
- Use IDs to target with Javascript, but keep these IDs out of your CSS.
It’s also useful to apply many of these same rules when creating Sass variable names.
// Bad example
.t { ... }
.red { ... }
.header { ... }
// Good example
.tweet { ... }
.important { ... }
.tweet-header { ... }
More Stylesheets tutorials
-
SCSS Math Operators
Best practices for writing easy to maintain stylesheets.
-
SCSS Nesting
Best practices for writing easy to maintain stylesheets.
-
CSS Syntax
Best practices for writing easy to maintain stylesheets.
-
CSS Shorthand Notation
Best practices for writing easy to maintain stylesheets.
-
CSS Selectors
Best practices for writing easy to maintain stylesheets.
-
CSS Media Queries
Best practices for media queries inside your stylesheets.
-
CSS Declaration Order
Best practices for order of declarations inside stylesheet elements.
-
Code Comments
Best practices for writing easy to maintain code.
-
SCSS Extend and Mixin
Best practices for writing easy to maintain stylesheets.