70
Welcome to Creative Coders!

Css Preprocessors

Embed Size (px)

DESCRIPTION

Talk on CSS Preprocessors from Creative Coders in Singapore

Citation preview

Page 1: Css Preprocessors

Welcome to Creative Coders!

Page 2: Css Preprocessors
Page 3: Css Preprocessors

CSS Preprocessors

Page 4: Css Preprocessors

Ed Moore

twitter: @_edmoore

gplus: +EdMoore

github: eddiemoore

Page 5: Css Preprocessors

CSS is Awesome

Page 6: Css Preprocessors

Single Element

Page 7: Css Preprocessors

FIRE

Page 8: Css Preprocessors

CSS SUCKS

Page 9: Css Preprocessors
Page 10: Css Preprocessors

CSS3 not consistant across browsers

Modularisation sucks

Hard to stay DRY

No Variables*

Page 11: Css Preprocessors

Vendor Prefixes‐webkit‐whatever: something‐moz‐whatever: something‐o‐whatever: something‐ms‐whatever: somethingwhatever: something

Page 12: Css Preprocessors
Page 13: Css Preprocessors

Enter CSS Preprocessors

Page 14: Css Preprocessors
Page 15: Css Preprocessors
Page 16: Css Preprocessors

Any valid CSS file is a valid SASS/LESS/Stylus file

Page 17: Css Preprocessors

Variables

Page 18: Css Preprocessors

SASS$primaryColor: #BADA55;

a {  color: $primaryColor;}

Page 19: Css Preprocessors

LESS@primaryColor: #BADA55;

a {  color: @primaryColor;}

Page 20: Css Preprocessors

StylusprimaryColor = #BADA55

a  color: primaryColor  

Page 21: Css Preprocessors

Nesting

Page 22: Css Preprocessors

.module {  width: 500px;    a {    color: red;  }    ul {    li {      display: inline‐block;      a {        color: pink;      }    }  }}

Page 23: Css Preprocessors

.module {  width: 500px;}.module a {  color: red;}.module ul li {  display: inline‐block;}.module ul li a {  color: pink;}

Page 24: Css Preprocessors

Avoid inception

Page 25: Css Preprocessors

Only go 3 levels deep

Page 26: Css Preprocessors

BAD#mything .module .something ul li a { ... }

Page 27: Css Preprocessors

Partials

Page 28: Css Preprocessors

Break up your styles@import 'base/reset';@import 'base/typography';

@import 'layout/grid';...

Page 29: Css Preprocessors

Functions

Page 30: Css Preprocessors

@mixin box‐shadow($top, $left, $blur, $color) {  ‐webkit‐box‐shadow: $top $left $blur $color;  ‐moz‐box‐shadow: $top $left $blur $color;  box‐shadow: $top $left $blur $color;}

div {  @include box‐shadow(2px, 2px, 5px, rgba(0,0,0,0.3));}

Page 31: Css Preprocessors

div {  ‐webkit‐box‐shadow: 2px 2px 5px rgba(0,0,0,0.3);  ‐moz‐box‐shadow: 2px 2px 5px rgba(0,0,0,0.3);  box‐shadow: 2px 2px 5px rgba(0,0,0,0.3);}

Page 32: Css Preprocessors

Maths Operations

Page 33: Css Preprocessors

.col‐1‐2 {  width: (100% / 2);}.col‐1‐3 {  width: (100% / 3);}.col‐2‐3 {  width: (100% / 3) * 2;}

Page 34: Css Preprocessors

.col‐1‐2 {  width: 50%;}.col‐1‐3 {  width: 33.33333%;}.col‐2‐3 {  width: 66.66667%;}

Page 35: Css Preprocessors

Extending / Inheritance

Page 36: Css Preprocessors

.foo {  color: red;}.bar {  @extend .foo;}

Page 37: Css Preprocessors

.foo, .bar {  color: red;}

Page 38: Css Preprocessors

Referencing Parent Selectors

Page 39: Css Preprocessors

a {  color: red;  &;:hover {    color: pink;  }}

Page 40: Css Preprocessors

a {  color: red;}a:hover {  color: pink;}

Page 41: Css Preprocessors

a {  color: red;    .module &; {    color: green;  }}

Page 42: Css Preprocessors

a {  color: red;}.module a {  color: green;}

Page 43: Css Preprocessors

Colour Functions

Page 44: Css Preprocessors

button {  background: #000;    &;:hover {    color: lighten(#000, 20%);  }}

Page 45: Css Preprocessors

button {  background: #000;}button:hover {  background: #333333;}

Page 46: Css Preprocessors

Loops

Page 47: Css Preprocessors

$total‐columns: 6;$col‐widths: ();@for $i from 1 through $total‐columns {  @for $j from 1 through $i {    $w: ($j/$i);        @if index($col‐widths, $w) == false {      .col‐#{$j}‐#{$i} {        width: $w * 100%;      }      $col‐widths: append($col‐widths, $w, comma);    }  }}

Page 48: Css Preprocessors

.col‐1‐1 { width: 100%; }

.col‐1‐2 { width: 50%; }

.col‐1‐3 { width: 33.33333%; }

.col‐2‐3 { width: 66.66667%; }

.col‐1‐4 { width: 25%; }

.col‐3‐4 { width: 75%; }

.col‐1‐5 { width: 20%; }

.col‐2‐5 { width: 40%; }

.col‐3‐5 { width: 60%; }

.col‐4‐5 { width: 80%; }

.col‐1‐6 { width: 16.66667%; }

.col‐5‐6 { width: 83.33333%; }

Page 49: Css Preprocessors

Utilities

Page 53: Css Preprocessors

How the *!@$ do I turn all that into CSS?!?

Page 54: Css Preprocessors

Embrace the command line

Page 55: Css Preprocessors
Page 56: Css Preprocessors

Tools

Page 57: Css Preprocessors

LiveReload

Page 58: Css Preprocessors

CodeKit (OSX only)

Page 59: Css Preprocessors

Compass.app

Page 62: Css Preprocessors

Koala

Page 63: Css Preprocessors

Less.app (OSX only)

Page 64: Css Preprocessors

Crunch! (Less)

Page 66: Css Preprocessors

Future

Page 67: Css Preprocessors

CSS Variables:root {  ‐‐main‐bg‐color: brown;}

.one {  background‐color: var(‐‐main‐bg‐color);}

Note: The custom property prefix was var- in the earlier spec, but later changed to --. Firefox 31 and above follow thenew spec.

MozillaCan I Use

Page 68: Css Preprocessors

MythCSS the way it was imagined.

Myth.io

Page 69: Css Preprocessors
Page 70: Css Preprocessors

Ed Mooretwitter: @_edmoore

gplus: +EdMoore

github: eddiemoore

slides: http://bit.ly/1gQqRoe