Lauren Byrwa
@labyrwa
Front End Developer at Palantir.net
Heather Rodriguez
@hrodrig
Web Developer at Rock Creek Strategic Marketing
Sass is a lean, clean CSS extension language:
You write Sass; your browser sees CSS.
$bold: 700;
$link-color: #33CCFF;
$sans-serif: 'Arial, Helvetica, sans-serif';
body {
font-family: $sans-serif;
a {
font-weight: $bold;
color: $link-color;
}
ul {
list-style-type: none;
}
}
body {
font-family: "Arial, Helvetica, sans-serif";
}
body a {
font-weight: 700;
color: #33ccff;
}
body ul {
list-style-type: none;
}
Variables store small pieces of information for reuse throughout your stylesheets.
$variable-name: code;
// Fonts
$nimbus-sans: "nimbus-sans", Times, serif;
$bold: 500;
$normal: 400;
$light: 300;
// Colors
$highlight-color: #ffca0a;
$link-color: #ff0000;
$black: #000000;
$white: #ffffff;
$charcoal: darken($white, 70);
// Fonts
$nimbus-sans: "nimbus-sans", Times, serif;
$highlight-color: #ffca0a;
$bold: 500;
blockquote{
font-family: $nimbus-sans;
color: $highlight-color;
font-weight: $bold;
}
// Colors
blockquote{
font-family: "nimbus-sans", Times, serif;
color: #ffca0a;
font-weight: 500;
}
Math is hard. Let Sass take care of the work.
.container{
width: 100%;
}
article[role=”main”]{
float: left;
width: 600px / 960px * 100%;
}
aside[role=”complimentary”]{
float: right;
width: 300px / 960px * 100%;
}
.container{
width: 100%;
}
article[role=”main”]{
float: left;
width: 62.5%;
}
aside[role=”complimentary”]{
float: left;
width: 31.25%;
}
Visual hierarchy makes it easy to understand and organize selector relationships.
.breadcrumb {
font-size: .8em;
margin-bottom: 2rem;
li {
display: inline;
list-style-type: none;
margin: 0;
padding: 0;
}
a{
color: $mirage;
}
}
.breadcrumb {
font-size: .8em;
margin-bottom: 2rem;
}
.breadcrumb li {
display: inline;
list-style-type: none;
margin: 0;
padding: 0;
}
.breadcrumb a{
color: $mirage;
}
Nest Sparingly and avoid overly specific code
Use to merge two selectors, either preceding or following an element.
Use Cases:
.menu a{
color: $crimson;
&:hover{
color: $white;
}
}
.menu a{
color: #ed1c24;
}
.menu a:hover{
color: #ffffff;
}
.accordion__header{
margin: 0;
background: url(../images/retina/accordion-down.png) no-repeat;
background-size: 13px;
.lt-ie9 &{
background: url(../images/ie/accordion-down.png) no-repeat;
}
}
.accordion__header {
margin: 0;
background: url(../images/retina/accordion-down.png) no-repeat;
background-size: 13px;
}
.lt-ie9 .accordion__header {
background: url(../images/ie/accordion-down.png) no-repeat;
}
Split your CSS into smaller, more maintainable snippets
Partials are NOT standalone CSS files
_filename.scss
_alerts.scss
_buttons.scss
_checkboxes.scss
_footer.scss
_forms.scss
_icons.scss
_menus.scss
_messages.scss
_throbbers.scss
_typography.scss
@import "base";
@import "reset";
@import "layouts/responsive";
@import "components/header";
@import "components/navigation";
@import "components/messages";
@import "components/tabs";
@import "components/morelinks";
@import "components/blocks";
@import "components/unpublished";
@import "print";
@mixin border-radius($radius) {
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
-ms-border-radius: $radius;
border-radius: $radius;
}
.hover-content--featured{
@include border-radius(10px);
background: $viking;
font-size: 1.333333333rem;
font-weight: $semibold;
height: 100%;
}
.hover-content--featured{
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
-ms-border-radius: 10px;
border-radius: 10px;
background: $viking;
font-size: 1.333333333rem;
font-weight: $semibold;
height: 100%;
}
Using @extend lets you share CSS properties from one selector to another.
It really helps keep your CSS DRY.
.btn{
background: $whitesmoke;
color: $mirage;
padding: .6rem .8rem;
padding-right: 2.666666667rem;
}
.btn--expand{
@extend .btn;
background: $crimson;
padding-right: 2.133333333rem;
}
.btn,
.btn--expand{
background: $whitesmoke;
color: $mirage;
padding: .6rem .8rem;
padding-right: 2.666666667rem;
}
.btn--expand{
background: $crimson;
padding-right: 2.133333333rem;
}
@mixin repeats the code every time and the CSS gets bloated
@extend will add the selectors together and then output the code just once
@mixin element-invisible{
position: absolute;
height: 1px;
width: 1px;
overflow: hidden;
}
.foo{
@include element-invisible;
}
.bar{
@include element-invisible;
}
.foo{
position: absolute !important;
height: 1px;
width: 1px;
overflow: hidden;
}
.bar{
position: absolute !important;
height: 1px;
width: 1px;
overflow: hidden;
}
.element-invisible{
position: absolute !important;
height: 1px;
width: 1px;
overflow: hidden;
}
.foo{
@extend element-invisible;
}
.bar{
@extend element-invisible;
}
.element-invisible,
.foo,
.bar{
position: absolute !important;
height: 1px;
width: 1px;
overflow: hidden;
}
ANSWER:
%placeholder with @extend
Using placeholder (%) with @extend lets you combine the selectors
No worries about unused selectors
%element-invisible{
position: absolute !important;
height: 1px;
width: 1px;
overflow: hidden;
}
.foo{
@extend element-invisible;
}
.bar{
@extend element-invisible;
}
.foo,
.bar{
position: absolute !important;
height: 1px;
width: 1px;
overflow: hidden;
}
When you use a variable or the output changes
when you’re using the original selector already
when the code is always the same
when you don’t need the original selector
Base themes that support Sass:
Open Terminal and run a short command
gem install sass
YAY! YOU’VE INSTALLED SASS
Sass can compile itself
sass --watch path/sass-directory
So can libraries such as Compass or Bourbon, which can also add much more functionality
gem install compass
compass watch
compass compile
If you're using a task runner like Gulp and Grunt, that will compile too.
Not a Terminal fan? Try Scout or CodeKit
Nesting: can get crazy long and over specific
Versioning: Bundler to keep team members on same gem
Keep output in mind: Using @import to separate out files requires a logical structure
If you write bad CSS, you will write bad Sass
Data type: a way of classifying a piece of information. Common examples in programming are booleans, strings and integers
Control structure: instruction to the computer on how to evaluate a piece of data.
Lists = arrays
Maps = hashes
$social: (
"facebook": '\f204',
"twitter": '\f202',
"linkedin": '\f207',
"pinterest": '\f209',
"github": '\f200',
"dribbble": '\f201',
"instagram": '\f215',
"email": '\f410'
);
And can also be multidimensional
$mega-social: (
"facebook": ( content: "\f204", coords: 0 0 ),
"twitter": ( content: "\f202", coords: 0 -64px ),
"linkedin": ( content: "\f207", coords: 0 -128px )
);
@each $network, $content in $social {
.#{$network} a:before {
content: $content;
}
}
.facebook a:before {
content: '\f204';
}
.twitter a:before {
content: '\f202';
}
.linkedin a:before {
content: '\f207';
}
...