So Sassy!

An introduction to Sass and Compass
 
Presenter: Andrew Koebbe

Live Slide Stream: http://bit.ly/sass-live

Ask me about...

  • DIY personal care
  • Ramen Hacking
  • MST3k
  • Sous Vide Cooking
  • Marshmallow cannons!

Andrew Koebbe

Programmer Analyst

PHP (Symfony)

My Love/Hate Relationship With CSS

Back in the '90s

<!-- Start: Local menus -->
<TD BGCOLOR='#6699CC' HEIGHT=20 VALIGN='MIDDLE' NOWRAP COLSPAN=4>
    <FONT COLOR='#FFFFFF' FACE='Verdana, Arial' SIZE=1><B>
          <A STYLE='color:#FFFFFF;text-decoration:none;'
                       HREF='/web/19981205060735/http://microsoft.com/isapi/gomscom.asp?target=/' TARGET='_top'><FONT
            COLOR='#FFFFFF'>Home</FONT></A>  <FONT COLOR='#FFFFFF'>|</FONT>
          <A STYLE='color:#FFFFFF;text-decoration:none;'
                       HREF='/web/19981205060735/http://microsoft.com/isapi/goevents.asp?target=/' TARGET='_top'><FONT
            COLOR='#FFFFFF'>Events</FONT></A>  <FONT COLOR='#FFFFFF'>|</FONT>
          <A STYLE='color:#FFFFFF;text-decoration:none;'
                       HREF='/web/19981205060735/http://microsoft.com/isapi/gomscom.asp?target=/train_cert/'
                       TARGET='_top'><FONT COLOR='#FFFFFF'>Training</FONT></A>  <FONT COLOR='#FFFFFF'>|</FONT>
          <A STYLE='color:#FFFFFF;text-decoration:none;'
                       HREF='/web/19981205060735/http://microsoft.com/isapi/gomscom.asp?target=/msdownload/'
                       TARGET='_top'><FONT COLOR='#FFFFFF'>Downloads</FONT></A>  <FONT
            COLOR='#FFFFFF'>|</FONT>
          <A STYLE='color:#FFFFFF;text-decoration:none;'
                       HREF='/web/19981205060735/http://microsoft.com/isapi/goregwiz.asp?target=/regwiz/forms/pic.asp'
                       TARGET='_top'><FONT COLOR='#FFFFFF'>Newsletters</FONT></A>  <FONT
            COLOR='#FFFFFF'>|</FONT>
          <A STYLE='color:#FFFFFF;text-decoration:none;'
                       HREF='/web/19981205060735/http://microsoft.com/isapi/gomscom.asp?target=/worldwide/'
                       TARGET='_top'><FONT COLOR='#FFFFFF'>International</FONT></A>  <FONT
            COLOR='#FFFFFF'>|</FONT>
          <A STYLE='color:#FFFFFF;text-decoration:none;'
                       HREF='/web/19981205060735/http://microsoft.com/isapi/gomscom.asp?target=/backstage/'
                       TARGET='_top'><FONT COLOR='#FFFFFF'>About Our Site</FONT></A>  <FONT
            COLOR='#FFFFFF'>|</FONT>
    </B></FONT></TD>
<!-- End: Local menus -->

The Old Way

CSS Fun Facts

5 Things CSS
Is Failing At

Nesting (Specificity)

section {
  padding: 0 1em;
}

section p {
  font-size: 1em;
  color: black;
}

section p a {
  font-weight: bold;
  text-decoration: none;
}

section p a:hover {
  text-decoration: underline;
}
section {
  padding: 0 1em;

  p {
    font-size: 1em;
    color: black;

    a {
      font-weight: bold;
      text-decoration: none;
    }

    a:hover {
      text-decoration: underline;
    }
  }
}

Wouldn't it be great...

Extending Selectors

.message {
  padding: .5em;
  margin: 1em 0;
  background: lightgrey;
  border: solid 1px darkgrey;
  color: black;
}

.message-error { /* ????? */ }

.message, message-error {} /* hard to maintain */

.message.error {} /* ???? */
.message-error {
  @extend .message;
  border-color: red;
  border-width: 3px;
}

