sass and compass - getting started

29
Sass & Compass Presented by: Ethan Gardner

Upload: edgincvg

Post on 28-Jan-2015

126 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Sass and Compass - Getting Started

Sass & CompassPresented by: Ethan Gardner

Page 2: Sass and Compass - Getting Started

What is Sass?

• CSS preprocessor written in Ruby

• Compiles to regular CSS

• Adds many helpful features to CSS…

Official Documentation: http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html

Page 3: Sass and Compass - Getting Started

What is Compass?

• An extension of Sass

• Contains many CSS3 helpers used in modern web development.

• Adds many helpful features to CSS…

Official Documentation: http://compass-style.org/reference/compass/

Page 4: Sass and Compass - Getting Started

Retrofitting Compass to Existing Project

$ compass create --bare --sass-dir "sass" --css-dir "." --javascripts-dir "assets/scripts"--images-dir "images"

Installation: http://compass-style.org/install/

Page 5: Sass and Compass - Getting Started

After Running…

Page 6: Sass and Compass - Getting Started

Changing Options

To change these options after Compass has been added to the project, you can always edit the “config.rb” file

Page 7: Sass and Compass - Getting Started

Starting Compass

Navigate to the directory that contains the “config.rb” file in the command line and type:

compass watch

Page 8: Sass and Compass - Getting Started

Variables

• Common values can assigned to variables for use throughout a project.

• One of the best features of Sass

• $variable-name: value;

Page 9: Sass and Compass - Getting Started

Using Variables

SCSS$accent-color:#17CF57;

h1 {color:$accent-color;

}

fieldset {border:1px solid $accent-color;

}

caption {background:$accent-color;color:#fff;

}

CSS outputh1 {color: #17cf57;

}

fieldset {border: 1px solid #17cf57;

}

caption {background: #17cf57;color: #fff;

}

Page 10: Sass and Compass - Getting Started

Working with Variables

• Assign at the top of the file or in an external file

• Great for consistency

• Benefits are apparent with larger files

Page 11: Sass and Compass - Getting Started

Nested Rules

Rules can be nested to add specificity to a selector.

Page 12: Sass and Compass - Getting Started

Adding Specificity with Nested Rules

SCSSul {list-style-type: disc;li {

padding:10px;}

}

CSS outputul {list-style-type: disc;

}ul li {padding: 10px;

}

Page 13: Sass and Compass - Getting Started

Ampersands in Nested Rules

When nested rules are used, an ampersand adds properties to a child selector and is useful for pseudo classes like :hover.

Page 14: Sass and Compass - Getting Started

Ampersands in Nested Rules

SCSSa {text-decoration:none;&:hover {

text-decoration:underline;}

}

CSS outputa {text-decoration: none;

}a:hover {text-decoration: underline;

}

Page 15: Sass and Compass - Getting Started

Ampersands in Nested Rules (cont’d)

SCSSp {font-size:14px;&.error {

color:#f00;font-weight:bold;

}

}

CSS outputp {font-size: 14px;

}p.error {color: #f00;font-weight: bold;

}

Page 16: Sass and Compass - Getting Started

Did you catch the difference?

Notice there is no space between p and .error in the CSS from the previous example.

Page 17: Sass and Compass - Getting Started

If you WANTED a space…

SCSSp {font-size:14px;.error {

color:#f00;font-weight:bold;

}

}

CSS outputp {font-size: 14px;

}p .error {color: #f00;font-weight: bold;

}

Page 18: Sass and Compass - Getting Started

Multiple Nesting

You don’t have to stop at one level of nesting. Multiple levels of nesting can be VERY powerful.

Page 19: Sass and Compass - Getting Started

Multiple Nesting

SCSS.tablesorter {border:1px solid #000;thead {

background:#000;color:#fff;th {

text-align:center;}

}tr {

background:#fff;}

}

CSS output.tablesorter {border: 1px solid #000;

}.tablesorter thead {background: #000;color: #fff;

}.tablesorter thead th {text-align: center;

}.tablesorter tr {background: #fff;

}

Page 20: Sass and Compass - Getting Started

@extend

@extend uses the properties of a selector as the foundation for another selector

Page 21: Sass and Compass - Getting Started

@extend

SCSS.session {padding-left:8px;padding-right:20px;background-repeat:repeat-y;

}

.keynote {@extend .session;background-image:url(cat-1.png);

}

.company {@extend .session;background-image:url(cat-2.png);

}

CSS output.session, .keynote, .company {padding-left: 8px;padding-right: 20px;background-repeat: repeat-y;

}

.keynote {background-image: url(cat-1.png);

}

.company {background-image: url(cat-2.png);

}

Page 22: Sass and Compass - Getting Started

@import

Properties can be imported from other Sass files.

Page 23: Sass and Compass - Getting Started

Tips for using @import

• Put common properties in external file

• Different than using @import in CSS

• Imported files should begin with an “_”

Page 24: Sass and Compass - Getting Started

@import

SCSS// Loads Compass CSS3 helpers@import 'compass/css3';

// Loads user defined files@import 'base';@import 'reset';

CSS output/* Loads all helpers from Compass’ CSS3 modules and makes it available to the rest of our project.

Imports the “_base.scss” file and then imports the “_reset.scss” file .*/

Page 25: Sass and Compass - Getting Started

@mixin

• Small piece of code that is used to build something larger

• Define once and use anywhere

• Can accept arguments and parameters

• Used via the @include keyword

Page 26: Sass and Compass - Getting Started

@mixin – User Defined

SCSS

$border-color:#efefef;

@mixin content-area {border:1px solid $border-color;padding:10px 2%;

}

#article {@include content-area;float:left;width:60%;

}

#aside {@include content-area;float:right;width:30%;

}

CSS output#article {border: 1px solid #efefef;padding: 10px 2%;float: left;width: 60%;

}

#aside {border: 1px solid #efefef;padding: 10px 2%;float: right;width: 30%;

}

Page 27: Sass and Compass - Getting Started

Compass @mixin

Compass provides mixins for common CSS3 features like gradients, box-shadow, text-shadow, etc.

Page 28: Sass and Compass - Getting Started

@mixin – Compass

SCSS#article {@include background-image(linear-gradient(#dedede, #fff));color:$content-text-color;@include content-area; // user defined from previous example@include box-shadow;@include text-shadow;float:left;width:60%;

}

Page 29: Sass and Compass - Getting Started

@mixin – Compass (cont’d)

CSS output#article {

background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #dedede), color-stop(100%, #ffffff));

background-image: -webkit-linear-gradient(#dedede, #ffffff);background-image: -moz-linear-gradient(#dedede, #ffffff);background-image: -o-linear-gradient(#dedede, #ffffff);background-image: -ms-linear-gradient(#dedede, #ffffff);background-image: linear-gradient(#dedede, #ffffff);color: black;border: 1px solid #efefef;padding: 10px 2%;-webkit-box-shadow: 0px 0px 5px #333333;-moz-box-shadow: 0px 0px 5px #333333;box-shadow: 0px 0px 5px #333333;text-shadow: #efefef 0px 0px 1px;float: left;width: 60%;

}