getting sassy with css

Post on 27-Jan-2015

137 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

I don't know about you, but vanilla CSS always leaves me wanting more. It's a fairly simple language, can do some pretty powerful things, but after a while.. well, it can become rather tedious to work with. Enter Sass: Syntactically Awesome Stylesheets. Sass is a CSS meta-language and precompiler that makes the prettification of your website a breeze by adding invaluable features of more traditional programming languages. In this session, we'll jump right in to the Sass workflow and cover all the key game changers - nesting, variables, mixins, inheritance, and directives. We'll also cover some of the gotchyas, tools and extensions, and tips for organization and coding standards. Prepared for the self.conference at COBO Hall in Detroit, Michigan on May 30, 2014

TRANSCRIPT

Getting Sassy with CSS

Julie Cameron @jewlofthelotus

CodeMash 2015 bit.ly/getting-sassy

DERP.

I <3 PAIR PROGRAMMING

Syntactically Awesome Stylesheets

NOT an acronym…

Syntactically Awesome Stylesheets

“If you see something, say something. Only you can prevent overcapitalization.”

Hampton Catlin, Creator Of Sass

This is a public service announcement…

Just don’t be a

#Sasshole about it. :)

What Is Sass?

What Is Sass?

1. An extension of CSS SassScript Language. Fully CSS-compatible syntax.

What Is Sass?

1. An extension of CSS SassScript Language. Fully CSS-compatible syntax.

2. A CSS preprocessor SassScript MAGIC CSS

Where Did Sass Come From?

• 2007: Designed by Hampton Catlin (Haml)

• Developed by Chris Eppstein (Compass) and Natalie Weizenbaum

Why Did They Make Sass?

Why Did They Make Sass?

Because vanilla CSS just wasn’t cutting it any more.

What Does Sass Give Us That CSS Doesn’t?

What Does Sass Give Us That CSS Doesn’t?

• Nesting

What Does Sass Give Us That CSS Doesn’t?

• Nesting

• Imports

What Does Sass Give Us That CSS Doesn’t?

• Nesting

• Imports

• Variables

What Does Sass Give Us That CSS Doesn’t?

• Nesting

• Imports

• Variables

• Mixins

What Does Sass Give Us That CSS Doesn’t?

• Nesting

• Imports

• Variables

• Mixins

• Inheritance

What Does Sass Give Us That CSS Doesn’t?

• Nesting

• Imports

• Variables

• Mixins

• Inheritance

• Functions

What Does Sass Give Us That CSS Doesn’t?

• Nesting

• Imports

• Variables

• Mixins

• Inheritance

• Functions

• Loops

What Does Sass Give Us That CSS Doesn’t?

• Nesting

• Imports

• Variables

• Mixins

• Inheritance

• Functions

• Loops

• AWESOME

What Does Sass Give Us That CSS Doesn’t?

What Does Sass Give Us That CSS Doesn’t?

• Reduced Repetition

What Does Sass Give Us That CSS Doesn’t?

• Reduced Repetition

• Cleaner Modularity

What Does Sass Give Us That CSS Doesn’t?

• Reduced Repetition

• Cleaner Modularity

• Maintainability

What Does Sass Give Us That CSS Doesn’t?

• Reduced Repetition

• Cleaner Modularity

• Maintainability

• Scalability

What Does Sass Give Us That CSS Doesn’t?

• Reduced Repetition

• Cleaner Modularity

• Maintainability

• Scalability

• REAL PROGRAMMING!

Setup & Workflow

Sass Installation

Ruby OS X? Lucky you, it’s preinstalled!

Windows? Try RubyInstaller.

Sass $ gem install sass

Compiling Sass

Sass CSS

$ sass source.sass output.css

$ sass source_dir output_dir

Compiling Sass

Sass CSS

$ sass source.sass output.css

$ sass source_dir output_dir

MAGIC

Sass Is Watching You

$ sass —-watch source.sass:output.css

This is the best thing. Ever.

Gotchya: Colon vs. Space

Two Sass SyntaxesSassy CSS .scss

Default; Valid CSS == Valid SCSS

Indented .sass Original syntax; Haml-esque

The Good StuffOr, Why You’ll Never Write Plain Old CSS Again.

NestingHTML has a clear hierarchy - elements are nested.

We can apply the same concept in Sass.

HTML Sass

Nesting Output

MAGIC

Sass CSS

Nesting Gotchya

Mirroring HTML nesting is SUPER easy.

MA

GIC

?

Sass

CSS

Nesting Gotchya

