├── .scss-lint.yml └── README.md /.scss-lint.yml: -------------------------------------------------------------------------------- 1 | linters: 2 | 3 | BorderZero: 4 | enabled: true 5 | convention: none 6 | 7 | BemDepth: 8 | enabled: true 9 | 10 | DeclarationOrder: 11 | enabled: false 12 | 13 | LeadingZero: 14 | enabled: false 15 | 16 | PropertySortOrder: 17 | enabled: false 18 | 19 | QualifyingElement: 20 | enabled: false 21 | 22 | SelectorFormat: 23 | enabled: true 24 | convention: hyphenated_BEM 25 | class_convention: ^(?!js-).* 26 | class_convention_explanation: should not be written in the form js-* 27 | 28 | SingleLinePerProperty: 29 | enabled: true 30 | allow_single_line_rule_sets: false 31 | 32 | StringQuotes: 33 | enabled: true 34 | style: double_quotes 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Airbnb CSS / Sass Styleguide 2 | 3 | *A mostly reasonable approach to CSS and Sass* 4 | 5 | ## Table of Contents 6 | 7 | 1. [Terminology](#terminology) 8 | - [Rule Declaration](#rule-declaration) 9 | - [Selectors](#selectors) 10 | - [Properties](#properties) 11 | 1. [CSS](#css) 12 | - [Formatting](#formatting) 13 | - [Comments](#comments) 14 | - [OOCSS and BEM](#oocss-and-bem) 15 | - [ID Selectors](#id-selectors) 16 | - [JavaScript hooks](#javascript-hooks) 17 | 1. [Sass](#sass) 18 | - [Syntax](#syntax) 19 | - [Ordering](#ordering-of-property-declarations) 20 | - [Mixins](#mixins) 21 | - [Placeholders](#placeholders) 22 | - [Nested selectors](#nested-selectors) 23 | 24 | ## Terminology 25 | 26 | ### Rule declaration 27 | 28 | A “rule declaration” is the name given to a selector (or a group of selectors) with an accompanying group of properties. Here's an example: 29 | 30 | ```css 31 | .listing { 32 | font-size: 18px; 33 | line-height: 1.2; 34 | } 35 | ``` 36 | 37 | ### Selectors 38 | 39 | In a rule declaration, “selectors” are the bits that determine which elements in the DOM tree will be styled by the defined properties. Selectors can match HTML elements, as well as an element's class, ID, or any of its attributes. Here are some examples of selectors: 40 | 41 | ```css 42 | .my-element-class { 43 | /* ... */ 44 | } 45 | 46 | [aria-hidden] { 47 | /* ... */ 48 | } 49 | ``` 50 | 51 | ### Properties 52 | 53 | Finally, properties are what give the selected elements of a rule declaration their style. Properties are key-value pairs, and a rule declaration can contain one or more property declarations. Property declarations look like this: 54 | 55 | ```css 56 | /* some selector */ { 57 | background: #f1f1f1; 58 | color: #333; 59 | } 60 | ``` 61 | 62 | ## CSS 63 | 64 | ### Formatting 65 | 66 | * Use soft tabs (2 spaces) for indentation 67 | * Prefer dashes over camelCasing in class names. Underscores are OK if you're using BEM (see [OOCSS and BEM](#oocss-and-bem) below). 68 | * Do not use ID selectors 69 | * When using multiple selectors in a rule declaration, give each selector its own line. 70 | * Put a space before the opening brace `{` in rule declarations 71 | * In properties, put a space after, but not before, the `:` character. 72 | * Put closing braces `}` of rule declarations on a new line 73 | * Put blank lines between rule declarations 74 | 75 | **Bad** 76 | 77 | ```css 78 | .avatar{ 79 | border-radius:50%; 80 | border:2px solid white; } 81 | .no, .nope, .not_good { 82 | // ... 83 | } 84 | #lol-no { 85 | // ... 86 | } 87 | ``` 88 | 89 | **Good** 90 | 91 | ```css 92 | .avatar { 93 | border-radius: 50%; 94 | border: 2px solid white; 95 | } 96 | 97 | .one, 98 | .selector, 99 | .per-line { 100 | // ... 101 | } 102 | ``` 103 | 104 | ### Comments 105 | 106 | * Prefer line comments (`//` in Sass-land) to block comments. 107 | * Prefer comments on their own line. Avoid end-of-line comments. 108 | * Write detailed comments for code that isn't self-documenting: 109 | - Uses of z-index 110 | - Compatibility or browser-specific hacks 111 | 112 | ### OOCSS and BEM 113 | 114 | We encourage some combination of OOCSS and BEM for these reasons: 115 | 116 | * It helps create clear, strict relationships between CSS and HTML 117 | * It helps us create reusable, composable components 118 | * It allows for less nesting and lower specificity 119 | * It helps in building scalable stylesheets 120 | 121 | **OOCSS**, or “Object Oriented CSS”, is an approach for writing CSS that encourages you to think about your stylesheets as a collection of “objects”: reusuable, repeatable snippets that can be used independently throughout a website. 122 | 123 | * Nicole Sullivan's [OOCSS wiki](https://github.com/stubbornella/oocss/wiki) 124 | * Smashing Magazine's [Introduction to OOCSS](http://www.smashingmagazine.com/2011/12/12/an-introduction-to-object-oriented-css-oocss/) 125 | 126 | **BEM**, or “Block-Element-Modifier”, is a _naming convention_ for classes in HTML and CSS. It was originally developed by Yandex with large codebases and scalability in mind, and can serve as a solid set of guidelines for implementing OOCSS. 127 | 128 | * CSS Trick's [BEM 101](https://css-tricks.com/bem-101/) 129 | * Harry Roberts' [introduction to BEM](http://csswizardry.com/2013/01/mindbemding-getting-your-head-round-bem-syntax/) 130 | 131 | **Example** 132 | 133 | ```html 134 | 143 | ``` 144 | 145 | ```css 146 | .listing-card { } 147 | .listing-card--featured { } 148 | .listing-card__title { } 149 | .listing-card__content { } 150 | ``` 151 | 152 | * `.listing-card` is the “block” and represents the higher-level component 153 | * `.listing-card__title` is an “element” and represents a descendant of `.listing-card` that helps compose the block as a whole. 154 | * `.listing-card--featured` is a “modifier” and represents a different state or variation on the `.listing-card` block. 155 | 156 | ### ID selectors 157 | 158 | While it is possible to select elements by ID in CSS, it should generally be considered an anti-pattern. ID selectors introduce an unnecessarily high level of [specificity](https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity) to your rule declarations, and they are not reusable. 159 | 160 | For more on this subject, read [CSS Wizardry's article](http://csswizardry.com/2014/07/hacks-for-dealing-with-specificity/) on dealing with specificity. 161 | 162 | ### JavaScript hooks 163 | 164 | Avoid binding to the same class in both your CSS and JavaScript. Conflating the two often leads to, at a minimum, time wasted during refactoring when a developer must cross-reference each class they are changing, and at its worst, developers being afraid to make changes for fear of breaking functionality. 165 | 166 | We recommend creating JavaScript-specific classes to bind to, prefixed with `.js-`: 167 | 168 | ```html 169 | 170 | ``` 171 | 172 | ## Sass 173 | 174 | ### Syntax 175 | 176 | * Use the `.scss` syntax, never the original `.sass` syntax 177 | * Order your `@extend`, regular CSS and `@include` declarations logically (see below) 178 | 179 | ### Ordering of property declarations 180 | 181 | 1. `@extend` declarations 182 | 183 | Just as in other OOP languages, it's helpful to know right away that this “class” inherits from another. 184 | 185 | ```scss 186 | .btn-green { 187 | @extend %btn; 188 | // ... 189 | } 190 | ``` 191 | 192 | 2. Property declarations 193 | 194 | Now list all standard property declarations, anything that isn't an `@extend`, `@include`, or a nested selector. 195 | 196 | ```scss 197 | .btn-green { 198 | @extend %btn; 199 | background: green; 200 | font-weight: bold; 201 | // ... 202 | } 203 | ``` 204 | 205 | 3. `@include` declarations 206 | 207 | Grouping `@include`s at the end makes it easier to read the entire selector, and it also visually separates them from `@extend`s. 208 | 209 | ```scss 210 | .btn-green { 211 | @extend %btn; 212 | background: green; 213 | font-weight: bold; 214 | @include transition(background 0.5s ease); 215 | // ... 216 | } 217 | ``` 218 | 219 | 4. Nested selectors 220 | 221 | Nested selectors, _if necessary_, go last, and nothing goes after them. Add whitespace between your rule declarations and nested selectors, as well as between adjacent nested selectors. Apply the same guidelines as above to your nested selectors. 222 | 223 | ```scss 224 | .btn { 225 | @extend %btn; 226 | background: green; 227 | font-weight: bold; 228 | @include transition(background 0.5s ease); 229 | 230 | .icon { 231 | margin-right: 10px; 232 | } 233 | } 234 | ``` 235 | 236 | ### Mixins 237 | 238 | Mixins, defined via `@mixin` and called with `@include`, should be used sparingly and only when function arguments are necessary. A mixin without function arguments (i.e. `@mixin hide { display: none; }`) is better accomplished using a placeholder selector (see below) in order to prevent code duplication. 239 | 240 | ### Placeholders 241 | 242 | Placeholders in Sass, defined via `%selector` and used with `@extend`, are a way of defining rule declarations that aren't automatically output in your compiled stylesheet. Instead, other selectors “inherit” from the placeholder, and the relevant selectors are copied to the point in the stylesheet where the placeholder is defined. This is best illustrated with the example below. 243 | 244 | Placeholders are powerful but easy to abuse, especially when combined with nested selectors. **As a rule of thumb, avoid creating placeholders with nested rule declarations, or calling `@extend` inside nested selectors.** Placeholders are great for simple inheritance, but can easily result in the accidental creation of additional selectors without paying close attention to how and where they are used. 245 | 246 | **Sass** 247 | 248 | ```sass 249 | // Unless we call `@extend %icon` these properties won't be compiled! 250 | %icon { 251 | font-family: "Airglyphs"; 252 | } 253 | 254 | .icon-error { 255 | @extend %icon; 256 | color: red; 257 | } 258 | 259 | .icon-success { 260 | @extend %icon; 261 | color: green; 262 | } 263 | ``` 264 | 265 | **CSS** 266 | 267 | ```css 268 | .icon-error, 269 | .icon-success { 270 | font-family: "Airglyphs"; 271 | } 272 | 273 | .icon-error { 274 | color: red; 275 | } 276 | 277 | .icon-success { 278 | color: green; 279 | } 280 | ``` 281 | 282 | ### Nested selectors 283 | 284 | **Do not nest selectors more than three levels deep!** 285 | 286 | ```scss 287 | .page-container { 288 | .content { 289 | .profile { 290 | // STOP! 291 | } 292 | } 293 | } 294 | ``` 295 | 296 | When selectors become this long, you're likely writing CSS that is: 297 | 298 | * Strongly coupled to the HTML (fragile) *—OR—* 299 | * Overly specific (powerful) *—OR—* 300 | * Not reusable 301 | 302 | 303 | Again: **never nest ID selectors!** 304 | 305 | If you must use an ID selector in the first place (and you should really try not to), they should never be nested. If you find yourself doing this, you need to revisit your markup, or figure out why such strong specificity is needed. If you are writing well formed HTML and CSS, you should **never** need to do this. 306 | --------------------------------------------------------------------------------