└── readme.md /readme.md: -------------------------------------------------------------------------------- 1 | # CSS Coding Standards 2 | 3 | 4 | [Documentation](http://documentup.com/Salesfloor/css-architecture?theme=v1) 5 | 6 | 7 | ## Philosophy 8 | 9 | The easiest way to keep CSS maintainable is by writing less of it. 10 | 11 | * Write object oriented CSS ([OOCSS](https://github.com/stubbornella/oocss/wiki)) that allows common design solutions to be extended and re-used repeatedly. This methodology is scalable, maintainable, and standards-based. 12 | * Writing less CSS means there is a less chance of things going wrong, resulting in a more robust front-end. 13 | * [Specificity](http://css-tricks.com/specifics-on-css-specificity/) is the biggest problem in CSS, but we can work around it by respecting the cascade and building complete styles progressively through extending common styles. 14 | * All styles should follow the principle of keeping specificity as low as possible. 15 | 16 | 17 | ## Architecture 18 | 19 | The source order is designed to take advantage of inheritance in the cascade with the goal of writing DRYer code. 20 | 21 | * Reset 22 | * Variables (used throughout the project) 23 | * Mixins (used throughout the project) 24 | * Patterns/Objects (any repeatable or extendable patterns/skeletons) 25 | * Components (combinations of patterns/objects + extended styles) 26 | * Sections (combinations of components) 27 | * Overrides 28 | 29 | The source order: 30 | 31 | 32 | * Settings 33 | * Global variables 34 | * Configs 35 | * Tools 36 | * Mixins 37 | * Functions 38 | * Base 39 | * Reset 40 | * Patterns 41 | * Forms 42 | * Buttons 43 | * Alerts 44 | * Etc. 45 | * Components 46 | * Video Player 47 | * Newsletter Signup 48 | * Etc. 49 | * Sections (should have a very small amount of content) 50 | * Confirmation page 51 | * Rep listing page 52 | * Themes 53 | * retailers overrides 54 | * Trumps 55 | * Others overrides, helper 56 | 57 | ## Coding Style 58 | 59 | ### Selectors 60 | 61 | * IDs are not to be used in any stylesheets. 62 | * They are far too high in specificity. It would take 256 classes to override one ID. 63 | * Anything an ID can do, a class can do, however classes are more useful since they are reusable. 64 | * You can read arguments for this rule from [Chris Coyier](http://css-tricks.com/a-line-in-the-sand/), [Stubbornella](https://github.com/stubbornella/oocss/wiki/FAQ#should-i-use-ids-to-style-my-content), [Harry Roberts](http://csswizardry.com/2011/09/when-using-ids-can-be-a-pain-in-the-class/) 65 | * Always Avoid over-qualifying selectors. For example: ```p.intro {}``` could have been ```.intro {}``` 66 | * Makes the selector non-reusable. 67 | * Increases specificity unnecessarily. 68 | * Always Avoid chaining selectors unnecessarily. For example: ```.msg.error {}``` could have been ```.error-msg {}```. Chaining two selectors makes it doubly specific with no real benefit. It is less clear for a new developer to understand at a glance what exactly a generic error class does. 69 | * Selectors should be lowercase with dash-separated words, no camel case. 70 | 71 | ### Values 72 | 73 | #### Units 74 | 75 | * Use px for font-size, because it offers absolute control over text. 76 | 77 | * The line-height should always be a multiple of the vertical rhythm variable (unit-less). 78 | All other values should be as relative as possible. (see [magic numbers and absolutes](#magic-numbers-and-absolutes)) 79 | * All vertical spacing around elements should consider the vertical rhythm. 80 | * Use [single direction](http://csswizardry.com/2012/06/single-direction-margin-declarations/) (downward) margin declarations. This would mean only use margin-bottom, no margin-top, to keep a constant downward flow of elements. 81 | * Easier to define vertical rhythm in one fell swoop. 82 | * More confidence in (re)moving components if you know their margins all push in the same direction. 83 | * Components and elements don't have to necessarily live in a certain order if their margins aren't dependent on adjoining sides. 84 | * Not being concerned with collapsing margins means one less thing to worry about. 85 | 86 | #### Colors 87 | 88 | * Colors should be set as variables for theming purpose. 89 | 90 | * Hex notation is prefered for convenience and shortness. 91 | 92 | ### Formatting 93 | 94 | Single-line styles should be formatted as follows: 95 | 96 | ``` 97 | [selector(s)] { [property]: [value]; } 98 | ``` 99 | 100 | Multiple-line styles should be formatted as follows: 101 | 102 | ``` 103 | [selectors] { 104 | [property]: [value]; 105 | [property]: [value]; 106 | } 107 | ``` 108 | 109 | Use soft-tabs with a two space indent. Spaces are the only way to guarantee code renders the same in any person's environment. 110 | 111 | **Avoid [unnecessary nesting](http://thesassway.com/intermediate/avoid-nested-selectors-for-more-modular-css) in SCSS.** 112 | 113 | At most, aim for two levels. It is perfectly reasonable to use nesting when creating components, however limit the amount of levels as much as possible. 114 | 115 | * Increases specificity unnecessarily. 116 | * Limits portability. 117 | * Less efficient. 118 | * If you cannot help it, step back and rethink your overall strategy (either the specificity needed, or the layout of the nesting). 119 | 120 | #### Organisation 121 | 122 | CSS attributes should be organized by types, like: 123 | 124 | ``` 125 | [selectors] { 126 | //layout 127 | margin: 0 0 0 0; 128 | [property]: [value]; 129 | 130 | //skin 131 | background: $background-color; 132 | [property]: [value]; 133 | 134 | //typo 135 | font-size: 12px; 136 | [property]: [value]; 137 | } 138 | ``` 139 | 140 | ### Naming conventions 141 | 142 | Follow a very simple naming convention: hyphen (-) delimited strings, with BEM-like naming for more complex pieces of code. 143 | 144 | #### Hyphen Delimited 145 | 146 | All strings in classes are delimited with a hyphen (-), like so: 147 | 148 | ``` 149 | .page-head {} 150 | 151 | .sub-content {} 152 | ``` 153 | 154 | Camel case and underscores are not used for regular classes; the following are incorrect: 155 | 156 | ``` 157 | .pageHead {} 158 | 159 | .sub_content {} 160 | ``` 161 | 162 | ### BEM like Naming 163 | 164 | BEM, meaning Block, Element, Modifier, is a front-end methodology coined by developers working at Yandex. Whilst BEM is a complete methodology, here we are only concerned with its naming convention. Further, the naming convention here only is BEM-like; the principles are exactly the same, but the actual syntax differs slightly. 165 | 166 | BEM splits components classes into three groups: 167 | 168 | * Block: The sole root of the component. 169 | * Element: A component part of the Block. 170 | * Modifier: A variant or extension of the Block. 171 | 172 | This gives: 173 | 174 | ``` 175 |
176 | 177 | 178 | 179 |

