Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A reasonable coding approach to CSS and Sass

A reasonable coding approach to CSS and Sass

How to properly write your code so that its easy for others to understand.

Avatar for Umar Waliyullah

Umar Waliyullah

July 30, 2016
Tweet

Other Decks in Programming

Transcript

  1. History ◇ CSS 1 - 1996 ◇ CSS 2 -

    1998 ◇ CSS 3 - 1998 – 2014 CSS 4 – 2016?
  2. Rule declaration 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:
  3. Selectors 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:
  4. Properties 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:
  5. Formatting ◇ Use soft tabs (2 spaces) for indentation ◇

    Prefer dashes over camelCasing in class names. ◇ Underscores and PascalCasing are okay if you are using BEM ◇ Do not use ID selectors ◇ When using multiple selectors in a rule declaration, give each selector its own line. ◇ Put a space before the opening brace { in rule declarations ◇ In properties, put a space after, but not before, the : character. ◇ Put closing braces } of rule declarations on a new line ◇ Put blank lines between rule declarations
  6. Comments ◇ Prefer line comments (// in Sass-land) to block

    comments. ◇ Prefer comments on their own line. Avoid end-of-line comments. ◇ Write detailed comments for code that isn't self- documenting: ■ Uses of z-index ■ Compatibility or browser-specific hacks
  7. OOCSS and BEM We encourage some combination of OOCSS and

    BEM for these reasons: ◇ It helps create clear, strict relationships between CSS and HTML ◇ It helps us create reusable, composable components ◇ It allows for less nesting and lower specificity ◇ It helps in building scalable stylesheets
  8. ID selectors 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 to your rule declarations, and they are not reusable.
  9. JavaScript hooks 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. We recommend creating JavaScript-specific classes to bind to, prefixed with .js-:
  10. Syntax Use the .scss syntax, never the original .sass syntax

    Order your regular CSS and @include declarations logically (see below)
  11. Variables Prefer dash-cased variable names (e.g. $my-variable) over camelCased or

    snake_cased variable names. It is acceptable to prefix variable names that are intended to be used only within the same file with an underscore (e.g. $_my-variable).
  12. Mixins Mixins should be used to DRY up your code,

    add clarity, or abstract complexity--in much the same way as well- named functions.
  13. Nested selectors Do not nest selectors more than three levels

    deep! When selectors become this long, you're likely writing CSS that is: ◇Strongly coupled to the HTML (fragile) —OR— ◇Overly specific (powerful) —OR— ◇Not reusable