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