Introduction to sass

22
SASS (Syntactically Awesome Style Sheets) Anoop P R Senior Software Engineer Experion Technologies

description

Introduction to syntactically awesome style sheets (sass) taken on 25th July 2014

Transcript of Introduction to sass

Page 1: Introduction to sass

SASS (Syntactically Awesome Style

Sheets)Anoop P R

Senior Software EngineerExperion Technologies

Page 2: Introduction to sass

CSS Problems

• Repetitive • Not expressive• No variables• No functions• No calculation• No inheritance etc.

Page 3: Introduction to sass

What is Sass?

• SASS is a system that extends CSS and can make you more efficient and make your CSS more organized.• Serves as

Scripting language Preprocessor for CSS

Page 4: Introduction to sass

SASS has two main parts

• SassScript (.sass & .scss) SassScript is a scripting language that extends the CSS

syntax, adding nested rules, variables, mixins.• SASS CSS pre-processor

The pre-processor interprets your SassScript and produces well formatted standard CSS.

Page 5: Introduction to sass

SASS Syntax

body font: 100% $font-stack color: $primary-color

body { font: 100% $font-stack; color: $primary-color;}

.sass

.scss

Page 6: Introduction to sass

The process

• Write your SassScript• Use CSS pre-processor to interpret your SassScript• CSS files are produced by the pre‐processor (use

these in your website)

Page 7: Introduction to sass

What sass offers

• Variables• Nesting• Mixins• Partials• Selector inheritance• Calculations• Conditions

Page 8: Introduction to sass

Variables

• Variables allow you to assign a value to an easy-to-remember placeholder name Works with hex values, strings of text, colors, numbers,

boolean values etc. No more memorizing hex values!

Page 9: Introduction to sass

Variables

h1#master { color: #f00;}

.post-hd { color: #ff0000;}

a.outline { color: red;}

.sidebar p { color: rgba(255, 0, 0, 1);}

css scss

$base-color : red;

h1#master { color: $base-color;}

.post-hd { color: $base-color;}

a.outline { color: $base-color;}

.sidebar p { color: $base-color;}

Page 10: Introduction to sass

Nesting

• Nested rules allow you to break up endlessly long selectors of CSS

Page 11: Introduction to sass

Nesting

.nav ul { margin: 0; padding: 0; list-style: none;}.nav ul li { display: inline-block;}.nav ul a { display: block; padding: 6px 12px; text-decoration: none;}.nav ul a:hover { color: #fff;}

css scss.nav { ul { margin: 0; padding: 0; list-style: none; li {

display: inline-block; } } a { display: block; padding: 6px 12px; text-decoration: none; &:hover {

color: #fff; } }}

Page 12: Introduction to sass

Nesting (properties)

.selected-text { font-family: "Georgia", Times, serif; font-style: oblique; font-size: 12px;}

.selcted-text { font { family: "Georgia", Times, serif; style: oblique; size: 12px; }}

css

sass

Page 13: Introduction to sass

Mixins

• Mixins allow you to chunk up CSS declarations to be reusable with one reference.• Reusable code

Page 14: Introduction to sass

Mixins

.box-1 { -webkit-border-radius: 10px; -moz-border-radius: 10px; -ms-border-radius: 10px; border-radius: 10px;}

.box-2 { -webkit-border-radius: 25px; -moz-border-radius: 25px; -ms-border-radius: 25px; border-radius: 25px;}

css

Page 15: Introduction to sass

Mixins

.box-1 { -webkit-border-radius: 10px; -moz-border-radius: 10px; -ms-border-radius: 10px; border-radius: 10px;}

.box-2 { -webkit-border-radius: 25px; -moz-border-radius: 25px; -ms-border-radius: 25px; border-radius: 25px;}

css scss

@mixin border-radius($radius: 12px) { -webkit-border-radius: $radius; -moz-border-radius: $radius; -ms-border-radius: $radius; border-radius: $radius;}

.box-0 {@include border-radius;}

.box-1 { @include border-radius(10px); }.box-2 { @include border-radius(25px); }

Tip: Mixins can reference mixins as well

Page 16: Introduction to sass

Selector inheritance

• Inheritance imports syntax and styles from another selection.

Page 17: Introduction to sass

Selector inheritance

.message { border: 1px solid #ccc; padding: 10px; color: #333;}.success { @extend .message; border-color: green;}.error { @extend .message; border-color: red;}.warning { @extend .message; border-color: yellow;}

scss css

.message, .success, .error, .warning { border: 1px solid #ccc; padding: 10px; color: #333;}.success { border-color: green;}.error { border-color: red;}.warning { border-color: yellow;}

Page 18: Introduction to sass

Partials

• You can create partial Sass files that contain little snippets of CSS that you can include in other Sass files.• A partial is simply a Sass file named with a leading

underscore.• Sass partials are used with the @import directive.

_buttons.scss _navigation.scss

genaral.sass

@import "buttons", "navigation";

Page 19: Introduction to sass

Arithmetic operations

• Sass has a handful of standard math operators like +, -, *, /, and %

.main-article { float: left; width: 600px / 960px * 100%;}

Page 20: Introduction to sass

Conditions

$type: monster;

p { @if $type == ocean { color: blue; } @else if $type == matador { color: red; } @else if $type == monster { color: green; } @else { color: black; }}

Page 21: Introduction to sass

SASS Compilation

• Install Sass•

• SASS to CSS

• Watch sass file

gem install sass

sass input.scss output.css

sass --watch input.scss:output.css

Page 22: Introduction to sass

Thank You!