...

180 | 181 |
182 | ``` 183 | *Note: Never reference js- prefixed class names from CSS files. js- are used exclusively from JS files.* 184 | 185 | ### Semantic naming approach 186 | 187 | In CSS, a semantic naming approach implies that the class name describes the content it enclose, but not specifically. Also, never name your classe anything relater to a CSS property (like color, font-family, float, border, etc.). Name your elements based on what they are, not what they look like. 188 | 189 | Semantic classes: 190 | ``` 191 | .main-content {} 192 | 193 | .copyright {} 194 | 195 | .external-link {} 196 | ``` 197 | 198 | Never go specifically or unecessary: 199 | ``` 200 | .left {} 201 | 202 | .link {} 203 | 204 | .title-blue {} 205 | 206 | .intro-p {} 207 | ``` 208 | 209 | ### Magic numbers and absolutes 210 | 211 | A magic number is a number which is used because it just work. These are bad 212 | because they rarely work for any real reason and are not usually very 213 | futureproof or flexible/forgiving. They tend to fix symptoms and not problems. 214 | 215 | * For example, using `.dropdown-nav li:hover ul { top: 37px; }` to move a dropdown 216 | to the bottom of the nav on hover is bad, as 37px is a magic number. 37px only 217 | works here because in this particular scenario the `.dropdown-nav` happens to be 218 | 37px tall. 219 | * Instead you should use `.dropdown-nav li:hover ul { top: 100%; }` which means no 220 | matter how tall the `.dropdown-nav` gets, the dropdown will always sit 100% from 221 | the top. 222 | * Every time you hard code a number think twice; if you can avoid it by using 223 | keywords. 224 | * Every hard-coded measurement you set is a commitment you might not necessarily 225 | want to keep. 226 | 227 | 228 | ### Commenting 229 | 230 | Write more comments! Write as much information as you possibly can. If you integrate a hacky IE9 fix from stack overflow, it wouldn't hurt to briefly explain it and even provide a link so future devs understand the justification of our choices. We can then safely remove code later if we drop support for that particular browser. 231 | 232 | * Write comments to [quasi-qualify](http://csswizardry.com/2012/07/quasi-qualified-css-selectors/) selectors. 233 | * Let's say we have an unqualified ```.breadcrumb {}``` selector. It's not immediately apparent what type of element that is expected to style (is it an ol, ul, li, ... ?). 234 | * What we can do to help other developers identify unqualified selectors is to quickly add a comment inline to indicate that. 235 | Ex: ```.breadcrumb {} // ul``` 236 | 237 | Don't be shy to include HTML code snippets within comments to indicate how the style will be used. Comments are removed at build time so they will never be seen by the end-user. For example: 238 | 239 | ``` 240 | // 243 | .nav {} 244 | .nav > li {} 245 | .nav > li > a {} 246 | ``` 247 | 248 | * Use ```//``` for comment blocks (instead of ```/* */```). 249 | 250 | ### !important 251 | 252 | Because we maintain control of specificity within our cascade, it should never be necessary to use an ```!important``` on any property values. Favor refactoring the troublesome code before resorting to importants. Low specificity is our precious commodity, so importants should be very rare in our stylesheets. 253 | 254 | ## Resources 255 | * [How Specificity Works](http://css-tricks.com/specifics-on-css-specificity/) 256 | * [Specificity Diagram](http://cssspecificity.com/) 257 | * [Avoid nested selectors for more modular CSS](http://thesassway.com/intermediate/avoid-nested-selectors-for-more-modular-css) 258 | 259 | ### Credits 260 | This style guide is a living document based on a mixture of ideas from [GitHub](https://github.com/styleguide), [The Sass Way](http://thesassway.com/), [Chris Coyier](http://css-tricks.com/) and [Harry Roberts](https://twitter.com/csswizardry). --------------------------------------------------------------------------------