Girl Develop It

Intro to Sass

Class 2

Download Class 2 Files:

http://gdiseattle.github.io/gdi-sass/class2-exercises.zip

http://codepen.io/collection/XgYvdj/

Review: Watching for Changes

First, navigate via the command line to your /class2-exercises/practice/stylesheets directory.

Then type:

$ sass --watch scss:css

Review: Mixins

@mixin yourMixinName($optionalArgument) {
  // hard-code values
  property: value;

  // or use arguments
  property: $optionalArgument;
}

p {
  // example usage
  @include yourMixinName($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 - try it in _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;
}

Let's Develop It

Create some mixins!

Where should you define the mixin?

For example:

  • dropshadow
    @mixin dropshadow($text) {
      color: $text;
      text-shadow: 2px 4px lighten($text, 50%);
    }
    p {
      @include dropshadow(black);
    }

Think about using:

  • Multiple arguments
  • Default arguments
  • Interpolation

Extends

Sometimes we have styles that duplicate another class. We can use the extend prefix (%) for these:

@mixin clearfix {
  content: "";
  display: table;
  clear: both;
}
&:after {
  @include clearfix;
}

@extend classes

After extending a class, we can add to it:

.headline {
  font-size: 2em;
  font-weight: bold;
}
.lead-story-headline {
  @extend .headline;
  text-decoration: underline;
  text-transform: uppercase;
}

Let's Develop It

  • Create some extends! For example:
    @mixin clearfix {
      content: "";
      display: table;
      clear: both;
    }
    &:after {
      @include clearfix;
    }

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:

Mixins for UI Elements

Mixins are very useful for common design patterns. A great example is the CSS sprite design pattern.

Here's YouTube's icon sprite:

Sprite image:

CSS for icon:

.icon {
display: block;
background: no-repeat url(img/youtube_sprite.png) -395px -114px;
background-size: auto;
width: 18px;
height: 18px; }

Sprite Mixins

If you set up your Photoshop sprite to use grid lines, you can use Sass to easily position your sprite image without lots of trial and error.

@mixin sprite-position($x:0, $y:0) {
  $gridSize:  -32px;
  $offsetX:   $x * $gridSize;
  $offsetY:   $y * $gridSize;
  background-position: $offsetX $offsetY;
}
@mixin sprite-image($file, $x:0, $y:0) {
  background-image: url("../../img/icons/#{$file}");
  @include sprite-position($x, $y);
}

Or use Compass!

CSS3 Mixins

Other common UI elements that Sass mixins make easy:

Let's Develop It

  • Create more mixins and or extends. Examples:
    • @font-face
    • transitions
    • CSS shapes
    • CSS3 box-shadow
    • Opacity for all browsers, including IE

Break Time!

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

If/Else If

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

@mixin arrow($direction: right) {
  @if $direction == right {
    //right arrow styles
  }
  @else if $direction == left {
    // left arrow styles
  }
}
h1 {
  padding: 1rem;
  background-color: pink;
  @include arrow(left);
}

http://codepen.io/KatieK2/pen/GJVKRo

If/Else If

You can incorporate this into your existing mixins to use if/else if statements, so that they output different CSS depending on different arguments.

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

h1 {
  @include opacity(transparent);
}

http://codepen.io/KatieK2/pen/jPjggG

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;
}

http://codepen.io/KatieK2/pen/waVwBJ

For Loops

  • Use loops to write classes and styles for you! Use them to make columns that fit within in 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

@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');
}

Let's Develop It! @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('img/#{$woman}.png');
  }
}

Extend Sass

The following tools can help you write Sass even faster:

  • CodeKit: Compile Sass & more without command line
  • Sass Sleuth: See Sass line numbers in your browser
  • Live Reload: Compile Sass without refreshing your browser
  • Compass: Lots of mixins and advanced functionality (Ruby only)
  • Sassmeister: Quickly test Sass. Lots of mixin options.

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?

?