Share
Embed
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
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
63 slides
-
Nathan Henderson WITH SASS AND COMPASS KICKING ASS (CSS for
awesome people) 2012 EDITION -
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 -
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 -
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 -
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 -
• 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 -
• 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 -
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; } 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 -
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 -
• “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 -
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 -
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? -
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 -
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 -
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 -
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 -
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 -
EXAMPLE FO’ REALZ // Dimensions $page_width: 960px $nav_tabs: 6 //
number of navigation tabs $tab_width: round($page_width / $nav_tabs) - 1 -
EXAMPLE FO’ REALZ // Dimensions $page_width: 960px $nav_tabs: 7 //
number of navigation tabs $tab_width: round($page_width / $nav_tabs) - 1 -
EXAMPLE FO’ REALZ // Dimensions $page_width: 960px $nav_tabs: 7 //
number of navigation tabs $tab_width: round($page_width / $nav_tabs) - 1 -
USING VARIABLES • Colors • Font Stacks • Margins &
Padding • Border widths • Border radii -
MIXINS @mixin large-text font-family: Helvetica, Arial, sans-serif font-size: 20px font-weight:
bold color: #ff0000 #content h1 @include large-text -
MORE MIXINS @mixin ez-border($width, $color) border-width: $width border-style: solid border-color:
$color #aboutbox @include ez-border(2px, #eee) -
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 -
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) -
FUN WITH MIXINS • Mixin definitions can include other mixins
• Try the ‘&’ parent selector • Global variables in mixins -
@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 -
@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 -
@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 -
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> -
@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 -
@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 -
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 -
THE GOOD STUFF • Variables • Mixins • Selector Inheritance
“@extend” • Other goodies -
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 -
@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 -
USING SASS sass input.sass output.css sass --watch input.sass:output.css sass --watch
app/sass:public/stylesheets or… -
WHAT IS IT? • Extends Sass to be a full-fledged
stylesheet framework • Mixins, helpers, new functions, resets & more -
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) -
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: -
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 -
• 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