Intro to Sass

Class 2

Review: Mixins

If you haven't already, create these mixins under Utilities:

  • dropshadow/text-shadow style
  • clearfix
  • gradient
@mixin example($optionalArgument) {
  property: value;
  property: $optionalArgument;
}
p {
  @include example($optionalArgument);
}

Multiple Arguments

Mixins can take multiple arguments, as we saw with the gradient mixin:
@mixin gradient($color1, $color2) {
  background-image: -webkit-linear-gradient($color1, $color2, $color1);
  background-image: -moz-linear-gradient($color1, $color2, $color1);
  background-image: linear-gradient($color1, $color2, $color1);
}

Default Arguments

You can also specify *default* values for arguments, for next-level laziness (a good thing):
@mixin gradient($color1: #fff, $color2: #666) {
  background-image: -webkit-linear-gradient($color1, $color2, $color1);
  background-image: -moz-linear-gradient($color1, $color2, $color1);
  background-image: linear-gradient($color1, $color2, $color1);
}

Interpolation

With some styles, we may want to use variables right next to text, like with our image rotate style - we'll try it under Utilities:
@mixin rotate($degree, $position) {
  -webkit-transform: rotate(-5deg);
  -moz-transform: rotate(-5deg);
  transform: rotate(-5deg);
  -webkit-transform-origin: $position;
  -moz-transform-origin: $position;
  transform-origin: $position;
}

Interpolation

