SASS commands

SCSS is a special type of file for SASS (syntactically awesome style sheets)
Sass allows variables to be defined. Variables begin with a dollar sign ($). Variable assignment is done with a colon (:)
Sass Script supports four data types :
  • Numbers (including units)
  • Strings (with quotes or without)
  • Colors (name, or names)
  • Boolean
Don’t be confused by the SASS and SCSS. for eg:
  1. /* SCSS */
  2. $red: #f00;
  3. $padding: 10px;
  4. .content{
  5. border-color: $red;
  6. color: darken($red, 9%);
  7. }
  8. .border {
  9. padding: $padding / 2;
  10. margin: $padding / 2;
  11. border: 2px solid $red;
  12. }
  1. /* SASS */
  2. $red: #f00
  3. $padding: 10px
  4. .content
  5. border-color: $red
  6. color: darken($red, 9%)
  7. .border
  8. padding: $padding / 2
  9. margin: $padding / 2
  10. border: 2px solid $red
After Compilation result would be:
  1. /* CSS */
  2. .content{
  3. border-color: #f00;
  4. color: #f00;
  5. }
  6. .border {
  7. padding: 5px;
  8. margin: 5px;
  9. border: 2px solid #f00;
  10. }

Let start sass/scss step by step:

There are many ways to use SASS, By RUBY, Node.js
First start with node, If ruby not install in your computer system the first install ruby package manager,
  1. /* install */
  2. gem install rail
  3. /* check sass version */
  4. sass -v
  5. /* run/ watch sass code. compile code scss file to css */
  6. sass --watch sass/main.scss:assets/css/main.css

or

second start with node, If node.js not install in your computer system the first install node.js package manager
then open cmd promt(window user) or terminal(ubuntu or mac user) then goto directory
  1. $ npm init
  2. /*for npm initiate*/
this command create a file package.json
  1. {
  2. "name": "sassTute",
  3. "version": "1.0.0",
  4. "description": "this is for SASS practice",
  5. "main": "index.js",
  6. "scripts": {
  7. "compile-sass": "node-sass sass/main.scss css/style.css -w"
  8. },
  9. "author": "shan",
  10. "license": "ISC",
  11. "devDependencies": {
  12. "node-sass": "^4.12.0"
  13. }
  14. }
  15. /* you can fill information or skip it pressing enter */
Now go to cmd promt or teminal and run below command
  1. $ npm install node-sass
  2. /* this install node packages */
now compile command
  1. $ npm run compile-sass
  2. /* compile-sass (image line no. 7) compile all sass/scss to css file */
SASS/SCSS have many interesting features:
  • Variables
  • Mixin
  • Nesting
  • Modules
  • Operators
  • Loops

Variables

variables as a way to store information that you want to reuse throughout your stylesheet. Sass uses the $ symbol to make something a variable. for example:
  1. /* SASS */
  2. $font-roboto: Roboto;
  3. $font-helvita: Helvetica, sans-serif;
  4. $light-color: #333;
  5. $dark-color: #111;
  6. h1{
  7. font: 100% $font-roboto;
  8. color: $dark-color;
  9. }
  10. h2{
  11. font: 100% $font-helvita;
  12. color: $light-color;
  13. }
  1. /* css */
  2. h1{
  3. font: 100% Roboto;
  4. color: #111;}
  5. h2{
  6. font: 100% Helvetica, sans-serif;
  7. color: #333;}

Mixin

mixin is another powerful feature, sometime css is common in some tags/classes, then we use mixin. A mixin lets you make groups of CSS declarations that you want to reuse throughout your website. for eg:
  1. /*scss */
  2. $fontcolor: #f00;
  3. @mixin transform($property) {
  4. -webkit-transform: $property;
  5. -ms-transform: $property;
  6. transform: $property;
  7. }
  8. @mixin propertyname{
  9. color: $fontcolor;
  10. font-weight: bold;
  11. }
  12. .box {
  13. @include transform(rotate(30deg)); }
  14. h1{
  15. @include propertyname;
  16. }
  17. p{
  18. @include propertyname;
  19. text-align: center;
  20. }
  21. a{
  22. @include propertyname;
  23. text-decoration: none;
  24. }
  1. /* css */
  2. .box {
  3. -webkit-transform: rotate(30deg);
  4. -ms-transform: rotate(30deg);
  5. transform: rotate(30deg);
  6. }
  7. h1 {
  8. color: #0a0;
  9. font-weight: bold; }
  10. p {
  11. color: #f00;
  12. font-size: 21px;
  13. text-align: center;}
  14. a{
  15. color: #f00;
  16. font-size: 21px;
  17. text-decoration: none;}

Nesting

SASS provide Nesting feature, But be-aware that overly nested rules
  1. /* scss */
  2. $small: 12px;
  3. $font-color: #f00;
  4. #service {
  5. padding: 20px 10px;
  6. .h2{
  7. font-size: $small;
  8. color: $font-color;
  9. span{ font-weight: bold; }
  10. }
  11. }
  1. /* css */
  2. #service{
  3. padding: 20px 10px;}
  4. #service h2{
  5. font-size: 12px;
  6. color: #f00;}
  7. $service h2 span{
  8. font-weight: bold;}

