Skip to content

Instantly share code, notes, and snippets.

@yinonc
Last active May 11, 2019 15:54
Show Gist options
  • Save yinonc/a24787c21774918bdc498e4da14965a9 to your computer and use it in GitHub Desktop.
Save yinonc/a24787c21774918bdc498e4da14965a9 to your computer and use it in GitHub Desktop.
SASS basics

SASS

  1. Introduction
  2. Installation
  3. variables
  4. Nesting
  5. Partials
  6. Mixins
  7. Extend
  8. Referencing
  9. Math Operators

Extension for CSS.

First, install sass in your repo:

npm i --save-dev sass

Compile your scss files with:

sass src/main.scss dist/style.css

Optional: Use sass-lint in your project:

npm i --save-dev sass-lint

Run sass-lint with:

sass-lint 'src/**/*.scss' -v -q

You can use either the default config, or .sasslintrc file with your rules specifications. More details in sass-lint repo.

The first feature of SASS is using variables, declared by $:

$offwhite: #EEE8D6;
$darkblue: #022933;
$font-main: helvetiva, 'bree Serif', sans-serif;
body {
  color: $darkblue;
  background-color: $offwhite;
  font-family: $font-main;
}

Variables also can be set to other variables value:

$offwhite: #EEE8D6;
$main-color: $offwhite;

Nesting helps us to apply rules for specific id or classname.

.media {
  margin: 0;
  padding: 0;
  .item {
    margin-bottom: 20px;
  }

  p {
    font-size: 16px;
  }
}

Only .item classes and <p> elements under .media classname would accept the rules above.

Variables also can be set to other variables value:

$offwhite: #EEE8D6;
$main-color: $offwhite;

SASS allows you to break your css into modules called partials.
Usually your main.scss file would import all of the modules.
You simply add import command for another module:

// video.scss
.video {
  margin: 0;
  max-width: 500px;
}
// media.sass
@import "./video";
.media {
  margin: 0;
}
  • Most processors allows to import module without .scss suffix.

You can also import module from the web:

// media.sass
@import url(http://fonts.googleapis.com/css?family=Roboto+Slab|Merriweather);
.media {
  margin: 0;
}

SASS allows you to add JavaScript-like functions for rules called mixins. The mixin can accept arguments and also set a default value, if not passed:

// mixins.scss
@mixin backImage($image, $height: 200px) {
  background: url($image);
  background-repeat: no-repeat;
  background-position: center center;
  height: $height;
}
// media.sass
@import "./video";
.media {
  @include backImage('http://images.google.com/1.jpg', 600px);
  margin: 0;
}

SASS allows you to add extend rules for a class.
Basically it will take all the rules from the extended class and apply them into the other class:

// index.<!DOCTYPE html>
<button class=btn-first>First</button>
<button class=btn-second>Second</button>
<button class=btn-third>Third</button>
// index.sass
.btn {
  display: inline-block;
  padding: 6px 12px;
  text-align: center;
}
.btn-first {
  @extend .btn;
  background: red;
}
.btn-second {
  @extend .btn;
  background: blue;
}
.btn-third {
  @extend .btn;
  background: yellow;
}

SASS allows you to reference parent selector with &:

// index.sass
.btn {
  display: inline-block;

  &:hover {
    background: yellow;
  }

  // css has last-of-type keyword to apply rule for last selector in the html.
  &:last-of-type {
    margin-bottom: 10px;
  }

  // you can use the & as a nested selector, so this'll apply rules to:
  // .btn #my-id .btn
  #my-id & {
    color: $subColor;
  }
}

SASS allows you to use math operators for calculations:

// index.sass
.btn {
  width: ((100% - (($qty - 1) * $margin)) / $qty);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment