Girl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some "rules"
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;
}
}
%visually-hidden {
text-indent: 100%;
white-space: nowrap;
overflow: hidden;
}
.image-replace {
@extend %visually-hidden;
background: url(logo.png) no-repeat;
}
How Sass compiles into CSS
$ cd ../
Go to your home directory:
$ cd /
Go to a specific folder:
$ cd Users/marcy/Sites/gdi-sass
List files in a directory:
$ ls
ruby -v
Now that Ruby is installed, we can install the Sass gem.
In Terminal or Git Bash:
gem install sass
We need to structure our stylesheets before we can compile them.
Check out your index.html file in a browser. Looks funky, yes?
We need to compile our .scss (Sass) files to make the CSS work in the browser.
First, navigate via the command line to your /stylesheets directory in the "practice" folder.
Then type:
$ sass --watch scss:css
Let's break this command down - you're going to use it *a lot*!
$ sass --watch scss:css
--watch tells Sass to look for changes to our .scss files, and to compile them to css if they have updates.
The scss bit is the folder where our .scss files live. We edit these files only.
The css part is the folder where our .css files will be. These files are the compiled output of our Sass, and are only created when we run the above command.
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;
@import "_utilities";
You can download tools to highlight your Sass properly in Sublime Text:
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:
|
CSS output:
Example of output style |
This is the darken function:
Sass input:
|
CSS output:
Example of output style |
This is the grayscale function:
Sass input:
|
CSS output:
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:
|
CSS output:
|
Mixins are really just a collection of one or more styles that are reusable, like variables are reusable values
Sass input:
|
CSS output:
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;
}