We do this by using interpolations, which is a special syntax for variables that appear inside regular CSS text:
@mixin rotate($degree, $position) {
  -webkit-transform: rotate(#{$degree}deg);
  -moz-transform: rotate(#{$degree}deg);
  transform: rotate(#{$degree}deg);
  -webkit-transform-origin: $position;
  -moz-transform-origin: $position;
  transform-origin: $position;
}

@extend

Sometimes we have styles that duplicate another class, then add to it. We can use a mixin for these, OR we can use @extend:
.headline {
  font-size: 2em;
  font-weight: bold;
}
.lead-story-headline {
  @extend .headline;
  text-decoration: underline;
  text-transform: uppercase;
}

Let's Develop It

  • Create new mixins using the following:
    • Multiple arguments
    • Default arguments
    • Interpolation
    • @extend to include common styles
  • Use these mix-ins in your styles
  • Toggle to CSS or take a look in Dev Tools

CSS3: Browser Prefixes

Lots of CSS3 features require browser prefixes or have complex syntaxes. These are perfectly suited to mixins, so we can include them in our styles without having to copy and paste from sites like CSS3 Please.
@mixin rotate($degree, $position) {
  -webkit-transform: rotate(#{$degree}deg);
  -moz-transform: rotate(#{$degree}deg);
  transform: rotate(#{$degree}deg);
  -webkit-transform-origin: $position;
  -moz-transform-origin: $position;
  transform-origin: $position;
}

CSS3: Browser Prefixes

Gradients are CSS3 and require browser prefixes to work in Firefox, Chrome, Safari, & Internet Explorer.
@mixin gradient($color1: $bodyBackground, $color2: $accentBackground) {
  background-image: -webkit-linear-gradient($color1, $color2, $color1);
  background-image: -moz-linear-gradient($color1, $color2, $color1);
  background-image: linear-gradient($color1, $color2, $color1);
}
@include gradient(#fff, #000);

CSS3 Mixins

Other CSS3 features that require browser prefixes to work in most popular browsers include:

CSS3 Mixins

Other common UI elements that Sass mixins make easy:

Let's Develop It

  • Create new mixins for properties that require prefixes, combining as many as possible.
  • Reference http://shouldiprefix.com/
  • Take a look at the compiled CSS to check your changes

Break Time!

Stand up and stretch - we'll resume in 5 minutes

If/Else logic

Sass has logic statements that you can use to create conditionals. They are @if, @else if, and @else

@mixin opacity($value: 0.5) {
  @if $value == transparent {
    opacity: 0;
  } @else if $value == opaque {
    opacity: 1;
  } @else {
    opacity: $value;
  }
}
@include opacity(transparent);

If/Else If

Rewrite your existing mixins to use if/else if statements, so that they output different CSS depending on different arguments.

@mixin arrow($direction: right) {
  @if $direction == right {
    //right arrow styles
  }
  @else if $direction == left {
    // left arrow styles
  }
}

For Loops

With @for loops, you can make Sass write your classes and styles for you.

Sass code:

@for $i from 1 through 3 {
  .column-#{$i} { width: 2em * $i; }
}

CSS output:

.column-1 {
  width: 2em;
}
.column-2 {
  width: 4em;
}
.column-3 {
  width: 6em;
}

For Loops

  • Use loops to write classes and styles for you! Use them to make columns that fit within our layout, and add links to our footer nav in these columns.
  • Experiment with math in your loop's styles - change the width, padding, even font-size
@for $i from 1 through 3 {
  .column-#{$i} { width: 2em * $i; }
}

@each

With @each, you can loop through a list of items and create styles for each item in the list.

Sass code:

@each $icon in youtube, twitter, facebook {
  .icon-$icon {
    background-image: url('#{$icon}.png');
  }
}

CSS output:

.icon-youtube {
  background: url('youtube.png');
}
.icon-twitter {
  background: url('twitter.png');
}
.icon-facebook {
  background: url('facebook.png');
}

@each

  • Create background image styles for each Famous Woman article
  • Try SubtlePatterns.com for images to use
  • Write these background styles with @each and a list
  • Bonus: style your social media icons using @each and @if logic
@each $woman in ada, grace, frances, barbara, anita, maria {
  .#{$woman}-bg {
    background-image: url('images/#{$woman}.png');
  }
}

Extend Sass

The following tools can help you write Sass even faster:

More to Explore

  • Haml: like Sass for HTML
  • Rails: Rails is a software development framework written in Ruby. Sass & Haml are used in Rails apps. Try the Rails Girls tutorials to build your own app!
  • LESS: a CSS preprocessor like Sass with different syntax

Questions?

Intro to Sass

Extended Lesson: Installing Ruby and Sass Locally

Sass and CSS

How Sass compiles into CSS

1. You write Sass (.scss files)
2 .You run a command (sass --watch) on the command line
3. .scss files compile into .css files

Command Line Tips

Up one level:

        $ cd ../
                    
Go to your home directory:

        $ cd /
                    
Go to a specific folder:

        $ cd Users/cfarman/Sites/gdi-sass
                    
List files in a directory:

        $ ls
                    
Command Line Cheat Sheet!!! (In class1-exercises folder)

Installing Sass

First, we need to install Ruby:
  • Download for Windows: rubyinstaller.org
  • Mac users: you're in luck! Mac OS X comes pre-installed with Ruby. Try the following command in Terminal to make sure:
ruby -v

Installing Sass

Now that Ruby is installed, we can install the Sass gem.

In Terminal or Git Bash:

gem install sass

Sass Set Up

You should have installed Sass on your computer prior to this workshop.

Check if Sass is installed:


        gem list sass
                  

You should see something like this:


        # *** LOCAL GEMS ***
        # sass (3.4.23)
                   

Raise your hand if Sass is not installed.

Setting up our stylesheets

We need to structure our stylesheets before we can compile them.

Setting up our stylesheets

  • rename CSS files to have a .scss file extension
  • structure them inside the stylesheets folder like so:
    • /stylesheets/
      • /css/
        • empty for now
      • /scss/
        • /font/
        • reset.scss
        • styles.scss
  • Update your index.html stylesheet url in <head> to point to your /css/ folder.

Compile with the Sass Watch Command

Check out your index.html file in a browser. Looks funky, yes?

We need to compile our .scss (Sass) files to make the CSS work in the browser.

First, navigate via the command line to your /stylesheets directory in the "practice" folder.

Then type:

$ sass --watch scss:css

Compile with the Sass Watch Command

Let's break this command down - you're going to use it *a lot*!

$ sass --watch scss:css

--watch tells Sass to look for changes to our .scss files, and to compile them to css if they have updates.

The scss bit is the folder where our .scss files live. We edit these files only.

The css part is the folder where our .css files will be. These files are the compiled output of our Sass, and are only created when we run the above command.

Try it Out!

Edit some Sass in the .scss files and see your changes automatically compiled to CSS!

Questions?