On Github iowacodeninja / sass-lunch-learn
Or, CSS With Superpowers
So what do we need to get SASSy?
You won't ever edit or commit the .css files.
EVAR.
#navbar { width: 80%; height: 23px; ul { list-style-type: none; } li { float: left; a { font-weight: bold; } } }
#navbar { width: 80%; height: 23px; } #navbar ul { list-style-type: none; } #navbar li { float: left; } #navbar li a { font-weight: bold; }
a { color: #ce4dd6; &:hover { color: #ffb3ff; } &:visited { color: #c458cb; } }
a { color: #ce4dd6; } a:hover { color: #ffb3ff; } a:visited { color: #c458cb; }
Nesting tags within each other translates to a SPACE (descendant selector).
#siteContainer{ .block{} }
/* Outputs to... */ #siteContainer .block{}
Using the ampersand in FRONT removes the space.
#siteContainer{ &.block{} &:hover{} }
/* Outputs to... */ #siteContainer.block{} #siteContainer:hover{}
Using the ampersand BEHIND specifies a parent selector.
#siteContainer{ color: red; .blue &{ color: blue; } }
/* Outputs to... */ #siteContainer{ color: red; } .blue #siteContainer{ color: blue; }
Variables are used to store/reuse commonly used pieces of information
$green: #8fc357; #siteNavContainer{ background-color: $green; }
You can do addition, subtraction, multiplication and division on variables.
$margin: 16px; .border { padding: $margin / 2; margin: $margin + 5px; }
This standard output format will work in most cases:
color: $green;
But what if you want to use the variable as part of the property name?
/* doesn't work */ border-$direction: 1px solid $green; /* does work */ border-{$direction}: 1px solid $green;
Allows you to include smaller stylesheets inside larger ones.
@import "partials/filename"
@import "partials/filename"
/* include the Compass framework */ @import "compass"; /* include a file inside your folder, 'partials/_colors.scss' */ @import "partials/colors";
Mixins are reusable, configurable chunks of CSS.
@include my-mixin;
/* turns a bulleted list into a horizontal list */ ul.horizontal{ @include horizontal-list; }
Old way:
<link href="/web/css-min/common/css/html5Reset.css" rel="stylesheet">
New way:
@import "blueprint/reset";
$green: #128c7f; $light-green: lighten($green, 20%); $dark-green: darken($green, 20%);
Old way:
.rounded-corner{ -webkit-border-radius: 4px; -moz-border-radius: 4px; -khtml-border-radius: 4px; border-radius: 4px; }
New way:
.rounded-corner{ @include border-radius(4px); }
Grids require math. Compass does the math for you!
// grid setup $blueprint-grid-columns: 9; $blueprint-grid-margin: 20px; $blueprint-grid-width: 90px; #siteContainer{ @include container; } #mainColumn{ @include column(6); } #rightColumn{ @include column(3); @include last; }
Compass gives you tools to generate sprites automagically.
@import "my-icons/*.png";
@import "my-icons/*.png"; .actions { .new { @include my-icons-sprite(new); } .edit { @include my-icons-sprite(edit); } }
$width: my-icons-sprite-width(new);
@mixin green-me(){ background-color: #9f9; border: 2px solid #096; } p{ @include green-me; }
You can define arguments to be passed into your mixin to change the output.
@mixin green-me($border-style: solid){ background-color: #9f9; border: 2px $border-style #096; } p{ @include green-me($border-style: dashed); }
You can use basic control structures (if/else, for loops) in mixins.
@mixin color-me($color: blue, $border-style: solid){ @if $color == blue{ background-color: #b2deed; border: 2px $border-style #008bb4; }@else if $color == green{ background-color: #9f9; border: 2px $border-style #096; } } p{ @include color-me($color: green, $border-style: dashed); }
Balance it out somewhere in the middle.
Similar to mixins, but they generally use math or logic to return a value (rather than a set of styles).
/* Compass span function: returns a width in columns */ @function span($n) { @return $blueprint-grid-width * $n + $blueprint-grid-margin * ($n - 1); } #mainColumn{ width: span(6); }
/* this is the column mixin (does floating, margin and width) */ #mainColumn{ @include column(6); } /* this is the span function (just returns width) */ #mainColumn{ width: span(6); }
We used imports of partials with media queries.
// ---- core-responsive.scss --- /* mobile layout */ @media screen and (max-width: 653px){ @import "partials/narrow/core"; } /* tablet portrait */ @media screen and (min-width: 654px){ @import "partials/normal/core"; } /* Widescreen layout */ @media screen and (min-width: 990px){ @import "partials/wide/core"; } // ---- core.scss --- @import "partials/narrow/core"; @import "partials/normal/core"; @import "partials/wide/core";
Same trick, with additional variables
// ---- image.scss ---- $secure: false; @import "partials/image"; // ---- secureimage.scss ---- $secure: true; @import "partials/image"; // ---- partials/image.scss ---- $image_path: "http://images.meredith.com/parents/"; $secure_image_path: "https://secure.parents.com/parents/"; @if $secure{ $image_path: $secure_image_path; } #header{ background: url("#{$image_path}image.png"; }
/* ---- image.css ---- */ #header{ background: url("http://images.meredith.com/image.png"; } /* ---- secureimage.css ---- */ #header{ background: url("https://secure.parents.com/image.png"; }
a.more{ @include block-more-link; } ul{ @include pink-bullets; } .blogModule{ @include content-box; } .up{ @include triangle($direction: up, $size: 10px); }
Questions?