|
13 | 13 | - [Comments](#comments) |
14 | 14 | - [OOCSS and BEM](#oocss-and-bem) |
15 | 15 | - [ID Selectors](#id-selectors) |
16 | | - - [JavaScript hooks](#javascript-hooks) |
17 | 16 | - [Border](#border) |
18 | 17 | 1. [Sass](#sass) |
19 | 18 | - [Syntax](#syntax) |
20 | 19 | - [Ordering](#ordering-of-property-declarations) |
21 | 20 | - [Variables](#variables) |
22 | 21 | - [Mixins](#mixins) |
23 | 22 | - [Extend directive](#extend-directive) |
24 | | - - [Nested selectors](#nested-selectors) |
| 23 | +1. [Scoutside CSS Implemenation](#scoutside-css-implementation) |
25 | 24 | 1. [Translation](#translation) |
26 | 25 | 1. [License](#license) |
27 | 26 |
|
@@ -174,16 +173,6 @@ While it is possible to select elements by ID in CSS, it should generally be con |
174 | 173 |
|
175 | 174 | For more on this subject, read [CSS Wizardry's article](http://csswizardry.com/2014/07/hacks-for-dealing-with-specificity/) on dealing with specificity. |
176 | 175 |
|
177 | | -### JavaScript hooks |
178 | | - |
179 | | -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. |
180 | | - |
181 | | -We recommend creating JavaScript-specific classes to bind to, prefixed with `.js-`: |
182 | | - |
183 | | -```html |
184 | | -<button class="btn btn-primary js-request-to-book">Request to Book</button> |
185 | | -``` |
186 | | - |
187 | 176 | ### Border |
188 | 177 |
|
189 | 178 | Use `0` instead of `none` to specify that a style has no border. |
@@ -267,30 +256,250 @@ Mixins should be used to DRY up your code, add clarity, or abstract complexity-- |
267 | 256 |
|
268 | 257 | `@extend` should be avoided because it has unintuitive and potentially dangerous behavior, especially when used with nested selectors. Even extending top-level placeholder selectors can cause problems if the order of selectors ends up changing later (e.g. if they are in other files and the order the files are loaded shifts). Gzipping should handle most of the savings you would have gained by using `@extend`, and you can DRY up your stylesheets nicely with mixins. |
269 | 258 |
|
270 | | -### Nested selectors |
| 259 | +## Scoutside CSS Implementation |
| 260 | + |
| 261 | +### Use BEM |
271 | 262 |
|
272 | | -**Do not nest selectors more than three levels deep!** |
| 263 | +It may seem a little tricky and tedious at first, but writing clean, unnested BEM reduces nasty css side-effects and makes it easy to understand how classes relate to one another. |
| 264 | + |
| 265 | +Another important benefit of BEM is portability. We write every component as if it could be used in a totally different context than the one it was created for, so that when it is used in a new context, we have no new css to write or modify. |
| 266 | + |
| 267 | +### Definition of "nesting" |
| 268 | + |
| 269 | +These guidelines are largely based around the problems created by nested css and how to avoid them. It's important to distinguish here, that we are only referring to the kind of nesting that limits a selector's styles to a specific context. There are some types of nesting that we do not discourage and that are practical to use. |
| 270 | + |
| 271 | +**Problematic Nesting** |
273 | 272 |
|
274 | 273 | ```scss |
275 | | -.page-container { |
276 | | - .content { |
277 | | - .profile { |
278 | | - // STOP! |
| 274 | +.template-product { |
| 275 | + .product-details { |
| 276 | + h1 { |
| 277 | + font-size: 36px; |
| 278 | + text-transform: uppercase; |
| 279 | + color: $color-blue; |
279 | 280 | } |
280 | 281 | } |
281 | 282 | } |
282 | 283 | ``` |
283 | 284 |
|
284 | | -When selectors become this long, you're likely writing CSS that is: |
| 285 | +If this h1 style is used in a new context, we need to do extra work to locate and unnest these styles. |
| 286 | + |
| 287 | +**Practical Nesting** |
| 288 | + |
| 289 | +```scss |
| 290 | +.selector { |
| 291 | + //... |
| 292 | + @media (min-width: $bp-tablet) { |
| 293 | + //... |
| 294 | + } |
| 295 | + &:after { |
| 296 | + //... |
| 297 | + } |
| 298 | + &.selector--modifier { |
| 299 | + //... |
| 300 | + } |
| 301 | +} |
| 302 | +``` |
| 303 | + |
| 304 | +Everything written within the selector above is technically also considered nesting, however, since it directly relates to the selector and does not limit it to a specific context, it makes sense to nest media queries, psuedo-elements, and modifier classes. |
| 305 | + |
| 306 | +### Only nest css when there are no other practical options |
| 307 | + |
| 308 | +Nesting css puts our styles in highly specific contexts and makes it difficult to move components around the site. We should write anything as if it could be reused in a different context from the one it was originally designed for. This way we save precious time and energy when components pop up in different places, as designers build new pages, features, etc. |
| 309 | + |
| 310 | +**Bad** |
| 311 | +```scss |
| 312 | +.template-product { |
| 313 | + .product-details { |
| 314 | + h1 { |
| 315 | + font-size: 36px; |
| 316 | + text-transform: uppercase; |
| 317 | + color: $color-blue; |
| 318 | + } |
| 319 | + } |
| 320 | +} |
| 321 | + |
| 322 | +// Crap!! This same heading style is used in the new slider section on the homepage. |
| 323 | +``` |
| 324 | + |
| 325 | +**Good** |
| 326 | +```scss |
| 327 | +.main-heading { |
| 328 | + font-size: 36px; |
| 329 | + &.main-heading--caps { |
| 330 | + text-transform: uppercase; |
| 331 | + } |
| 332 | + &.main-heading--blue { |
| 333 | + color: $color-blue; |
| 334 | + } |
| 335 | +} |
| 336 | +``` |
| 337 | +```html |
| 338 | +<h1 class="main-heading main-heading--caps main-heading--blue"> |
| 339 | + I'm product details heading. I need to be an h1 for SEO. |
| 340 | +</h1> |
| 341 | + |
| 342 | +<h2 class="main-heading main-heading--caps main-heading--blue"> |
| 343 | + I'm that new slider section heading. I look exact same as the product details heading. |
| 344 | +</h2> |
| 345 | +``` |
| 346 | + |
| 347 | +**Pro-tip** |
| 348 | + |
| 349 | +If you decide there's no other option than to nest, you can use the parent selector (&) in the following way, so that your selector exists in as few places as possible in the repo, making it easy to find and debug. |
| 350 | + |
| 351 | +```scss |
| 352 | +.selector { |
| 353 | + //... |
| 354 | + .slick-slider & { |
| 355 | + // Compiles to .slick-slider .selector |
| 356 | + } |
| 357 | + .modal & { |
| 358 | + // Compiles to .modal .selector |
| 359 | + } |
| 360 | +} |
| 361 | + |
| 362 | +/* Nice! My selector only exists in one place in the scss directory. I don't have to dig through a bunch of files of nested code |
| 363 | +to see where my change needs to be made.*/ |
| 364 | +``` |
| 365 | + |
| 366 | +As compared to: |
| 367 | + |
| 368 | +```scss |
| 369 | +// _index.scss |
| 370 | +.selector { |
| 371 | + //... |
| 372 | +} |
| 373 | + |
| 374 | +// _slider.scss |
| 375 | +.slick-slider { |
| 376 | + .selector { |
| 377 | + //... |
| 378 | + } |
| 379 | +} |
| 380 | + |
| 381 | +// _modal.scss |
| 382 | +.modal { |
| 383 | + .selector { |
| 384 | + //... |
| 385 | + } |
| 386 | +} |
| 387 | + |
| 388 | +/* Crap! When I search the repo for .selector, it's in 3 different places and 3 different files. Now I have to spend time finding |
| 389 | +where I should make my change */ |
| 390 | +``` |
| 391 | + |
| 392 | +### Only use classes to write styles. No IDs or html tags. |
| 393 | + |
| 394 | +**Bad** |
| 395 | + |
| 396 | +```scss |
| 397 | +button { |
| 398 | + appearance: none; |
| 399 | + border: 1px solid $color-black; |
| 400 | + border-radius: 4px; |
| 401 | + color: $color-black; |
| 402 | + font-weight: 700; |
| 403 | + cursor: pointer; |
| 404 | +} |
| 405 | + |
| 406 | +// Crap, I need these exact styles on an <a> tag |
| 407 | +``` |
| 408 | + |
| 409 | +**Good** |
| 410 | + |
| 411 | +```scss |
| 412 | +.btn { |
| 413 | + appearance: none; |
| 414 | + border: 1px solid $color-black; |
| 415 | + border-radius: 4px; |
| 416 | + color: $color-black; |
| 417 | + font-weight: 700; |
| 418 | + cursor: pointer; |
| 419 | +} |
| 420 | +``` |
| 421 | +```html |
| 422 | +<button class="btn"> |
| 423 | + I'm a button |
| 424 | +</button> |
| 425 | + |
| 426 | +<a class="btn" href="http://www.coolstore.com"> |
| 427 | + I'm a link (but look the same as the button) |
| 428 | +</a> |
| 429 | +``` |
| 430 | + |
| 431 | +By giving everything a class name, our styles are portable and markup-independent. It might seem difficult to give everything a class name, but the BEM convention makes this easier. Because this style of coding is so "class driven", it is makes sense to have the class attribute as the first attribute on any element. |
| 432 | + |
| 433 | +### Write media queries within selectors |
285 | 434 |
|
286 | | -* Strongly coupled to the HTML (fragile) *—OR—* |
287 | | -* Overly specific (powerful) *—OR—* |
288 | | -* Not reusable |
| 435 | +Nesting all styles within a single media query for each breakpoint causes excessive scrolling and the mental burden of remembering what the styles were at previous breakpoints. |
| 436 | + |
| 437 | +When searching a repo for a selector whose styles you need to modify, it is much nicer to find that selector in one place, rather than 10. For this reason, it's best to put the media queries within each selector, so when we search for a selector in the repo, there is one place we can go to see everything about it. |
| 438 | + |
| 439 | +**Bad** |
| 440 | +```scss |
| 441 | +.block__element { |
| 442 | + font-size: 12px; |
| 443 | +} |
| 444 | +@media (min-width: $bp-tablet) { |
| 445 | + .block__element { |
| 446 | + display: flex; |
| 447 | + justify-content: space-around; |
| 448 | + } |
| 449 | +} |
| 450 | +@media (min-width: $bp-desktop) { |
| 451 | + .block__element { |
| 452 | + justify-content: space-between; |
| 453 | + } |
| 454 | +} |
| 455 | +``` |
| 456 | + |
| 457 | +**Good** |
| 458 | +```scss |
| 459 | +.block__element { |
| 460 | + font-size: 12px; |
| 461 | + @media (min-width: $bp-tablet) { |
| 462 | + display: flex; |
| 463 | + justify-content: space-around; |
| 464 | + } |
| 465 | + @media (min-width: $bp-desktop) { |
| 466 | + justify-content: space-between; |
| 467 | + } |
| 468 | +} |
| 469 | +``` |
| 470 | + |
| 471 | +### Use data attributes a Javascript selectors |
| 472 | + |
| 473 | +By using a data attribute as a selector, we can kill two birds with one stone. Not only do we get a selector, but we can also store a value in the data attribute for use in our Javascript. We also segment functionality to something unrelated to css, which makes is easier to see what is affecting a component and how. Lastly, if the class name changes on an element, it still retains it's Javascript functionality. |
| 474 | + |
| 475 | +**Bad** |
| 476 | +```html |
| 477 | +<button class="btn btn--blue" data-variant-id="{{ product.variants.first.id }}">Add to Cart</button> |
| 478 | +``` |
| 479 | +```js |
| 480 | +$('.product-actions .btn').click(function() { |
| 481 | + const variantId = $(this).attr('data-variant-id'); |
| 482 | + CartJS.addItem(variantId, 1); |
| 483 | +}) |
| 484 | + |
| 485 | +// Crap!! Someone changed .btn to .button and now no one can add to cart!!! |
| 486 | +``` |
| 487 | + |
| 488 | +**Good** |
| 489 | +```html |
| 490 | +<button class="btn btn--blue" data-add-to-cart="{{ product.variants.first.id }}">Add to Cart</button> |
| 491 | +``` |
| 492 | +```js |
| 493 | +$('[data-add-to-cart]').on('click', function(){ |
| 494 | + const variantId = $(this).attr('data-add-to-cart'); |
| 495 | + CartJS.addItem(variantId, 1); |
| 496 | +}); |
| 497 | +``` |
289 | 498 |
|
| 499 | +### Use min-width breakpoints |
290 | 500 |
|
291 | | -Again: **never nest ID selectors!** |
| 501 | +The web community has long touted the benefits of mobile-first development. We should use min-width breakpoints as the standard on any new project. |
292 | 502 |
|
293 | | -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. |
294 | 503 |
|
295 | 504 | **[⬆ back to top](#table-of-contents)** |
296 | 505 |
|
|
0 commit comments