An Introduction to Sass – Syntactically Awesome StyleSheets – Preprocessors



An Introduction to Sass – Syntactically Awesome StyleSheets – Preprocessors

0 0


Intro-to-Sass


On Github rmjames / Intro-to-Sass

An Introduction to Sass

Syntactically Awesome StyleSheets

Created by RobertJames / @bkrjames

Preprocessors

"A preprocessor is a program that processes its input data to produce output that is used as input to another program" - wikipedia

Sass is one of many CSS preprocessors .

CSS Processors

Less

Stylus

Sass

Pros

Modularize your project into multiple files called partials in developement environment.

									
										├── components
										│   ├── _buttons.scss
										│   ├── forms.scss
										│   └── _navigation.scss
										├── config
										│   ├── _extends.scss
										│   ├── _functions.scss
										│   ├── _mixins.scss
										│   └── _variables.scss
										├── layout
										│   ├── _footer.scss
										│   ├── _grids.scss
										│   ├── _header.scss
										│   └── _sidebar.scss
										└── main.scss
									
								

All import to one .scss file

Pros

Large Community of Libraries and Frameworks

Cons

Compilations of large files can sometimes be slow

However, when sass imports files it caches them, which greatly increases the speed

Variables

						
					  	$color-header: #b4aaf4;
							
							header {background: $color-header;}
						
					

Nesting

							
								nav {
									  background: #000;
									    li {
									      display: inline;}
									}
							
						

CSS Output

						
							nav {
									  background: #000;
									}
									nav li {
									  display: inline;
									}							
						
					

Partials

Are .scss files preceeded with an underscore => _filename.scss

						
							├── ie.scss
							├── partials
							│   ├── _base.scss
							│   ├── _fonts.scss
							│   ├── _forms.scss
							│   ├── _header.scss
							│   ├── _layout.scss
							│   ├── _menu.scss
							│   └── _mixins.scss
							├── print.scss
							└── screen.scss
						
						

Operators

addition +, subtraction -, multiplication *, division /, and modulo %)

							
								.foo {width: 100px + 30px * 3/ 2 - 25px;}
							
						

CSS Output

							
								.foo {width: 120px;}
							
						

Data Types

  • Numbers
  • Strings
  • Colors
  • Booleans
  • Null
  • Lists
  • MapsNew in Sass 3.3

@Directives

  • at-root
  • extend
  • include
  • import
  • media

@at-root

								
									.avatar {
										  background-color: red;
										  height: 120px;
										  margin: 40px;
										  width: 120px;

										  @at-root {
										    @keyframes fade {
										      0% { transform: scale(1.0); }
										      25% { transform: scale(1.1); }
										      50% { transform: scale(1.0); }
										      75% { transform: scale(1.2); }
										      100% { transform: scale(1.1); }
										    }
										  }
										  &:hover {
										    animation: fade .8s infinite ease-in alternate;
										  }
										}

										--Outputs--

										.avatar {
						  background-color: red;
						  height: 120px;
						  margin: 40px;
						  width: 120px;
							}
							@keyframes fade {
							  0% {
							    transform: scale(1);
							  }
							  25% {
							    transform: scale(1.1);
							  }
							  50% {
							    transform: scale(1);
							  }
							  75% {
							    transform: scale(1.2);
							  }
							  100% {
							    transform: scale(1.1);
							  }
							}
							.avatar:hover {
							  animation: fade .8s infinite ease-in alternate;
							}

								
							

@extend

									
										.error {
											  border: 1px #f00;
											  background-color: #fdd;
											}
											.seriousError {
											  @extend .error;
											  border-width: 3px;
											}
												
												/*** Outputs ***/

											.error, .seriousError {
											  border: 1px #f00;
											  background-color: #fdd;
											}

											.seriousError {
											  border-width: 3px;
											}
									
								

@include

							
								@include mq(ml) {
								    .nav-top {display: none;}
								}
							
						

@imports

Allows you to import modules and partials

								
									@import "compass/utilities";
									@import "partials/base";
									@import "partials/fonts";
								
							

@media

							
								.profile-img {
								  float: left;
								  width: 10em;
								    @media screen and (min-width: 25em){
								      width: 15em;
								   }
								} 	

							--OutPuts--	

								.profile-img {
									  float: left;
									  width: 10em; }
									  @media screen and (min-width: 25em) {
									    .profile-img {
									      width: 15em;
									    }
									  }					
							
						

Mixins

						
							@mixin transform ($scale){
						  -webkit-transform: scale($scale);
						     -moz-transform: scale($scale);
						      -ms-transform: scale($scale);
						       -o-transform: scale($scale);
						          transform: scale($scale);
						}

							.foo {@include transform(.5);}
						
					

CSS Output

						
							.foo {
						  -webkit-transform: scale(0.5);
						  -moz-transform: scale(0.5);
						  -ms-transform: scale(0.5);
						  -o-transform: scale(0.5);
						  transform: scale(0.5);
						}
						
					
P

Control Directives

@if

							
								@function f($a) {
							    @return if(length($a) > 1, nth($a, 2), $a);
							}
							
						

@else

							
								
							
						

@else if

							
								@mixin li ($style) {
									  @if ($style == float) {
									    float:right;
									    margin-left: 2%;
									  }
									  
									  @else if ($style == inline){
									     display: inline;
									     margin-right: 2%;
									  }
									}
							
						

@each

							
								$breakpoints: (
												s: 20em,
												m: 30em,
												l: 48em,
												xl: 64em,  
											);

											@mixin mq($) {
											  @each $name, $breakpoint in $breakpoints {
											    @if ($name == $size) {
											      @media (max-width: $breakpoint) {
											        @content;
											      }
											    }
											  }
											}
							
						

@for

							
								@for $i from 1 through $grid-columns {
								  .grid-#{$i} { @include grid-base($i); @extend .grid-block; }
								}
							
						

@while

							
								$types: 4
								$type-width: 20px

								@while $types > 0
								  .while-#{$types}
								    width: $type-width + $types
								  $types: $types - 1
							
						

Functions

RGB, HSL, Opacity, Color, String, Number, List, Map, Introspection

Custom functions

							
								@function percent() {
								  @return ($container-width/$col-width) * 100;
								  }
							
						

$List

							
								$a: 1 2 3 4 5 a f;
							
						

$Maps

							
								$a: ('city': brooklyn,
								     'state': new York,
								);
							
						

Libraries and Frameworks

Compass

Bourbon

Breakpoint

Susy

Singularity

Zengrids

Foundation

Bootstrap-sass

Normalize-scss

And many more ...

Thank You

By: Robert James /@bkrjames