Sass.
The Future of Stylesheets.
Chris Eppstein
Follow me @chriseppstein
Architect - Caring.com
Creator - Compass Stylesheet Framework
Core Contributor - Sass
A website for caregivers of the sick and elderly.

The right information at just the right time

We’re HIRING!
Book:
Sass & Compass In Action
Buy the pre-release now and save 40%

http://www.manning.com/netherland/
DISCOUNT CODE: sass40 (Good through Friday)
Today
Rationale
Why does Sass exist?
What problems does it solve?
Syntax
What does Sass provide?
Best Practices
Tips, tricks & pitfalls
Questions?
Please ask them.
What is “Sass”?
Syntactically Awesome StyleSheets
What is CSS?
• Originated in Academia
• 14 Years Old
• Three Primitives:
  ‣ Selectors
  ‣ Properties
  ‣ Values
CSS is Pretty.
CSS is Pretty.
Pretty dumb.
Never in the history of
software have
developers been
asked to do so much
with so little.
Browsers & Versions
Multiple Form Factors
Desktop, Mobile, Print...
Dozens of Templates
Experiment & Test
Maintain Consistency
Performant
Due Yesterday
New Hotness:
CSS3
33 New Selectors
120+ New Properties
3 New @-rules
Enhanced Media Queries
CSS3
No new syntax*


* Yet.
CSS was designed for the
Mythical Designer who is too stupid to
understand High School Algebra.
If you want to make
something simple for
beginners...
...make it powerful
enough for the experts
to create that simplicity.
But...
There is an alternative.
What is Sass?
• Born from Real World Experience
• Brings Software Development
 Methodologies to Stylesheets:
 ‣ Reusable, distributable libraries
 ‣ DRY
 ‣ Define abstractions
 ‣ Adhere to architectural boundaries
What is Sass?
New Primitives:
• Variables       • Calculations
• Mixins          • Functions
• Selector        • Conditionals
  Inheritance     • Loops
What is Sass?
It’s what CSS would have been if we had
known then what we know now:

