37
SASS IN THE REAL WORLD Tom Crinson - Front End Suffolk - April 30th

SASS In The Real World

Embed Size (px)

DESCRIPTION

Tom Crinson gave fesuffolk this rousing and entertaining introduction to SASS in 2012. A great introduction to CSS preprocessors.

Citation preview

Page 1: SASS In The Real World

SASS IN THE REAL WORLDTom Crinson - Front End Suffolk - April 30th

Page 2: SASS In The Real World

Hello.

Page 3: SASS In The Real World

I’m a Developer.

Page 4: SASS In The Real World
Page 5: SASS In The Real World

CSSis

MOREthanjustTEXT

Page 6: SASS In The Real World

Structured.

Page 7: SASS In The Real World

Ordered.

Page 8: SASS In The Real World

DRY.(don’t repeat yourself)

Page 9: SASS In The Real World

SASS.(syntactically awesome stylesheets)

A meta language on top of CSSAims to describe the structure of the document cleanly andstructurallySimpler and more elegant syntaxAdds very handy features for creating more manageablestylesheets

Page 10: SASS In The Real World

Intended Target.

Page 11: SASS In The Real World
Page 12: SASS In The Real World

We have a winner.

Page 13: SASS In The Real World

Are you ready?

Page 14: SASS In The Real World
Page 15: SASS In The Real World

The Stylesheet.

Page 16: SASS In The Real World

.style4 { color: #FFFFFF; font-weight: bold; font-size: x-small;}.style5 { border-color: #FF00FF; border-width: 1px; background-color: #FF0000;}.style6 { border: 1px solid #FFFFFF;}.style7 { color: #000000;}.style8 { color: #000000; font-weight: bold;}.style13 { font-family: "Arial Black"; color: #666666;}.style54 { background-color: #FFFF00;}

Everywhere!

Issue 1: Colour Scheme.

Page 17: SASS In The Real World

Solution 1: Variables.

$colour_background: #FFFFFF;$colour_border: #FF00FF;$colour_bordered_background: #FF0000;$colour_first_letter_highlight: #000;$colour_table_border: #000;$colour_text_highlight: #FFFF00;$colour_application: #00FF00;$colour_symposium: #0000FF;

.style5 { border-color: $colour_border; border-width: 1px; background-color: $colour_bordered_background;}.style6 { border: 1px solid $colour_background;}.style7 { color: $colour_first_letter_highlight;}

Page 18: SASS In The Real World

Solution 1: Why?

Keeps colour information in one placeGives the colours meaningful names not necessarily based oncolourEasy to changeReduces duplication

Page 19: SASS In The Real World

Extra Extra!(functions)

$colour_background: #FFFFFF;$colour_border: darken($colour_background, 10%);$colour_bordered_background: #FF0000;$colour_first_letter_highlight: #000;$colour_table_border: complement($colour_background);

Page 20: SASS In The Real World

Issue 2: Nesting Duplication..style196 { color: $colour_bordered_background; font-weight: 700; font-family: Calibri; margin: 0 5px; font-size: small;}

.style194 { font-weight: bold; color: $colour_bordered_background; margin: 0 5px; font-family: Tahoma; font-size: small;}

.style59 { font-weight: bold; color: $colour_bordered_background; margin: 0 5px; font-family: Tahoma;}

.style71 { font-size: small;}

.style66 { font-family: Calibri; font-weight: bold;}

<p class="style196"> 2012 Annual Meeting <p class="style59"> <font size="1" class="style71"> <span style="color: navy" class="style66"> April 14, 2012 </span> </font> </p> <p class="style194"> Ashland University </p></p>

Page 21: SASS In The Real World

Solution 2: Proper CSS.

.style196 { color: $colour_bordered_background; font-weight: 700; font-family: Calibri; margin: 0 5px; font-size: small;}.style196 .style59 { font-weight: bold; font-family: Tahoma;}.style59 .style71 .style66 { font-family: Calibri;}.style196 .style194 { font-weight: bold; font-family: Tahoma;}

.style196 { color: $colour_bordered_background; font-weight: 700; font-family: Calibri; margin: 0 5px; font-size: small;}

.style194 { font-weight: bold; color: $colour_bordered_background; margin: 0 5px; font-family: Tahoma; font-size: small;}

.style59 { font-weight: bold; color: $colour_bordered_background; margin: 0 5px; font-family: Tahoma;}

.style71 { font-size: small;}

.style66 { font-family: Calibri; font-weight: bold;}

Page 22: SASS In The Real World

Solution 2: Nested Rules.

.style196 { color: $colour_bordered_background; font-weight: 700; font-family: Calibri; margin: 0 5px; font-size: small; .style59 { font-weight: bold; font-family: Tahoma; .style66 { font-family: Calibri; } } .style194 { font-weight: bold; font-family: Tahoma; }}

.style196 { color: $colour_bordered_background; font-weight: 700; font-family: Calibri; margin: 0 5px; font-size: small;}

.style194 { font-weight: bold; color: $colour_bordered_background; margin: 0 5px; font-family: Tahoma; font-size: small;}

.style59 { font-weight: bold; color: $colour_bordered_background; margin: 0 5px; font-family: Tahoma;}

.style71 { font-size: small;}

.style66 { font-family: Calibri; font-weight: bold;}

Page 23: SASS In The Real World

Solution 2: Why?

Eliminates duplication - fewer chances for bugs to sneak inShorter - easier to readIt’s a pain to have to type out long selectors

Page 24: SASS In The Real World

Extra Extra!(parent references)

#complicated_selector p > a{ border:1px solid red;}

#complicated_selector p > a:hover { border:1px solid blue;}

#complicated_selector p > a:visited { border:1px solid green;}

#complicated_selector p > a{ border:1px solid red; &:hover { border:1px solid blue; }

&:visited { border:1px solid green; }}

