30
0

Introduction to CSS Preprocessors

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Introduction to CSS Preprocessors

0

Page 2: Introduction to CSS Preprocessors

Introduction to CSSPreprocessors

Honestly... Just SaSS

by Lucas Torres

Page 3: Introduction to CSS Preprocessors

Our staff

Page 4: Introduction to CSS Preprocessors

You can be part of our staff!

inQbation is looking for two greatdevelopers

HTML, CSS and Js lover? You can be our next Front-enddeveloperEnjoy coding in Python, PHP and Drupal? Then the back-enddeveloper spot could be yours

Page 5: Introduction to CSS Preprocessors

About me

Lucas TorresWeb Developer atinQbation

Page 6: Introduction to CSS Preprocessors

About mePython, PHPHTML, CSS, JavaScriptDrupalCrazy about UX and User Centered DesignPlaying with Node.js and MongoDB

Page 7: Introduction to CSS Preprocessors

So, a CSS preprocessor receive some instructions and compilethem to .css files

What are CSS Preprocessors?From Wikipedia:

...a preprocessor is a program that processes itsinput data to produce output that is used as

input to another program.

Page 8: Introduction to CSS Preprocessors

And what can I do with them?Have you ever dream about using the advantages of a

programming language with CSS? Well, that's what we are ableto do with CSS preprocessors.

Use variables, functions, mixins, and more.

Page 9: Introduction to CSS Preprocessors

Which one should I choose?There are many CSS Preprocessors

Page 10: Introduction to CSS Preprocessors

Which one should I choose?I can't compete with more than 1 million results from Google ;)

Page 11: Introduction to CSS Preprocessors

Which one should I choose?So, as with beer: Choose the one that tastes better for you

Page 12: Introduction to CSS Preprocessors

My personal taste is SaSSAnd in beer is Modelo Especial ;)

Page 13: Introduction to CSS Preprocessors

Main differences: SaSS Vs. Less

SaSS

//Variables$main_color: #000;

//Nestingp { color: $main_color;

a { text-decoration: underline; }}

//Mixins@mixin shaded-box { box-shadow: 2px 2px 0px #000;}

//Functions@function some-function($arg) { @return $arg;}

Less

//Variables@main_color: #000;

//Nestingp { color: @main_color;

a { text-decoration: underline; }}

//Mixins.shaded-box { box-shadow: 2px 2px 0px #000;}

//Functions/* 404 not found :( */

Page 14: Introduction to CSS Preprocessors

SaSS superpowersVariablesNestingPartials & ImportMixingsExtend/InheritanceOperatorsAnd yes, functions!

Page 15: Introduction to CSS Preprocessors

Enough talking man, show methe code!

Since we don't have enough time to learn SaSS features frombasics to advanced, I'll show the coolest ones so you can research

deeper later.You can go to to learn morehttp://sass-lang.com/documentation

Page 16: Introduction to CSS Preprocessors

Lets start with a simple css like this