Mirroring HTML nesting is SUPER easy.

MA

GIC

?

Overly verbose and overly specific.

Sass

CSS

Nesting Gotchya

Mirroring HTML nesting is SUPER easy.

MA

GIC

?

Overly verbose and overly specific.

Rule of thumb: No more than 3 levels deep

Sass

CSS

Nesting The Parent Selector

You can reference parent selector(s) with an &

MAGIC

Sass CSS

Nesting The Parent Selector

The & can follow other selectors, too!

MAGIC

Sass

CSS

Sass

Nesting The Parent Selector

As of Sass 3.3, you can also append a suffix to the &!

MA

GIC

CSS

Nesting Media Queries

MAGIC

THIS IS FREAKING AWESOME.

Sass CSS

Note: Like media queries will not group together in the output.

Imports

CSS @import has always meant an extra file download.

Sass modifies @import to instead include the resource during compilation, rather than on the client side.

Sass (application.scss)

Imports File Structure• project_awesome

• sass

• lib

• compass.scss

• fontawesome.scss

• base.scss

• grid.scss

• application.scss

• css

• application.css

$ sass sass/application.scss css/application.css

Sass

Imports File Structure

• project_awesome

• sass

• lib

• compass.scss

• fontawesome.scss

• base.scss

• grid.scss

• application.scss

$ sass sass/ css/

• project_awesome

• css

• lib

• compass.css

• fontawesome.css

• base.css

• grid.css

• application.css

MAGIC

MAGIC

Import Partials

• project_awesome

• sass

• lib

• _compass.scss

• _fontawesome.scss

• _base.scss

• _grid.scss

• application.scss

• print.css

$ sass sass/ css/

• project_awesome

• sass

• …

• css

• application.css

• print.css

Variables

WTF is #531946?

#30162b? #095179?

WAT?

Variables

Variables let you reuse single values.

Sass

Variable Usescolors

font sizes

font families

font paths

padding

margins

breakpoints

border-radius

pseudo content

shadows

gradients

SELECTORS!

LOGIC!

ALL THE THINGS!!!1!

Variable Data TypesNumbers

Strings

Colors

Lists

Booleans

Null

Maps

Sass

Variable Dynamics

• Mutable.

• Can have default values.

• Can be interpolated.

• Have scopes.

Sass

Math

• + addition

• - subtraction

• * multiplication

• / division

• % modulus

Sass lets us use basic math operations.

Sass

CodeMash

Math Functions

• abs($num) - absolute value

• ceil($num) - round up to closest whole number

• floor($num) - round down to closest whole number

• percentage($num) - convert to percentage

• round($num) - round to closest whole number

• max($list) - maximum list value

• min($list) - minimum list value

Sass comes with a set of math functions.

Color MathWe can even use math to manipulate colors.

Sass

Color Functions

• rgba($hex, $alpha)

• lighten($color, $percent)

• darken($color, $percent)

• saturate($color, $percent)

• desaturate($color, $percent)

• mix($color1, $color2)

• grayscale($color)

• invert($color)

• complement($color)

• AND MANY MORE!

Sass comes with AWESOME color functions.

http://sassme.arc90.com

Mixins

Variables let you reuse single values

Mixins let you reuse blocks of styles

Mixins Use

CSS

Mixins Use

CSS Sass

Mixins output their contents where they are called.

Mixin Output

MAGIC

Mixin Features

• argument defaults

• multiple arguments

• optional arguments

CSS

Sass

Mixin Alternative?CSS

Mixin Alternative?Better?CSS

@extendLets you group selectors

with blocks of styles

MAGIC

Sass

CSS

PlaceholdersPlaceholders can be @extended but will never compile to the CSS on their own

MAGIC

Sass

CSS

Plus Lots More WIN

Functions

Conditions

Each Loops

For Loops

While Loops

Maps

Key-value pairs.

Multiple assignment in loops.

Lots of helper methods.

Demo Timehttps://github.com/roytomeij/sassconf

Resources

Sass-Lang.com

The Sass Way

Sass For Web Designers

Assembling Sass

SassNews Newsletter

SassConf

SassConf 2013 Videos

SassMeister

Sass Style Guide

#teamSass

Closing Tips• Take it one step at a time.

• Check your output.

• Don’t nest too deeply.

• Mind your @mixins and @media queries.

• Don’t @extend too much.

• Modularize all the things.

Questions?

Getting Sassy with CSS

Julie Cameron @jewlofthelotus

CodeMash 2015 bit.ly/getting-sassy

top related