Page 25: SASS In The Real World

Warning!

Be careful how deep you nest; it can become very confusing!•

Page 26: SASS In The Real World

Issue 3:Duplication.

.style3 { font-family: "Arial Black"; font-size: x-small; margin-top: 0; margin-bottom: 0;}.style4 { color: $colour_background; font-weight: bold; font-size: x-small;}.style10 { font-family: Verdana; font-size: x-small; margin-top: 0; margin-bottom: 0;}

Page 27: SASS In The Real World

Solution 3: Mixins .@mixin font-style($family: Arial, $size: x-small, $weight: normal){ font-family: $family; font-size: $size; font-weight: $weight;}

.style3 { @include font-style("Arial Black", x-small); margin-top: 0; margin-bottom: 0;}.style4 { color: $colour_background; @include font-style("Arial", x-small, bold);}.style10 { @include font-style("Verdana", x-small); margin-top: 0; margin-bottom: 0;}

Page 28: SASS In The Real World

A Better Example.(Browser Prefixes)

@mixin rounded($radius: 10px) { border-radius: $radius; -moz-border-radius: $radius; -webkit-border-radius: $radius;}

.rounded_box{ @include rounded;}

Page 29: SASS In The Real World

Solution 3: Why?

Groups together frequently copied bits of code for ease of reuseEliminates copy & paste errorsParameterized functions are awesome

Page 30: SASS In The Real World

Issue 4:Mixing Concerns.

/* =========== Brochure Styles =========== */

.style6 { border: 1px solid $colour_background;}.style7 { color: $colour_first_letter_highlight;}.style8 { color: $colour_first_letter_highlight; font-weight: bold;}.style10 { @include font-style("Verdana", x-small); margin-top: 0; margin-bottom: 0;}.style11 { font-family: Verdana;}.style12 { font-size: x-small;}

/* ============ Product Pages ============ */.style13 { font-family: "Arial Black"; color: #666666;}.style14 { font-family: Verdana; margin-top: 0; margin-bottom: 0;}.style16 { @include font-style("Arial", x-small); color: $colour_first_letter_highlight; margin-top: 0; margin-bottom: 0;}

style.css

Page 31: SASS In The Real World

Solution 4: Multiple Links.

<link href="/stylesheets/products.css" media="screen" rel="stylesheet" type="text/css" /><link href="/stylesheets/home.css" media="screen" rel="stylesheet" type="text/css" /><link href="/stylesheets/brochure.css" media="screen" rel="stylesheet" type="text/css" /><link href="/stylesheets/about.css" media="screen" rel="stylesheet" type="text/css" />Noooo!!!

Page 32: SASS In The Real World

Solution 4: @Import.

@import "home";@import "about";@import "brochure";@import "products";

@include home;@include about;@include brochure;@include products;

home.scssabout.scss

brochure.scssproducts.scss

Page 33: SASS In The Real World

Solution 4: Why?

Breaks stylesheets into more logical sections - forcing you tothink about where each rule belongsKeeps common rules togetherNo overhead in the browser

Page 34: SASS In The Real World

Further Awesomeness.

Sass Docs - http://sass-lang.com/Compass - http://compass-style.org its like Sass on Steroids

Page 35: SASS In The Real World

Final Stylesheet.

$colour_background: #FFFFFF;$colour_border: #FF00FF;$colour_bordered_background: #FF0000;$colour_first_letter_highlight: #000;$colour_table_border: #000;$colour_text_highlight: #FFFF00;$colour_application: #00FF00;$colour_symposium: #0000FF;

@import "home";@import "about";@import "brochure";@import "products";

@include home;@include about;@include brochure;@include products;

Page 36: SASS In The Real World

Goodbye.

Page 37: SASS In The Real World

Image Credits.

http://www.codinghorror.com/blog/2006/11/this-is-what-happens-when-you-let-developers-create-ui.html

http://www.flickr.com/photos/23679420@N00/4573193013/

http://www.flickr.com/photos/mr_t_in_dc/4800819674/sizes/m/in/photostream/