Skip to content

Instantly share code, notes, and snippets.

@df2k2
Created October 9, 2019 13:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save df2k2/7b9289a7bbab3972f681333ee0f9ceee to your computer and use it in GitHub Desktop.
Save df2k2/7b9289a7bbab3972f681333ee0f9ceee to your computer and use it in GitHub Desktop.

Code smells in CSS

How can you tell if your CSS code smells? What are the signs that the code is sub-optimal, or that the developer hasn't done a good job? What do you look for in the code to determine how good or bad it is?

  1. Styles that undo styles
  2. Magic Numbers - header.menu { margin: 12px 37px 9px 6px; }
  3. Complex Selectors - header.top-header .nav-bar nav.menu ul.main-nav > li a.link-item { font-weight: bold; }
  4. Increasing Specificity Needlessly - body.cms-home-page .footer-block .footer-title { font-size:35px; }
  5. "!important" - !important should only ever be used proactively, not reactively. - .footer-block .footer-title { font-size:33px !important; }
  6. @import - <style> @import "/css/styles.css" </style>
  7. Usage of IDs (#) - #header.main-header nav#main-nav { margin: 0 auto; }
  8. Hard-coded/absolute values - h1 { line-height: 32px; font-size: 22px; }
  9. Image absolute referencing - background: transparent url("http://www.insecure-reference.com/from_secure_https_website/linked_background.jpg") no-repeat top center;
  10. Uncontained absolute positioning
  11. Brute-forcing - .dropdown-menu { z-index:999999; }
  12. Classes
  • Nomenclature
    • Meaningless
    • Excessively long / unique
  1. Broad Selectors - div { font-size:14px; padding: 15px 10px; }
  2. Qualified selectors - ul.nav, a.button, span.sub-title
  3. Meaningless and unique media-query breakpoints - @media only screen and (max-width: 524px) { }
  4. Prefixes for ancient browser technology
  5. Support for old IE - Reference http://caniuse.com/ for what is no longer used.
  6. Fonts - Unoptimized, too many different fonts, non-supported fonts
  7. Colors - Too many different colors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment