CSS Selectors
- Use classes over generic element tag for optimum rendering performance. (e.g.
.section-title
instead ofh2
) - Avoid using several attribute selectors (e.g., [class^=”…”]) on commonly occuring components. Browser performance is known to be impacted by these.
- Keep selectors and nesting short and strive to limit the number of elements in each selector to three.
- Scope classes to the closest parent only when necessary (e.g., when not using prefixed classes).
Additional reading:
/* Bad example */
span { ... }
.page-container #stream .stream-item .tweet .tweet-header .username { ... }
.avatar { ... }
/* Good example */
.avatar { ... }
.tweet-header .username { ... }
.tweet .avatar { ... }
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 Media Queries
Best practices for media queries inside your stylesheets.
-
CSS Declaration Order
Best practices for order of declarations inside stylesheet elements.
-
CSS Class Names
Best practices for writing easy to maintain stylesheets.
-
Code Comments
Best practices for writing easy to maintain code.
-
SCSS Extend and Mixin
Best practices for writing easy to maintain stylesheets.