Most stylesheets are written by
programmers.
Problem:
CSS Stylesheets
Are hard to maintain
Don’t
Repeat
Yourself
(I don’t have to tell you this -- It’s not like you want to)
A single change.
A single edit.
A single compile.
We have machines.
Let them do the busy work.
Problem:
Same Color
Many Representations
Problem:
Same Color
Different Reasons
rgba(#f00, 1.0)
                   hsla(0deg, 100%, 100%, 1)

hsl(0deg, 100%, 100%)
                                  #f00


  rgb(255, 0, 0)     red
     RED                   rgba(255, 0, 0, 1.0)
Solution:
Variables
The most requested CSS enhancement
Use variables anywhere a value is allowed.
Problem:
Magic Values
Lost knowledge
Deceptively Complex
Solution:
Transformations
Derive values during compilation
As complex as it actually is.
Sass Color Functions

mix(red, blue, 40%)         =>   #660099
lighten(red, 10%)           =>   #ff3333
darken(red, 10%)            =>   #cc0000
desaturate(red, 10%)        =>   #f20d0d
saturate(#511, 10%)         =>   #5a0c0c
transparentize(red, 0.25)   =>   rgba(255, 0, 0, 0.75)
opacify(red, 0.25)          =>   rgba(255, 0, 0, 0.75)
grayscale(red)              =>   #808080
complement(red)             =>   #00ffff



There’s more on the Sass website.
Solution:
Make Reusable Calculations
We call these things “functions”
Deceptively Complex
Solution:
Abstractions &
Calculations
Derive values during compilation
Create a simple abstraction
As complex as it actually is.
Best Practice:
Minimize HTTP Round Trips
Problem:
Long Stylesheets
Hard to find your styles.
And I mean long...




                     Seriously, this keeps going...
Solution:
Concatenation &
Partials
Partials: Just like with html templates, underscores denote a
partial and can only be imported by other stylesheets.
Convention: Each folder has a file of the same name that
imports the files contained within it in the correct order.
Problem:
Shared Stylesheets
Styles leak into places you didn’t intend
Sitewide Styles


    Forum        Blog       Articles    Directory


I    T   F   L    P     F   I   A   C   S   L   R
n    h   o   i    o     o   n   r   o   e   i   e
d    r   r   s    s     r   d   t   m   a   s   v
e    e   m   t    t     m   e   i   m   r   t   i
x    a                      x   c   e   c   i   e
     d                          l   n   h   n   w
                                e   t       g
Solution:
Scoped Selectors
CSS gives you
carpal tunnel
from typing
your scopes
OVER &
OVER AGAIN
Sass Provides
DRY Selectors
via nesting
Nested Selectors
• Default: descendant
• & - context reference
• Works with combinators




Co
 mp
  ile
     st
       o
Problem:
Dozens of Images
Spriting is annoying
Solution:
Compass Sprites
gem “compass”




Simple Sprites
(Compass makes
classes for you)


                   Or you can define your
                   own selectors.
Problem:
Dozens More Images
Solution:
Use CSS3 Instead
Abstract vendor prefixes away
gem “compass”
Best Practice:
Use a CDN / Asset Hosts
Load assets faster
Problem:
Image URLs Change
Solution:
Use URL Helpers
Just like in Rails
o
        st
     ile
  mp
Co
Best Practice:
Separate Content & Presentation
Problem:
Classitis
Solution:
Move Presentation Concerns
They belong in your stylesheets
This is all you should need.
o
            st
         ile
      mp
    Co
+




                   Mixins
Mixins
Create a design vocabulary
of common patterns independent
from your markup's selector
domain
More Mixins




       +
        Compiles to
More Mixins



       +

        Compiles to
More Mixins




       +
           Compiles to
This is all you should need.
Compiles to




Selector Inheritance
We’ve Only
Scratched the Surface
http://sass-lang.com
http://compass-style.org
My Teammates

Lead Developer of Sass
Nathan Wiezenbaum (@nex3)

The Compass Core Team
Eric Meyer (@eriiicam)
Brandon Mathis (@imathis)
Scott Davis (@jetviper21)
Nico Hagenburger (@hagenburger)
Compass is Charityware


If you use Compass and it makes your life better, please help
 make someone else’s life better by making a tax-deductible
   donation to the United Mitochondrial Disease Foundation.
                    http://umdf.org/compass

Sass: The Future of Stylesheets

Editor's Notes

  • #2 \n
  • #3 \n
  • #4 \n
  • #5 \n
  • #6 \n
  • #7 \n
  • #8 \n
  • #9 \n
  • #10 \n
  • #11  Yes, it’s an acronym.\n But, it’s *not* all caps.\n Why? I don’t know. \n Alternate Syntax\nCompiles to CSS\nTo understand Sass, you must know CSS\n \n
  • #12 \n
  • #13 CSS Selectors are great -- that’s why we use them in jQuery & Haml\nProperties are simple key/value pairs.\nCSS is elegantly simple.\n
  • #14 Maybe my memory doesn’t go back very far.\n\nI’m sure punch cards and assembler sucked.\n
  • #15 9 Browsers\n25 Versions\nPace of development is increasing\n
  • #16 \n
  • #17 \n
  • #18 \n
  • #19 \n
  • #20 \n
  • #21 \n
  • #22 \n
  • #23 CSS is a major front for the Browser War v2.0\nCasualty of War: Front-End Developers\nWhat do all of these new features have in common?\nThey’re all about the browser -- None of them address how we author stylesheets. \n
  • #24 \n
  • #25 \n
  • #26 \n
  • #27 I think rails is a great example of this. Ruby is incredibly powerful, that power makes the best practices of web programming accessible and scaffolds the n00bs during the learning process as they peel the layers of abstraction back.\n
  • #28 \n
  • #29 What if we gave stylesheets some brains?\n
  • #30 \n
  • #31 Alternate Syntax\nCompiles to CSS\nWe’ll get to these primitives in a second\n\n
  • #32 But: Sass in the hands in of a technical designer is a BEAUTIFUL THING\n
  • #33 \n
  • #34 \n
  • #35 \n
  • #36 \n
  • #37 \n
  • #38 \n
  • #39 Accidentally the same?\n
  • #40 You cannot safely Search and Replace a color.\n
  • #41 \n
  • #42 \n
  • #43 \n
  • #44 \n
  • #45 where do these colors come from? Clearly they’re related -- must be designer voodoo.\n
  • #46 \n
  • #47 \n
  • #48 \n
  • #49 \n
  • #50 \n
  • #51 Why is it 366px? If I want to change the border width, how should I adjust these values?\n
  • #52 \n
  • #53 Now our changes are obvious and the implementation details elided.\n
  • #54 Currently most of these calculations are done on a scratch pad.\n\nseriously.\n
  • #55  \n
  • #56  Don’t use CSS @import.\n Serve a few, longer stylesheets.\n
  • #57 \n
  • #58 \n
  • #59 \n
  • #60 \n
  • #61 \n
  • #62 \n
  • #63 Notice that comma selectors are permuted\n
  • #64 \n
  • #65 \n
  • #66 \n
  • #67 \n
  • #68 \n
  • #69 \n
  • #70 \n
  • #71 \n
  • #72 \n
  • #73 Compass CSS3 mixin arguments mimic the real css3 property values.\n
  • #74 \n
  • #75 \n
  • #76 relative paths\nroot relative paths\nroot changes\nasset hosts\ncdns\n\n\n
  • #77 \n
  • #78 \n
  • #79 \n
  • #80 Do you have markup that looks like this?\n
  • #81 You’re officially doing it wrong.\n\nBut I get it. You need re-use.\n
  • #82 \n
  • #83 \n
  • #84 \n
  • #85 \n
  • #86 \n
  • #87 \n
  • #88 \n
  • #89 \n
  • #90 \n
  • #91 \n
  • #92 \n
  • #93 \n