h1 { font-size: 20px; color: #666;}p { color: #666;}.content { overflow: hidden; background-color: #F6F6F6;}.content h1 { font-size: 18px; color: #333;}.content p { font-size: 12px; text-shadow: 1px 1px 0 #000; color: #333;}.content p a { color: #666; text-decoration: none;}

Page 17: Introduction to CSS Preprocessors

Now, the same code, written in SaSS. Let's begin with variables

//You can define variables. //BTW, comments like this won't compile in your CSS $main_fg_color: #666; $secondary_fg_color: #333; h1 { font-size: 20px; color: $main_fg_color; } p { color: $main_fg_color; } .content { overflow: hidden; background-color: #F6F6F6; } .content h1 { font-size: 18px; color: $secondary_fg_color; } .content p { font-size: 12px; text-shadow: 1px 1px 0 #000; color: $secondary_fg_color; } .content p a { color: $main_fg_color; text-decoration: none; }

SaSS allows you to use variables, so you can stick to the DRYprinciple and keep the code simple and easy to maintain.

Gist http://sassmeister.com/gist/9771767

Page 18: Introduction to CSS Preprocessors

What about nesting?

/* * You can nest your selectors. * and guess what? * Yes! this comment will be compiled to your CSS */ $main_fg_color: #666; $secondary_fg_color: #333; h1 { font-size: 20px; color: $main_fg_color; } p { color: $main_fg_color; } .content { overflow: hidden; background-color: #F6F6F6; h1 { font-size: 18px; color: $secondary_fg_color; } p { font-size: 12px; text-shadow: 1px 1px 0 #000; color: $secondary_fg_color; a { color: $main_fg_color; text-decoration: none; }

You can nest selectors, just as in HTML. Make sense, right?

Gist http://sassmeister.com/gist/9771826

Page 19: Introduction to CSS Preprocessors

Talking about easy to maintain, let me introduce you partials &import

_text.scss

p { color: #333; a { color: #000; text-decoration: none; } }

main.scss

@import "text";

SaSS won't compile any file with an underscore at the beginning(that's a partial), and the @import directive would import (duh!)

that file.

Page 20: Introduction to CSS Preprocessors

Want so see some real action? Ok, let's check the

Mixins

Page 21: Introduction to CSS Preprocessors

MixinsReuse instead of rewrite, that should be the definition of Mixins.

//Define de Mixin properties @mixin shaded-box { -webkit-box-shadow: 2px 2px 0px rgba(0, 0, 0, 0.75); -moz-box-shadow: 2px 2px 0px rgba(0, 0, 0, 0.75); box-shadow: 2px 2px 0px rgba(0, 0, 0, 0.75); padding: 5px; } //Apply the Mixin .content { background-color: #F6F6F6; @include shaded-box; }

Gist http://sassmeister.com/gist/9772361

Page 22: Introduction to CSS Preprocessors

Mixins with argumentsThey look like functions, but they are not. (isn't it a superpower?)

//Define de Mixin properties @mixin shaded-box( $blur, $opacity ) { -webkit-box-shadow: 2px 2px $blur rgba(0, 0, 0, $opacity); -moz-box-shadow: 2px 2px $blur rgba(0, 0, 0, $opacity); box-shadow: 2px 2px $blur rgba(0, 0, 0, $opacity); padding: 5px; } //Apply the Mixin .content { background-color: #F6F6F6; @include shaded-box( 2px, 0.75 ); }

Gist http://sassmeister.com/gist/9772390

Page 23: Introduction to CSS Preprocessors

FunctionsNo more child games! Let's use CSS as a programming language

@function set-background($img: false, $color: #F4F4F4) { @if $img != false { @return #{$color} url(#{$img}) no-repeat left top; } @else { @return #{$color}; } } .container { background: set-background("photo.png", #000); }

Not an useful function, but.. it's a function inside CSS!Gist http://sassmeister.com/gist/9772407

Page 24: Introduction to CSS Preprocessors

Control DirectivesI bet you saw the @if statement in the last slide, well, there is

more for you.

//Create a quick grid /* Number of columns */ $columns: 12; @for $i from 1 through $columns { .col-#{$i} { width: (100% / $columns) * $i; float: left; } }

You can use @if, @else if, @else, @for, @each and @whileGist http://sassmeister.com/gist/9772438

Page 25: Introduction to CSS Preprocessors

Control DirectivesNow, the same grid but using a function

@function get-col-width($width, $columns, $number){ @return ($width / $columns) * $number; } $columns: 6; @for $i from 1 through $columns { .columns-#{$i} { width: get-col-width(960px, $columns, $i); @if $i < $columns { float: left; } @else { float: right; } } }

Gist http://sassmeister.com/gist/9772499

Page 26: Introduction to CSS Preprocessors

Wait! It gets even better!Reinventing the wheel is not nice...

You can reuse Compass mixins, functions and more.

Page 27: Introduction to CSS Preprocessors

CompassA brief example with Compass

Gist http://sassmeister.com/gist/9773018

And let's proceed with some questions.

Page 28: Introduction to CSS Preprocessors

Questions?

Page 29: Introduction to CSS Preprocessors

Thank you!

Page 30: Introduction to CSS Preprocessors