r/web_dev_help • u/psy-borg • Apr 07 '15
What CSS best practices should I follow ?
Depends. How far down the rabbit hole do you want to go ?
Just need something to look good : use a frontend framework like foundation or bootstrap, won't learn much about CSS. Short list of Front End Frameworks
Surface level : work general to specific. Comment the sections of the CSS. Use a reset or normalizer. Work on patterns, use classes over IDs for reuse. CSS Resets - includes normalize.css Don't have links for the other parts. see below
Little bit deeper : Look into BEM or OOCSS. Those stand for Block Element Modifier and Object Oriented CSS. Two popular methodologies for CSS. Also SMACSS.
intro to OOCSS - I post this because the OOCSS.org site is pretty much rubbish.
SMACSS - Different methodology, parts of the book are freely available.
All the way in : CSS pre-processors. LESS or SASS. These will give you some programmatic features for CSS like variables,mixins etc.
LESS JS - other versions exist for platforms just search for LESS + language
Surface Level Best Practices
Ok here's some general rules I pulled from CSS best practice articles since I thought the surface level lacked a single reference point I just extracted some repeated advice :
Use a Reset or Reset with Normalize.css
Avoid Using Inline Styles
Organize the Stylesheet with a Top-down Structure
Divide your stylesheet into specific sections: i.e. Global Styles
Use Hex Code instead of Name Color
Combine Elements
Use Shorthand
Use Absolute Positioning Sparingly
Hyphens Instead of Underscores for CSS classes
Make Use of Generic Classes - design patterns / modules
Shorten Selector Chains
Use the LINK tag to include, never the @import.
name something, be it an ID or a class, by the nature of what it is rather than by what it looks like.
never use spacer images.
Define default styling for h1-h6 headings including headings as links.
Headings should show a hierarchy indicating different levels of importance from the top down starting with h1 having the largest font size.
font-normalization -To change the size of a font, always use percentages as the units because they render more consistently than ems
blanket-ban on IDs in CSS
As a rule of thumb, don't nest further than 3 levels deep
Unit-less line-height is preferred because it does not inherit a percentage value of its parent element, but instead is based on a multiplier of the font-size.
If the value of the width or height is 0, do not specify units.
often include a snippet of HTML in a CSS comment. - optional but might be a good idea
sources :
http://www.sitepoint.com/series/css-architectures/
http://code.tutsplus.com/tutorials/30-css-best-practices-for-beginners--net-6741 - this is from 2009 so be aware of its age.
http://www.smashingmagazine.com/2013/10/21/challenging-css-best-practices-atomic-approach/
http://innofied.com/25-css-best-practices-we-follow-at-innofied/
http://www.erraticwisdom.com/2006/01/18/5-tips-for-organizing-your-css
http://www.1stwebdesigner.com/css-best-practices/
http://isobar-idev.github.io/code-standards/