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

Kicking Ass with Sass & Compass (2012 Edition)

Kicking Ass with Sass & Compass (2012 Edition)

An updated slide deck of my Sass & Compass talk, delivered at the Fall 2012 Barcamp Rochester at RIT.

Nathan Henderson

October 06, 2012
Tweet

More Decks by Nathan Henderson

Other Decks in Design

Transcript

  1. body { font-family: sans-serif; font-size: 30em; font-weight: bold; } #contents

    { width: 800px; } #contents #sidebar { float: right; width: 200px; } #contents #main { width: 600px; background: #eeeeee; } #contents #main h2 { color: blue; } #footer { height: 200px; } CSS body { font-family: sans-serif; font-size: 30em; font-weight: bold; } #contents { width: 800px; } #contents #sidebar { float: right; width: 200px; } #contents #main { width: 600px; background: #eeeeee; } #contents #main h2 { color: blue; } #footer { height: 200px; } Nested CSS
  2. body { font-family: sans-serif; font-size: 30em; font-weight: bold; } #contents

    { width: 800px; #sidebar { float: right; width: 200px; } #main { width: 600px; background: #eeeeee; h2 { color: blue; } } } #footer { height: 200px; } SCSS body { font-family: sans-serif; font-size: 30em; font-weight: bold; } #contents { width: 800px; } #contents #sidebar { float: right; width: 200px; } #contents #main { width: 600px; background: #eeeeee; } #contents #main h2 { color: blue; } #footer { height: 200px; } Nested CSS
  3. body { font-family: sans-serif; font-size: 30em; font-weight: bold; } #contents

    { width: 800px; #sidebar { float: right; width: 200px; } #main { width: 600px; background: #eeeeee; h2 { color: blue; } } } #footer { height: 200px; } SCSS body { font-family: sans-serif; font-size: 30em; font-weight: bold; } #contents { width: 800px; } #contents #sidebar { float: right; width: 200px; } #contents #main { width: 600px; background: #eeeeee; } #contents #main h2 { color: blue; } #footer { height: 200px; } Nested CSS
  4. body { font-family: sans-serif; font-size: 30em; font-weight: bold; } #contents

    { width: 800px; #sidebar { float: right; width: 200px; } #main { width: 600px; background: #eeeeee; h2 { color: blue; } } } #footer { height: 200px; } SCSS • SCSS = “Sassy CSS” • Default Sass syntax • Superset of CSS3 (CSS is SCSS!) • Nested selectors
  5. • SCSS = “Sassy CSS” • Default Sass syntax •

    Superset of CSS3 (CSS is SCSS!) • Nested selectors • Nested properties too body { font: { family: sans-serif; size: 30em; weight: bold; } } #contents { width: 800px; #sidebar { float: right; width: 200px; } #main { width: 600px; background: #eeeeee; h2 { color: blue; } } } #footer { height: 200px; } SCSS
  6. • SCSS = “Sassy CSS” • Default Sass syntax •

    Superset of CSS3 (CSS is SCSS!) • Nested selectors • Nested properties too • Indentation isn’t required, but… body { font: { family: sans-serif; size: 30em; weight: bold; } } #contents { width: 800px; #sidebar { float: right; width: 200px; } #main { width: 600px; background: #eeeeee; h2 { color: blue; } } } #footer { height: 200px; } SCSS
  7. body { font-family: sans-serif; font-size: 30em; font-weight: bold; } #contents

    { width: 800px; #sidebar { float: right; width: 200px; } #main { width: 600px; background: #eeeeee; h2 { color: blue; } } } #footer { height: 200px; } SCSS
  8. body { font-family: sans-serif; font-size: 30em; font-weight: bold; } #contents

    { width: 800px; #sidebar { float: right; width: 200px; } #main { width: 600px; background: #eeeeee; h2 { color: blue; } } } #footer { height: 200px; } SCSS body font-family: sans-serif font-size: 30em font-weight: bold #contents width: 800px #sidebar float: right width: 200px #main width: 600px background: #eeeeee h2 color: blue #footer height: 200px Sass
  9. body { font-family: sans-serif; font-size: 30em; font-weight: bold; } #contents

    { width: 800px; #sidebar { float: right; width: 200px; } #main { width: 600px; background: #eeeeee; h2 { color: blue; } } } #footer { height: 200px; } SCSS body font-family: sans-serif font-size: 30em font-weight: bold #contents width: 800px #sidebar float: right width: 200px #main width: 600px background: #eeeeee h2 color: blue #footer height: 200px Sass
  10. • “Sass” or “indented” syntax • White space aware (2

    space soft tabs are your friends) • Cleaner than CSS body font-family: sans-serif font-size: 30em font-weight: bold #contents width: 800px #sidebar float: right width: 200px #main width: 600px background: #eeeeee h2 color: blue #footer height: 200px Sass
  11. body { font-family: sans-serif; font-size: 30em; font-weight: bold; } #contents

    { width: 800px; } #contents #sidebar { float: right; width: 200px; } #contents #main { width: 600px; background: #eeeeee; } #contents #main h2 { color: blue; } #footer { height: 200px; } CSS body font-family: sans-serif font-size: 30em font-weight: bold #contents width: 800px #sidebar float: right width: 200px #main width: 600px background: #eeeeee h2 color: blue #footer height: 200px Sass
  12. body font-family: sans-serif font-size: 30em font-weight: bold #contents width: 800px

    #sidebar float: right width: 200px #main width: 600px background: #eeeeee h2 color: blue #footer height: 200px Sass • “Sass” or “indented” syntax • White space aware (2 space soft tabs are your friends) • Cleaner than CSS, but so what?
  13. VARIABLES body font-family: sans-serif font-size: 30em font-weight: bold #contents width:

    800px #sidebar float: right width: 200px #main width: 600px background: #eeeeee h2 color: blue #footer height: 200px // Variable Definitions $page-width: 800px $sidebar-width: 200px $primary-color: #eeeeee
  14. VARIABLES body font-family: sans-serif font-size: 30em font-weight: bold #contents width:

    800px #sidebar float: right width: 200px #main width: 600px background: #eeeeee h2 color: blue #footer height: 200px // Variable Definitions $page-width: 800px $sidebar-width: 200px $primary-color: #eeeeee body font-family: sans-serif font-size: 30em font-weight: bold #contents width: $page-width #sidebar float: right width: $sidebar-width #main width: ??? background: $primary-color h2 color: blue #footer height: 200px
  15. VARIABLES body font-family: sans-serif font-size: 30em font-weight: bold #contents width:

    800px #sidebar float: right width: 200px #main width: 600px background: #eeeeee h2 color: blue #footer height: 200px // Variable Definitions $page-width: 800px $sidebar-width: 200px $primary-color: #eeeeee body font-family: sans-serif font-size: 30em font-weight: bold #contents width: $page-width #sidebar float: right width: $sidebar-width #main width: ??? background: $primary-color h2 color: blue #footer height: 200px
  16. VARIABLES body font-family: sans-serif font-size: 30em font-weight: bold #contents width:

    800px #sidebar float: right width: 200px #main width: 600px background: #eeeeee h2 color: blue #footer height: 200px // Variable Definitions $page-width: 800px $sidebar-width: 200px $primary-color: #eeeeee body font-family: sans-serif font-size: 30em font-weight: bold #contents width: $page-width #sidebar float: right width: $sidebar-width #main width: ??? background: $primary-color h2 color: blue #footer height: 200px body font-family: sans-serif font-size: 30em font-weight: bold #contents width: $page-width #sidebar float: right width: $sidebar-width #main width: $page-width - $sidebar-width background: $primary-color h2 color: blue #footer height: 200px
  17. VARIABLES body font-family: sans-serif font-size: 30em font-weight: bold #contents width:

    800px #sidebar float: right width: 200px #main width: 600px background: #eeeeee h2 color: blue #footer height: 200px // Variable Definitions $page-width: 800px $sidebar-width: 200px $primary-color: #eeeeee body font-family: sans-serif font-size: 30em font-weight: bold #contents width: $page-width #sidebar float: right width: $sidebar-width #main width: ??? background: $primary-color h2 color: blue #footer height: 200px body font-family: sans-serif font-size: 30em font-weight: bold #contents width: $page-width #sidebar float: right width: $sidebar-width #main width: $page-width - $sidebar-width background: $primary-color h2 color: blue #footer height: 200px MATH
  18. EXAMPLE FO’ REALZ // Dimensions $page_width: 960px $nav_tabs: 6 //

    number of navigation tabs $tab_width: round($page_width / $nav_tabs) - 1
  19. EXAMPLE FO’ REALZ // Dimensions $page_width: 960px $nav_tabs: 7 //

    number of navigation tabs $tab_width: round($page_width / $nav_tabs) - 1
  20. EXAMPLE FO’ REALZ // Dimensions $page_width: 960px $nav_tabs: 7 //

    number of navigation tabs $tab_width: round($page_width / $nav_tabs) - 1
  21. USING VARIABLES • Colors • Font Stacks • Margins &

    Padding • Border widths • Border radii
  22. MORE MIXINS @mixin ez-border($width, $color) border-width: $width border-style: solid border-color:

    $color #aboutbox @include ez-border(2px, #eee) @mixin ez-border($width: 2px, $color: #eee) border-width: $width border-style: solid border-color: $color #aboutbox @include ez-border
  23. MORE MIXINS @mixin ez-border($width, $color) border-width: $width border-style: solid border-color:

    $color #aboutbox @include ez-border(2px, #eee) @mixin ez-border($width: 2px, $color: #eee) border-width: $width border-style: solid border-color: $color #aboutbox @include ez-border #aboutbox @include ez-border(1px, #444)
  24. FUN WITH MIXINS • Mixin definitions can include other mixins

    • Try the ‘&’ parent selector • Global variables in mixins
  25. @EXTEND <div class="alert criticalAlert"> Oh snap! The roof is on

    fire! </div> .alert border: 1px #f00 background-color: #eee .criticalAlert border-width: 3px color: #8a0808
  26. @EXTEND <div class="alert criticalAlert"> Oh snap! The roof is on

    fire! </div> .alert border: 1px #f00 background-color: #eee .criticalAlert border-width: 3px color: #8a0808 .alert border: 1px #f00 background-color: #eee .criticalAlert @extend .alert border-width: 3px color: #8a0808
  27. @EXTEND <div class="alert criticalAlert"> Oh snap! The roof is on

    fire! </div> .alert border: 1px #f00 background-color: #eee .criticalAlert border-width: 3px color: #8a0808 <div class="criticalAlert"> Oh snap! The roof is on fire! </div> .alert border: 1px #f00 background-color: #eee .criticalAlert @extend .alert border-width: 3px color: #8a0808
  28. MULTIPLE @EXTENDS .alert border: 1px #f00 background-color: #eee .criticalAlert border-width:

    3px color: #8a0808 .greatGooglyMoogly @extend .alert @extend .criticalAlert border-style: dashed <div class="greatGooglyMoogly"> Oh snap! The roof is on fire! </div>
  29. @EXTEND VS. MIXINS @extend modifies selectors, mixins add properties &

    values .alert border: 1px #f00 background-color: #eee .criticalAlert @extend .alert border-width: 3px color: #8a0808 Sass Input (@extend) .alert, .criticalAlert border: 1px #f00 background-color: #eee .criticalAlert border-width: 3px color: #8a0808 CSS Output
  30. @EXTEND VS. MIXINS @extend modifies selectors, mixins add properties &

    values @mixin alert border: 1px #f00 background-color: #eee .criticalAlert @mixin alert border-width: 3px color: #8a0808 Sass Input (@mixin) .criticalAlert border: 1px #f00 background-color: #eee border-width: 3px color: #8a0808 CSS Output
  31. PLACEHOLDER SELECTOR %-prefixed selectors don’t get rendered to CSS %alert

    border: 1px #f00 background-color: #eee .criticalAlert @extend %alert border-width: 3px color: #8a0808 Sass Input .criticalAlert border: 1px #f00 background-color: #eee .criticalAlert border-width: 3px color: #8a0808 CSS Output
  32. PARENT SELECTORS a font-weight: bold text-decoration: none a:hover text-decoration: underline

    a font-weight: bold text-decoration: none &:hover text-decoration: underline
  33. @IMPORT & PARTIALS @import "rounded-corners", "text-shadow" filenames that start with

    an underscore won’t be compiled into files _rounded-corners.sass _text-shadow.sass
  34. WHAT IS IT? • Extends Sass to be a full-fledged

    stylesheet framework • Mixins, helpers, new functions, resets & more
  35. CSS3 HELPERS -webkit-border-radius: 4px -moz-border-radius: 4px -o-border-radius: 4px -ms-border-radius: 4px

    -khtml-border-radius: 4px border-radius: 4px @import "compass/css3/border-radius" @include border-radius(4px)
  36. OTHER HELPERS @include adjust-lightness($color, $amount) Color: @include font-face($name, $font-files, $eot)

    Fonts: @include clearfix Clearfix: @include horizontal-list($padding, $direction) List Nav: and many more! @import "icon/*.png" @include all-icon-sprites Spriting:
  37. TOOLS THAT USE SASS & COMPASS • Middleman http://middlemanapp.com •

    CodeKit http://incident57.com/codekit/ • Scout http://mhs.github.com/scout-app/ • Many other Ruby-based projects, including Rails
  38. • Keep your stylesheets DRY • Revisions are easier (no

    more “find & replace all”) • Bleeding edge CSS properties become production-ready • Build reuseable mixins & functions • Create your own stylesheet framework • You’ll be awesome