Create an account so you can save your work
Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
I'm Arelia Jones
Tell us about yourself.
$brandColor: #f90000;
$accentColor: #fff;
header {
background-color: $brandColor;
color: $accentColor;
}
header a { color: $accentColor; }
header {
background-color: $brandColor;
color: $accentColor;
a {
color: $accentColor;
}
}
@mixin visually-hidden {
text-indent: 100%;
white-space: nowrap;
overflow: hidden;
}
.image-replace {
@include visually-hidden;
background: url(logo.png) no-repeat;
}
How Sass compiles into CSS
Sass input:
header {
color: black;
nav {
background: red;
a { color: white; }
}
CSS output:
header { color: black; }
header nav { background: red; }
header nav a { color: white; }
Sass input:
nav {
background: red;
a {
color: white;
&:hover { text-decoration: underline; }
}
}
CSS output:
nav { background: red; }
nav a { color: white; }
nav a:hover { text-decoration: underline; }
Stand up and stretch - we'll resume in 5 minutes
Sometimes you want to reuse a value for a style - you use them frequently, they're hard to type or remember, such as
#2a79af
Georgia, Times, "Times New Roman", serif;
1.667em
Variables with Sass let us reuse values more than once, while only defining them in one place
//define using dollar sign
$brandColor: #f90000;
$mainTextcolor: #fff;
$accentColor: #ccc;
To create a variable you need a dollar sign before the name of your variable, and a colon: to give it a value
Note that in Sass files, you can comment out a line with // two slashes
$brandColor: #f90000; // red
$mainTextcolor: #fff; // white
$accentColor: #ccc; // grey
header {
color: $mainTextColor;
background-color: $brandColor;
}
.content {
color: $mainTextColor;
background-color: $accentColor;
}
footer {
color: $accentColor;
background-color: $brandColor;
}
Variables are easy to change if you keep them all in one stylesheet, and update or add to the styles as needed
// Let's define some variables
// Colors
$favoriteColor: #2a79af;
$anotherColor: #f05b62;
// Fonts
$favoriteFont: Papyrus, fantasy;
$aPracticalFont: "Trebuchet MS", "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Tahoma, sans-serif;
// Font sizes
$aNiceBigFontSize: 16px;
$finePrint: 10px;
$giantLogo: 36px;
// Margins and Padding
$defaultMargin: 15px;
$defaultPadding: $defaultMargin;
With CSS you have to be explicit about everything, including numbers. With Sass, you can write math to calculate numbers for you:
+ | Addition |
- | Subtraction |
* | Multiplication |
/ | Division* |
*division is special, check the documentation link for why and how
Sass input:
$layoutWidth: 960px;
#sidebar {
width: $layoutWidth/3;
}
CSS output:
#sidebar {
width: 320px;
}
Sass input:
$layoutWidth: 960px;
$defaultPadding: 16px;
#main {
padding: $defaultPadding;
width: $layoutWidth - $defaultPadding*2;
}
CSS output:
#main {
padding: 16px;
width: 928px;
}
$layoutWidth: 960px;
$navWidth: $layoutWidth/3;
footer {
width: ($layoutWidth - 20px);
}
Color functions are built-in to Sass. They let you alter existing color values. This is the lighten function:
Sass input:
$linkColor: #000;
$linkShadow: lighten(#000, 40%);
a {
color: $linkColor;
text-shadow: $linkShadow;
}
CSS output:
a {
color: #000;
text-shadow: #666;
}
Example of output style
This is the darken function:
Sass input:
$background: #ff0000; // red
$text: darken($background,50%);
body {
color: $text;
background: $background;
}
CSS output:
body {
color: #990000;
background: #ff0000;
}
Example of output style
This is the grayscale function:
Sass input:
$background: #ff0000; // red
$text: darken($background,50%);
body {
background: grayscale(#f00);
color: grayscale(darken(#f00, 50%));
}
CSS output:
body {
background: #000;
color: #808080;
}
Example of output style
lighten(#000, 20%)
darken(#eee, 30%)
grayscale(#2a79af)
saturate(#2a79af, 40%)
invert(#2a79af)
Traditional CSS comments are downloaded by the user. With Sass, we can specify whether comments are left in the final CSS code or are only visible to the developer:
Sass input:
/* Multiline comments will appear
in the final CSS file */
//This single-line comment won't
a { color: blue; }
CSS output:
/* Multiline comments will appear
in the final CSS file */
a { color: blue; }
Mixins are really just a collection of one or more styles that are reusable, like variables are reusable values
Sass input:
@mixin dropshadow($text) {
color: $text;
text-shadow: 2px 4px lighten($text, 50%);
}
p {
@include dropshadow(black);
}
CSS output:
p {
color: black;
text-shadow: 2px 4px #808080;
}
Example of output style
These are especially useful for CSS3 rules that need browser prefixes, like gradients.
@mixin name {
property: value;
}
@mixin example($argument) {
property: value;
property: $argument;
}