If you are using a screen reader or assistive device, you will most likely find the Print version of this slide set more enjoyable to follow.
Learn how to use Sass to make your CSS styles modular, extendable, and better prepared to fit into modern web site development.
space
- move to next slide (over or down)shift+space
- move to previous slide (back or up)o
- the overview modeGirl Develop It is here to provide affordable and accessible programs to learn software through mentorship and hands-on instruction.
Some Rules:
Tamara Temple
In the GDIMpls Slack,
join the #html-css-classes
channel.
README.md
file on Github.
Estimating 10 minutes
High Five!
Sass is a command-line tool
that pre-processes your CSS files
allows you to create:
Sass has two dialects:
Indented Form:
stripped down, white-space indented, terse;
does not look like CSS (called “Sass”)
Sassy Form:
keeps all the braces (“squirly brackets”) and
semi-colons that you have in CSS.
White space doesn’t
matter. (called “SCSS”)
Plain CSS is already SCSS, it’s the easiest to start with.
For this course, I’ll be showing you the SCSS dialect.
The printed notes show the Sass version as well.
styles.scss
becomes styles.css
.This last is really useful for building distributions for your site.
Start with SCSS:
Get CSS!
Sass let’s you set variables that can hold values used all over your css files, allowing you to quick change a commonly used value in only one place.
See the Pen Sass does Variables by Tamara Temple (@tamouse) on CodePen.
Start with SCSS:
Sass
Get CSS!
Sass allows the nesting of CSS so you can clearly tell which rules apply to which parent, without endless repetition.
See the Pen Sass does Nesting by Tamara Temple (@tamouse) on CodePen.
Start with SCSS:
or Sass
Get CSS!
Mixins let you specify often repeated style specification that are used in several independent styles. You could consider a mixin like a macro, where the mixin’s content gets included in the style specification.
See the Pen Stylin With Sass: Mixins by Tamara Temple (@tamouse) on CodePen.
Start with SCSS
Sass
Get CSS
You can split out your Sass / SCSS declarations into several files to help you keep them all organized. Beginning the file name with the underscore character (‘_’) will tell the Sass preprocessor to ignore the file because it’s being imported and doesn’t need to have it’s own translated CSS file.
Start with SCSS
Or Sass
Get CSS!
Math operations include:
See the Pen Stylin With Sass: Math by Tamara Temple (@tamouse) on CodePen.
Start with SCSS
Or Sass
Get CSS!
Color transformations include:
See the Pen Stylin With Sass: Colours! by Tamara Temple (@tamouse) on CodePen.
There are two types of comments
/* */
// ...
Start with SCSS
or Sass
End with CSS
Start with SCSS
or Sass
Get CSS!
See the Pen Stylin With Sass: Loops! by Tamara Temple (@tamouse) on CodePen.
Start with SCSS:
or Sass:
Get CSS!
See the Pen Stylin With Sass: Branches by Tamara Temple (@tamouse) on CodePen.
Sass needs to know where to find the files it should compile, and also where to write the compiled files.
There is no magic to this, and the little watch script is telling Sass exactly where to pick things up and where to put them down.
The concept of a sass/
source folder and a css/
destination folder
is very common, but you may find other ways to do this. If you do,
your tooling will need to reflect this.
If you look in the gulpfile.js
file that is being used by
npm start
, you can find the area where the Sass files are being
compiled. Line 11 contains:
which creates the task to compile Sass files. Lines 12 through 16 specify where to look for such files.
Line 22 specifies where to write them.
Since Sass is a pre-compiler, it needs to run before you open your files in the browser (and it will need to be run before you release your files to the public). This will always require some sort of manual or automated step. Using the watcher program here, which is called Gulp, is one way people do this in an automated way. There are several other ways as well.
This class is not concerned with such tooling, but it is an important part of using Sass.
You can reuse this setup in other projects by copying the
package.json
and gulpfile.js
files, and recreating the structure
of the project.
Checkout the exercise 1 branch:
Follow the exercise instructions in the file
README.md
Sass extends the @import
function from CSS to create the concept of
a partial.
A partial is a segment of Sass code in a file with a name that begins
with an underscore (_
). Such files are treated differently by Sass:
if a file starts with an underscore, do not compile a separate
.css
file for it.
otherwise, compile a .css
file
Source File Name | Destination File Name |
---|---|
styles.scss | styles.css |
_grid.scss | —— |
When you import the partial, you leave off the leading underscore.
Sass knows how to find the right file.
Stop the watcher (Ctrl-C)
Save your changes and checkout exercise 2:
Restart the watcher:
Follow the instructions in the README
file
Issue:
How to organize the CSS for
your website in a way that is:
Problem:
How to ensure proper specificity
when creating the CSS.
Managing CSS Projects with ITCSS by Harry Roberts, CSS Wizardry
“Inverted Triangle CSS”
Ctrl+C
git add --all && git
commit -m done
git checkout -b ex.03.start origin/ex.03.start
README.md
filenpm start
We’ll be doing a lot of refactoring over the next few Let’s Develp It! sections:
Cntl+C
git add --all && git commit -m done
git checkout -b ex.NN.start origin/ex.NN.start
npm start
README
fileex.04.start
README.md
using elements as styling selectors breaks the notion of general to explicit specificity. It means you have to design around their use in other contexts.
IDs can be used, but don’t use them for CSS; use them for JavaScript and in-page references.
In this exercise, we’ll extract our first two objects:
Checkout:
ex.05.start
README.md
Follow along on the printed notes
“BEM” - Block, Element, Modifier
A functionally independent page component that can be reused.
The block’s name describes its purpose, not its state.
The block shouldn’t influence its environment, meaning you shouldn’t set the external geometry (padding or boundaries that affect the size) or positioning for the block.
Blocks can be nested inside each other, and there can be any number of nesting levels.
A composite part of a block that can’t be used separately from it.
Again, the element’s name describes its purpose, not its state.
The structure of the element name is:
block-name__element-name
The defining part of the element name is the double underscore (__
)
separating the block name from the element name.
Elements can be nested inside each other, and you can have any number of nesting levels. But, and element is always part of a block; you cannot define a hierarchy of elements.
Thus something like block__elem1__elem2
would be considered
incorrect.
An entity that defines the appearance, state, or behaviour of a block or element
The modifer is where the majority of the style settings will end up,
usually. The naming convention varies for modules, a lot of people
seem to use the double-dash convention (--
): block_name--size
.
Using double-dash modifiers.
Let’s see this done in SCSS:
And in Sass:
ex.06.start
README.md
Whew!! That was a tough one.
Give yourselves a hand.
ex.07.start
README.md
How did that go?
ex.08.start
README.md
Yay?
We’ve been going pretty steady, but now we’re about to dive into more complex things, and stuff that requires some careful thought.
<section>
elements.(Including interior components)
ex.09.start
Way to go!
Now we’re gonna have fun!
ex.10.start
Awesome!
ex.11.start
Whew! We made it.
No, you don’t.
@import
works in plain old CSSbut you still probably want to
Something new that’s being introduced are CSS variables.
Please fill out the workshop survey at: tinyurl.com/GDIMpls
Use “Stylin With Sass” as the class name