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

Intro to Sass

Intro to Sass

For a Sass workshop at work

Max Glenister

April 26, 2017
Tweet

Other Decks in How-to & DIY

Transcript

  1. 1. What is Sass? 2. Similarity to LESS 3. Examples

    of syntax 4. Bit about gulp and postcss
  2. FROM HERE ON WHEN I TALK ABOUT SASS I'M ACTUALLY

    REFERRING TO SCSS (IT'S JUST EASIER TO SAY)
  3. LESS .loop(@counter) when (@counter > 0) { .loop((@counter - 1));

    // next iteration width: (10px * @counter); // code for each iteration } div { .loop(5); // launch the loop } // ! wtf?
  4. SASS @for $n from 1 though 5 { // includes

    limit div { width: 10px * $n; } } // Also available: @each, @while // ! #notbiased
  5. VARIABLES $color1: #f00; // can use hex colors $color2: red;

    // named colors $color3: rgb(255,0,0); // rgb colors $color4: darken($color1, 20%); // built-in functions // and other variables $unitSize: 10px; // values $width: 10 * $unitSize; // maths
  6. DATA STRUCTURES $peopleWithoutKids: Max, Callum, Rich; // comma separated $peopleWithKids:

    Morgan Aymeric Dave; // space separated $peopleWithPets: "Max", "Dave", "Morgan"; // quoted // Nested structures... $favouriteColors: ("Max", #73a325), ("Callum", rgb(255,140,0)); // Maps! $mappy: ( bar: #f00, cof: #fee, );
  7. REAL-WORLD EXAMPLE $cards: (A, ace), (2, two), (3, three), (4,

    four), (5, five), (6, six), (7, seven), (8, eight), (9, nine), (10, ten), (J, jack), (Q, queen), (K, king) ;
  8. PUTTING THE DATA TO USE @each $card in $cards {

    $imgPath: '../assets/cards'; $class: nth($card, 1); $filename: '#{$imgPath}/card-#{nth($card, 2)}.png'; .card-#{$card} { background-image: url($filename); } }
  9. // A function with an @if statement @function bestColor($baseColor, $light,

    $dark) { $bestColor: $light; // If our $baseColor is light, @if (lightness($baseColor) > 50%) { // set our $bestColor to $dark $bestColor: $dark; } @return $bestColor; }
  10. @mixin button($fgColor, $bgColor) { background-color: $bgColor; color: $fgColor; &:hover {

    background-color: lighten($bgColor, 10%); } &.disabled { background-color: desaturate($bgColor, 90%); color: rgba($fgColor, .6); } }
  11. .btn-primary { background-color: #73a325; color: #fff; } .btn-primary:hover { background-color:

    #85bd2b; } .btn-primary.disabled { background-color: #9ea396; color: rgba(255, 255, 255, .6); } .btn-secondary { // ... etc. }
  12. $buttons: ( 'primary': (#fff, #73a325), 'secondary': (#111, #fafafa), ); @each

    $class, $colors in $buttons { .btn-#{$class} { $fg: nth($colors, 1); $bg: nth($colors, 2); @include button($fg, $bg); } }
  13. @function multiplyUnit($unit, $multiplier) { @return $unit * $multiplier; } @function

    capitalize($string) { @return to-upper-case( str-slice("#{$string}", 1, 1) ) + str-slice("#{$string}", 2); }
  14. $things: 'primary', 'secondary', 'tertiary'; @each $thing in $things { .thing-#{$thing}

    { &::after { content: capitalize($thing); // e.g. Primary } } }
  15. %placeholderStyle { background: red; border-radius: 50px; } .extendedStyle { background:

    blue; border-radius: 100px; } .btn-primary { @extend .extendedStyle; } .btn-secondary { @extend %placeholderStyle; color: #fff; } .btn-tertiary { @extend .extendedStyle; color: #000; }
  16. .btn-secondary { background: red; border-radius: 50px; } .extendedStyle, .btn-primary, .btn-tertiary

    { background: blue; border-radius: 100px; } .btn-secondary { color: #fff; } .btn-tertiary { color: #000; }
  17. CONS ELEMENTS @EXTENDING A STYLE ARE GROUPED TOGETHER IN THE

    GENERATED CSS OUTPUT ORDER CHANGES, EXTENDS ARE "HOISTED" TO DEFINITION.
  18. ✨ COLOR FUNCTIONS ✨ darken($color, $amount) // darken a color

    lighten($color, $amount) // lighten a color rgba($color, $opacity) // named/hex colors to rgba red($color) // get red value of a color mix($color1, $color2, $amount) // Mixes two colors
  19. ! STRING AND NUMBER FUNCTIONS ! unquote($string) // Removes quotes

    from a string str-length($string) // Returns the length of a string min($numbers) // Finds the minimum of several numbers max($numbers) // Finds the maximum of several numbers unitless($number) // Returns whether a number has units
  20. // Standard CSS import, will work as usual @import "foo/bar.css";

    // SCSS import, pulls content in to file @import "foo/bar.scss"; // Can also omit the extension @import "foo/bar"; // Can also traverse directories @import "../baz/bar";
  21. # 1. install node # 2. install npm # 3.

    install gulp globally: $ npm install -g gulp # 4. install all WebClient dependencies $ npm install .
  22. # Builds Sass files $ gulp build:sass # Deletes all

    generated CSS files $ gulp clean:sass # Watches for changes to Sass files # and then builds changed files $ gulp watch:sass
  23. 1. Convert all LESS to Sass 2. Get you all

    up to speed with using Sass 3. Refactor existing Sass code
  24. 1. Convert all LESS to Sass 2. Get you all

    up to speed with using Sass 3. Refactor existing Sass code
  25. 1. Convert all LESS to Sass 2. Get you all

    up to speed with using Sass 3. Refactor existing Sass code