Wouldn't it be nice...

Variables

(At least not yet)

Calculations

(Well, kinda)

Functions/Mixins

Sass

What Sass Is...

  • A pre-compiler for your CSS (i.e. you end up with valid CSS)
    • Compile while developing and deployment (via CI)
  • A superset of CSS
  • Written in Ruby (other implementations exist)
  • Allows for easily organizing code
  • Allows for rapid development of styles
  • Completely logical

What Sass is not

  • It doesn't require knowledge of Ruby
  • It's not a client side interpreter
  • It doesn't add client side overhead
  • It's not a framework
    • But there are ones available (Susy, Neat, Foundation, Bootstrap, etc)

Alternatives

  • Stylus (Node.js)
  • Less (Node.js or Client side JS)
  • Myth (Node.js)

Install

$ sudo gem install sass
$ sudo apt-get install ruby
$ sudo gem install sass
C:\>gem install sass

Mac

Linux (Ubuntu)

What's in the box?

SCSS & SASS Syntax

article {
  font-size: 16px;
  color: blue;
}
article
  font-size: 16px
  color: blue

SASS

(similar to ruby and python)

SCSS

(backward compatible with CSS. We'll use this today)

Variables

// Colors
$header-bg-color: #ffeeee;
$font-color: rgba(200,225,180,0.7);
$border-color: hsl(300, 75%, 50%);
// Units
$header-height: 30px;
$base-font-size: 2em;
// Strings and Lists
$cool-class: "way-cool";
$body-font-family: "Lato",Helvetica,Arial,sans-serif;
// Booleans
$is-this-thing-on: true;
// Maps
$header: (
  style: italic,
  weight: bold, 
);

Using Variables

heading.way-cool {
    background-color: #ffeeee;
    height: 30px;
    font: "Lato", Helvetica, Arial, sans-serif;
    font-style: italic;
}

Compiled CSS

heading.#{$cool-class} {
    background-color: $header-bg-color;
    height: $header-height;
    @if $is-this-thing-on {
        font: $body-font-family;
    }
    font-style: map-get($header, style);
}

SCSS

$header-bg-color: #ffeeee;
$header-height: 30px;
$cool-class: "way-cool";
$body-font-family: "Lato",...
$is-this-thing-on: true;
$header: (
  style: italic,
  weight: bold, 
);

Nesting

article {
    background-color: slateblue;
}

article h1 {
    font-family: sans-serif;
}

article a {
    color: yellow;
}

article a:hover {
    font-weight: bold;
}
article {
    background-color: slateblue;
    h1 {
        font-family: sans-serif;
    }
    a {
        color: yellow;
        &:hover {
            font-weight: bold;
        }
    }
}

Math Operators

// Unit Math (units must match)
$header-height: 80px + 20px;
$footer-height: ($header-height / 2); //Put division in parenthesis
// Color Math
$new-color: #bb33dd + #113311;
$another-color: #010203 * 2;
$dark-color: darken($another-color, 10);
// Math Functions
$rounded-number: round(1.5);
$floored-number: floor(1.9);

Standard Operators

Color Math

Math Functions

Extending selectors

.error {
    border: 1px #f00;
    background-color: #fdd;
}

.serious-error {
    @extend .error;
    border-width: 3px;
} 
.error, .serious-error {
    border: 1px #f00;
    background-color: #fdd;
}

.serious-error {
    border-width: 3px;
} 

Compiled CSS

Give a selector the same styles as an earlier one

Mixins

@mixin link-color($base-color, $border-width: 1px) {
    color: $base-color;
    &:hover {
        color: darken($base-color, 20);
        border-bottom: $border-width solid complement($base-color);
    }
}

a {
    @include link-color(yellow);
}
a {
    color: yellow;
}

a:hover {
    color: #999900;
    border-bottom: 1px solid blue;
}

Compiled CSS

Mixins allow you to build a function that takes parameters

Plugins/Libraries

Let's do some live coding

Examples

Additional Resources

Questions?

joind.in: http://bit.ly/midcamp-sass

So Sassy - MidCamp

By akoebbe

So Sassy - MidCamp

  • 992
Loading comments...

More from akoebbe