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

CSS Pre-Processors: Stylus, Less & Sass

CSS Pre-Processors: Stylus, Less & Sass

Given originally at http://frontenddesignconference.com.

Updated for:

http://eduiconf.org/
http://ncdevcon.com/
http://www.fitc.ca/unleashed
http://futureofwebdesign.com/nyc-2013/

An overview of the popular pre-processors: Stylus, Less & Sass, with features subdivided up into easy to learn sections of beginner, intermediate and advanced.

Bermon Painter

April 27, 2014
Tweet

More Decks by Bermon Painter

Other Decks in Programming

Transcript

  1. TOPICS 1. What is a preprocessor 2. Preprocessors (Less, Sass,

    Stylus) 3. Setup 4. Features (beginner, intermediate, advanced) 5. Frameworks
  2. WISEHEA RT D ESIGN → The designer’s guide to the

    OSX command prompt A tutorial for the modern web designer The command prompt. Once the lofty domain of that guy you know with the computer science degree. Now more and more the every day domain of the hacker web designer. Perhaps you’ve mastered a little Javascript or PHP, but you are realizing that the cool kids are playing around with stuff that is only accessible to people who are comfortable with the command prompt. Or, perhaps you are just
  3. BEGINNER style.css > style.scss | style.less | style.styl Creating a

    pre-processed file Just change the file extension
  4. BEGINNER nav { width: 200px; }
 nav ul { list-style:

    none; }
 nav ul li { background: #ccc; } nav ul li a{ color: #333; } Nesting - CSS
  5. BEGINNER nav { width: 200px; ! ul { list-style: none;

    ! li { background: #ccc; ! a {
 color: #ccc; } } } } Nesting - Sass/Less
  6. BEGINNER Nesting - Stylus nav width 200px ! ul list-style

    none ! li background #ccc ! a 
 color #ccc
  7. BEGINNER nav { width: 200px; }
 nav ul { list-style:

    none; }
 nav ul li { background: #ccc; } nav ul li a{ color: #333; } Nesting - Output
  8. BEGINNER nav { width: 200px; ! ul { list-style: none;

    } li { background: #ccc; } a {
 color: #ccc; } } Nesting - Sass/Less
  9. BEGINNER Nesting - Stylus nav width 200px ! ul list-style

    none ! li background #ccc ! a 
 color #ccc
  10. BEGINNER nav { width: 200px; }
 nav ul { list-style:

    none; }
 nav li { background: #ccc; } nav a{ color: #333; } Nesting - Output
  11. BEGINNER nav {
 margin: 0; padding: 20px; a{ color: #000;

    } a:hover, a:focus { color: #999; } a:active { color: #333; } } Reference Selector
  12. BEGINNER nav {
 margin: 0; padding: 20px; a{ color: #000;

    &:hover, &:focus { color: #999; } ! &:active { color: #333; } } } Reference Selector - Sass/Less
  13. BEGINNER nav 
 margin 0 padding 20px ! a color

    #000 ! &:hover, &:focus color #999 ! &:active color #333 Reference Selector - Stylus
  14. BEGINNER nav {
 margin: 0; padding: 20px; } nav a{

    color: #000; } nav a:hover, nav a:focus { color: #999; } nav a:active { color: #333; } Reference Selector - Output
  15. BEGINNER nav { margin: 0; padding: 20px; ! .ie6 &

    { padding: 10px; } .ie7 & { padding: 20px; }
 .touch & { width: 100%; } } Reference Selector - Sass/Less
  16. BEGINNER nav margin 0 padding 20px ! .ie6 & padding

    10px .ie7 & padding 20px 
 .touch & width 100% Reference Selector - Stylus
  17. BEGINNER nav { margin: 0; padding: 20px; } .ie6 nav{

    padding: 10px; } .ie7 nav { padding: 20px; }
 .touch nav { width: 100%; } Reference Selector - Output
  18. INTERMEDIATE @mixin border-radius($radius) { -webkit-border-radius: $radius; -moz-border-radius: $radius; -ms-border-radius: $radius;

    -o-border-radius: $radius; border-radius: $radius; } .box { @include border-radius(5px); } Mixins - Sass
  19. INTERMEDIATE .borders { border: 1px solid #efefef; padding: 10px; }

    p { @extend .borders; font-size: 20px; } ul, ol { @extend .borders; text-transform: uppercase; } @extend – Sass
  20. INTERMEDIATE .borders { border: 1px solid #efefef; padding: 10px; }

    p { &:extend(.borders); font-size: 20px; } ul, ol { &:extend(.borders); text-transform: uppercase; } :extend – Less
  21. INTERMEDIATE .borders border 1px solid #efefef padding 10px ! p

    @extend .borders font-size 20px ! ul, ol @extend .borders text-transform uppercase @extend – Stylus
  22. INTERMEDIATE .borders, p, ul, ol { border: 1px solid #efefef;

    padding: 10px; } p {
 font-size: 20px; } ul, ol { text-transform: uppercase; } @extend – Output
  23. INTERMEDIATE %borders { border: 1px solid #efefef; padding: 10px; }

    p { @extend %borders; font-size: 20px; } ul, ol { @extend %borders; text-transform: uppercase; } @extend + Placeholder – Sass
  24. INTERMEDIATE %borders border 1px solid #efefef padding 10px ! p

    @extend %borders font-size 20px ! ul, ol @extend %borders text-transform uppercase @extend + Placeholder – Stylus
  25. INTERMEDIATE p, ul, ol { border: 1px solid #efefef; padding:

    10px; } p {
 font-size: 20px; } ul, ol { text-transform: uppercase; } @extend – Stylus
  26. ADVANCED $grid-columns: 12; $grid-width: 960px; ! @function calculate-column-width($cols) { @return

    (($grid-width / $grid-columns) * $cols / $grid-width) * 100%; } ! #container { margin: 0 auto; width: 100%; } article { float: left; width: calculate-column-width(8); } aside { float: right; width: calculate-column-width(4); } Functions – Sass
  27. ADVANCED @grid-columns: 12; @grid-width: 960px; ! .calculate-column-width(@cols) { width: (((@grid-width

    / @grid-columns) * @cols / @grid-width) * 100%); } ! #container { margin: 0 auto; width: 100%; } article { float: left; .calculate-column-width(8); } aside { float: right; .calculate-column-width(4); } Functions – Less
  28. ADVANCED grid-columns 12 grid-width 960px ! calculate-column-width(cols) ((grid-width / grid-columns)

    * cols / grid-width) * 100% ! #container margin 0 auto width 100% ! article float left width calculate-column-width(8) ! aside float right width calculate-column-width(4) Functions – Stylus
  29. ADVANCED #container { margin: 0 auto; width: 100%; } article

    { float: left; width: 66.66667%; } aside { float: right; width: 33.33333%; } Functions – Output
  30. ADVANCED rgba($color, $alpha) hue($color) saturation($color) lightness($color) adjust-hue($color, $degrees) lighten($color, $amount)

    darken($color, $amount) saturate($color, $amount) desaturate($color, $amount) grayscale($color) complement($color) invert($color) Color Functions – Sass
  31. ADVANCED lighten(@color, amount); darken(@color, amount); saturate(@color, amount); desaturate(@color, amount); fadein(@color,

    amount); fadeout(@color, amount); fade(@color, amount); spin(@color, amount); Color Functions – Less
  32. ADVANCED rgba(color, alpha) lighten(color, amount) darken(color, amount) desaturate(color, amount) saturate(color,

    amount) invert(color) hue(color) saturation(color) lightness(color) Color Functions – Stylus
  33. ADVANCED @mixin buttons($color, $type) { @if $type == "flat" {

    background-color: $color; } @else if $type == "gradient" { background: linear-gradient($color, darken($color, 20%)); } @else if $type == "glossy" { background: linear-gradient($color 50%,darken($color, 20%) 50%); } @else { background-color: $color; } } .button { @include buttons(green, glossy); } if, else – Sass
  34. ADVANCED .buttons (@color, @type) when (@type == "flat") { background-color:

    @color; } .buttons (@color, @type) when (@type == "gradient") { background: linear-gradient(@color, darken(@color, 20%)); } .buttons (@color, @type) when (@type == "glossy") { background: linear-gradient(@color 50%, darken(@color, 20%) 50%); } ! .button { .buttons(green, glossy); } when (if, else) – Less
  35. ADVANCED buttons(color, type) if type == "flat" background-color color else

    if type == "gradient" background linear-gradient(color, darken(color, 20%)) else if type == "glossy" background linear-gradient(color 50%,darken(color, 20%) 50%) else background-color color ! .button buttons(green, glossy) if, else – Stylus
  36. ADVANCED @for $i from 1px to 5px { .border-#{$i} {

    border: $i solid #000; } } for loop – Sass
  37. ADVANCED .border-1px { border: 1px solid black; } .border-2px {

    border: 2px solid black; } .border-3px { border: 3px solid black; } .border-4px { border: 4px solid black; } for loop – Output
  38. ADVANCED $emotions: happy sad excited mustached; ! @each $emotion in

    $emotions { .feeling-#{$emotion} { background-image: url("images/feeling-#{$emotion}"); } } each loop – Sass
  39. ADVANCED .feeling-happy { background-image: url("images/feeling-happy"); } .feeling-sad { background-image: url("images/feeling-sad");

    } .feeling-excited { background-image: url("images/feeling-excited"); } .feeling-mustached { background-image: url("images/feeling-mustached"); } each loop – Output
  40. ADVANCED $small-breakpoint: 480px; $medium-breakpoint: 768px; $large-breakpoint: 1024px; ! aside {

    width: 100%; @media screen and (min-width: $small-breakpoint) { width: 100px; float: right; } @media screen and (min-width: $medium-breakpoint) { width: 200px; } @media screen and (min-width: $large-breakpoint) { width: 400px; } } Media Queries – Sass
  41. ADVANCED @small-breakpoint: 480px; @medium-breakpoint: 768px; @large-breakpoint: 1024px; ! aside {

    width: 100%; @media screen and (min-width: @small-breakpoint) { width: 100px; float: right; } @media screen and (min-width: @medium-breakpoint) { width: 200px; } @media screen and (min-width: @large-breakpoint) { width: 400px; } } Media Queries – Less
  42. ADVANCED small-breakpoint 480px medium-breakpoint 768px large-breakpoint 1024px ! aside width

    100% ! @media screen and (min-width small-breakpoint) width 100px float right ! @media screen and (min-width medium-breakpoint) width 200px @media screen and (min-width large-breakpoint) width 400px Media Queries – Stylus
  43. ADVANCED aside { width: 100%; } @media screen and (min-width:

    480px) { aside { width: 100px; float: right; } } @media screen and (min-width: 768px) { aside { width: 200px; } } @media screen and (min-width: 1024px) { aside { width: 400px; } } Media Queries – Output
  44. ADVANCED @mixin respond-to($name){ @if $name == small-screen { @media only

    screen and (min-width: 320px) { @content } } @if $name == large-screen { @media only screen and (min-width: 960px) { @content } } } aside { width: 25% @include respond-to(small-screen) { width: 100%; } } @content – Sass
  45. ADVANCED aside { width: 25% } ! @media only screen

    and (min-width: 320px) { aside { width: 100% } } @content – Output
  46. ADVANCED $icons: ( home: e601, about: e602, services: e603 );

    ! @each $icon-name, $icon-keycode in $icons { .icon-#{$icon-name} { &:before { content: "#{$icon-keycode}"; } } } Hash Maps – Sass
  47. ADVANCED icons = { home: e601, about: e602, services: e603

    } ! for icon-name, icon-keycode in icons { .icon-{icon-name} { &:before { content: “\{icon-keycode}"; } } } Hash Maps – Stylus
  48. ADVANCED .icon-home:before { content: “\e601"; } .icon-about:before { content: “\e602";

    } .icon-services:before { content: “\e603"; } Hash Maps – Output