11/13/2018

SASS

How CSS should behave.

Sass... SCSS... whatever

I'll say Sass, but I'm talking about SCSS. `.sass` is an older style CSS preprocessor. `.scss` is the current file extension. I'll call it Sass, documentation will call it Sass, and all the file extensions are .scss

SASS Benefits

  • Variables
  • Nesting
  • Partials
  • Imports
  • Mixins

SASS API

SASS Variables

Allow you to set a value in one spot and use it throughout all of your files. Variables are auto detected and must start with $.

Source:


        $theme: #f00;

        a{
            color:$theme;
            border:1px solid $theme;
        }
        
        

Output:


        a{
            color:#f00;
            border:1px solid #f00;
        }
        

Nesting

Nest allows you to group your CSS and create a more specific CSS selector.

Source:


$theme: #f00;
.container {
    a{
        color:$theme;
        border:1px solid $theme;
    }
    p{
        color:black;
    }
}
        

Output:


    .container a{
        color:#f00;
        border:1px solid #f00;
    }
    .container p{
        color:black;
    }
        

Use nesting with caution

Source:


.container {
    .element{
        a {
            .icon{
                color:blue;
            }
            .text{
                color: black;
            }
        }
    }
}
        

Output:


.container .element a .icon { 
    color:blue;
}
.container .element a .text { 
    color:black;
}
        

Partials

Partials contain small blocks of SCSS that can be used to manage specific components of your website. Partials typically start with an underscore.

Examples

_reset.scss


* {
    box-sizing: border-box;
}
h1,h2,h3, p{
    margin:0;
    padding:0;
}
   
        

When would we use this? How about when we import a .css file based on screen size.

Examples

_theme.scss


    $brandcolor: #f00;
    $brandsecondary: #a00;
    $spacing: 15px;
    

Examples

_navigation.scss


   .navigation {
       display:flex;

        nav a{
            color:$brandcolor;
            margin:$spacing;
        }
   }
   

Import

SASS's import functionality allows you to combine multiple SCSS files into one.


    @import 'reset';
    @import 'theme';
    @import 'navigation';
    @import 'footer';
    @import 'page1';
        

Mixins

Mixins allow you to write functions inside you SCSS and minimize your code footprint.

Example:

See the Pen UA-SCSS--MIXIN1 by Jon (@Middaugh) on CodePen.

Example 2:

See the Pen UA-SCSS--MIXIN2 by Jon (@Middaugh) on CodePen.

Extend

Extend allows you to share common CSS accross multiple classes.

Extend Example

See the Pen UA-SCSS--MIXIN3 by Jon (@Middaugh) on CodePen.

Operators

We can do math calculations inside of SASS. You can use the +, -, *, /, and % operators like you would in JavaScript. SASS will automatically work with px and % values.

Operators Example

See the Pen UA-SCSS--Operators by Jon (@Middaugh) on CodePen.

Installation

Windows Users: Get Ruby

rubyinstaller.org

Mac Users: You already have Ruby

Open a command prompt after installing and type the following.


    gem -v
        

Installation Continued

SASS/SCSS installation

http://sass-lang.com/install


    gem install sass
        
or

    sudo gem install sass
        
Then run to verify

    sass -v
        

Gulp SCSS Usage


        npm install gulp-sass --save-dev
    

Gulp SASS Code


    gulp.task('sass', function () {
        return gulp.src('./sass/**/*.scss')
            .pipe(sass())
            .pipe(gulp.dest('./css'));
    });

Questions?

Sass continued

Referencing a parent selector

You can reference a nested selectors parent by using the & symbol.

Simple Parent Reference

See the Pen UAIT-SCSS-Nesting by Jon (@Middaugh) on CodePen.

Examine this in Chrome Dev Tools to see what's up.

And maybe read this too!

Variable Expansion in Selectors

You can use Sass variables for more than just values. You can use them in conjunction with selectors.

They must be used inside of a #{}

See the Pen UAIT-SCSS-Expanding Class by Jon (@Middaugh) on CodePen.

Using Parent for repetitive names.

See the Pen UAIT-SCSS-Nesting-Deep by Jon (@Middaugh) on CodePen.

Using Parent to Reference the Root

Using .class &{ will put .class before all other selectors

See the Pen UAIT-SCSS-Nesting-Parent by Jon (@Middaugh) on CodePen.

@at-root

@at-root allows you to nest inside css for structure, but places the rules in the root of the css.

You can use it as an inline rule or as a wrapping rule.

See the Pen UAIT-SCSS-Nesting-Root by Jon (@Middaugh) on CodePen.

@if

SCSS has the ability to use if, else if, and else statements. These can be used to theme your styles or setup conditional formatting.

See the Pen UAIT-SCSS-Nesting-Root by Jon (@Middaugh) on CodePen.

@for

You can use SCSS to execute a for loop based on indexes. This is useful for generating grids and removing duplicate code.

See the Pen UAIT-SCSS-Nesting-For by Jon (@Middaugh) on CodePen.

@each

@each is used for looping through a defined collection of items.

See the Pen UAIT-SCSS-Nesting-Each by Jon (@Middaugh) on CodePen.

Additional Methods

  • darken/lighten
  • quote/unquote
  • percentage
  • to-upper-case/to-lower-case

Darken/Lighten

See the Pen UAIT-SCSS-Darken by Jon (@Middaugh) on CodePen.

Facebook Integration

Documentation

Link Sharing

You can customize the previews of your links being shared on Facebook by leveraging OG meta tags.

Documentation

OG Tags

OG tags are meta tags that tell Facebook what to display when sharing.

The Basic Tags

  • og:url - The URL to the page.
  • og:title - The title of the page.
  • og:description
  • og:image - Preview image

In Markup


    
    
    
    
    
            

    

Testing

Facebook Debug Tool

Adding a Like Button

Like Button Generator

Step 1: Including the Facebook SDK

This needs to be included right next to the opening body tag.


Step 2: Adding the Markup


  

Facebook Share Button

Share Button Generator

Step 1: Add the Facebook SDK.

But only if you haven't added it yet.

Step 2: Add the Markup


  

Don't head out until I see progress on your business sites.

Exercise 26

>