- Do not use inline styles, neither the block kind (
<style></style>
), nor the tag attribute kind (style=””
). - Implied by #1 – use external stylesheets
- Expansion of #2 – Most projects should use a global stylesheet plus section-specific stylesheets. Some projects may use a single stylesheet for all styles.
- Do not use @import, unless the backend system will ‘inline’ the file
- Use semantically descriptive classes and ids. (I.e., “.redbox” is bad, “.errorMessage” is good.)
- Keep it organized. Use project-standard comment blocks to demarcate major sections and subsections (may vary somewhat by project):
- Reset
- Base styling
- Header
- Nav
- Main content
- Content type or item
- Other content type or item
- Etc.
- Aside
- Footer
- Use the least specific selector expression possible
- Avoid !important
- Introduce new namespaced classnames, even new wrapper elements, if needed (and possible) so as to reduce cascade override hell (most commonly happens with nested lists) – example: “.mymodule li li” -> “.mymodule-subitem”
- Abstract repeating visual patterns, use them as base classes and extend and or override them as needed, using a more specific classname.
- Do not use javascript in css (“:expression(…)”) – if absolutely needed, write it in an actual js file, and either conditionally serve it to the affected browser(s), or wrap the code in a lambda that is conditionally executed only in the affected browser(s).
- Use shorthands unless overriding an inherited style (for layout properties, there’s the “trouble” mnemonic: top, right, bottom, left)
- Use sprites, and consider using data-urls and application cache.
- When using sprites or other image-replacement styling, include fallback text content as appropriate (it is usually appropriate) – think “alt tag”
- Use *property to target ie6+7, follow with _property to exclusively target ie6 if needed. Although “hacks” are hotly debated, they preserve specificity and facilitate maintainability.
- Specify units for non-zero properties (0 is 0, but 1 can be px, em, in)
- Do not specify a unit for zero (i.e., “0px” is bad)
- Use ids for content that will never repeat per page, use classnames for elements that do repeat per page – as needed for either.
- Favor classnames over ids.
- Judiciously choose whether to use descendant selectors or namespaced classnames. The performance overhead of descendant selectors is negligible.
- Descendant selector/ root classname example: “.bio img {float:left;}”
- Namespaced classname example: “.bio-img {float:left;}”
- Property group order does not matter, but properties should be grouped together:
- layout-related: position, float, width, height, padding, margin, border
- font-related
- backgrounds
- vendor-prefixed and filters (-webkit-, -moz-, -o-, -ms-)
- hacks follow whatever property they override
- Never code text in all caps in the markup (and usually not all lowercase either). Always choose an appropriate casing: either sentence case or title case. Use text-transform to achieve the desired casing.
-
Property rules all on one line or each on a separate line? separate lines
-
Grouped selectors should appear on (the same line, separate lines): separate lines
-
Indentation: two spaces