Modules

You can split your code by using modules, modules use with the @use rule. This rule loads another Sass file as a module, which means you can refer to its variables, mixins, and functions in your Sass file with a namespace based on the filename.

  1. // _inner.scss
  2. $font-stack: Helvetica, sans-serif;
  3. $primary-color: #333;
  4. body {
  5. font: 100% $font-stack;
  6. color: $primary-color;
  7. }
  8. // styles.scss
  9. @use 'base';
  10. .inverse {
  11. background-color: base.$primary-color;
  12. color: white;
  13. }
  1. /* css */
  2. body {
  3. font: 100% Helvetica, sans-serif;
  4. color: #333;
  5. }
  6. .inverse {
  7. background-color: #333;
  8. color: white;
  9. }

Operators

Math in your CSS is very helpful. Sass has a handful of standard math operators like +-*/, and %.
  1. /* sass */
  2. .container {
  3. width: 100%;
  4. }
  5. article[role="main"] {
  6. float: left;
  7. width: 600px / 960px * 100%;
  8. }
  9. aside[role="complementary"] {
  10. float: right;
  11. width: 300px / 960px * 100%;
  12. }
  1. /* css */
  2. .container {
  3. width: 100%;
  4. }
  5. article[role="main"] {
  6. float: left;
  7. width: 62.5%;
  8. }
  9. aside[role="complementary"] {
  10. float: right;
  11. width: 31.25%;
  12. }

Loops

Loops also a very time efficient, we can do some mathematical operation with loops create multiple dynamic classes
  • for loop
  • while loop
  • each loop
  1. // FOR loop Syntax
  2. @for $var from <start> through <end>
  3. // eg:
  4. @for $space from 1 through 12{
  5. .padding-top-#{$space}{
  6. }
  7. }
  1. //css
  2. .padding-top-1 {
  3. padding-top: 10px; }
  4. .padding-top-2 {
  5. padding-top: 20px; }
  6. .padding-top-3 {
  7. padding-top: 30px; }
  8. .padding-top-4 {
  9. padding-top: 40px; }
  10. .padding-top-5 {
  11. padding-top: 50px; }
  12. .padding-top-6 {
  13. padding-top: 60px; }
  14. .padding-top-7 {
  15. padding-top: 70px; }
  16. .padding-top-8 {
  17. padding-top: 80px; }
  18. .padding-top-9 {
  19. padding-top: 90px; }
  20. .padding-top-10 {
  21. padding-top: 100px; }
  22. .padding-top-11 {
  23. padding-top: 110px; }
  24. .padding-top-12 {
  25. padding-top: 120px; }
while loop example in sass
  1. // scss
  2. $num: 6;
  3. @while $num > 0
  4. {
  5. $width: percentage( 1 / $num );
  6. .column-#{$num}
  7. {
  8. width: $width;
  9. }
  10. $num: $num - 1;
  11. }
  1. // css
  2. .column-6 {
  3. width: 16.66667%; }
  4. .column-5 {
  5. width: 20%; }
  6. .column-4 {
  7. width: 25%; }
  8. .column-3 {
  9. width: 33.33333%; }
  10. .column-2 {
  11. width: 50%; }
  12. .column-1 {
  13. width: 100%; }
each loop in sass
  1. //scss
  2. @each key, value in array {}
  3. $white: #fff;
  4. $black: #000;
  5. $red: #f00;
  6. $green: #0f0;
  7. $colors: (
  8. 'white': $white,
  9. 'black': $black,
  10. 'red': $red,
  11. 'green': $green
  12. );
  13. @each $color, $hex in $colors{
  14. .text-#{$color}
  15. {
  16. color: $hex;
  17. }
  18. .bg-#{$color}
  19. {
  20. background-color: $hex;
  21. }
  22. }
  1. //css
  2. .text-white {
  3. color: #fff; }
  4. .bg-white {
  5. background-color: #fff; }
  6. .text-black {
  7. color: #000; }
  8. .bg-black {
  9. background-color: #000; }
  10. .text-red {
  11. color: #f00; }
  12. .bg-red {
  13. background-color: #f00; }
  14. .text-green {
  15. color: #0f0; }
  16. .bg-green {
  17. background-color: #0f0; }


Comments