SASS
Why SASS?
SASS is an improvement on CSS in that there are methods of abstraction. It is a stylesheet language that is compiled into CSS, which means that Sass will translate the Sass code you wrote into CSS, which is what your web browser can read.
SASS vs SCSS
As you learn about Sass, you might notice something called Scss. They are basically the same thing except that Scss uses curly braces and semicolons to distinguish between lines. Sass uses indentation and newlines instead.
We will be teaching the Scss syntax because it is more commonly used.
Getting started
A easy way to write SASS and have it preprocessed into CSS is by using a Jekyll powered website, such as GitHub pages or Fastpages.
The first step is to clone a GitHub pages repo, such as this one.
Within the repository, head over to assets/css/
, and open style.scss
.
This is where you can create your SASS code.
To see your CSS-translated SASS code, head over to _site/assets/css/style.css
Note: You will need to run bundle exec jekyll serve before the _site directory appears.
The first few hundred lines are used to style Github's theme. Make sure to scroll to the very bottom to see the SASS code that you wrote, which is in the form of CSS.
Nesting
- Nesting allows for more concise and organized Sass code.
- Avoid excessive nesting to prevent overly specific and hard-to-maintain CSS output.
- Use the ampersand (&) to reference the parent selector inside a nested selector for dynamic and flexible styles.
- Utilize Sass's parent selector (@at-root) to break out of nested rules and output styles at the root level.
- Consider using Sass linters or linting plugins to check for proper nesting syntax and best practices.
Extend/Inheritance
What are some similarities that the buttons share? What are the differences?
- n CSS, you would need to copy and paste the code each time, while in SASS, you can use @extend to inherit the code.
- The buttons have the same width and height, font color, and spacing between each button. They have a different background color.
With SASS, we can create a placeholder class that stores the code we want to reuse. A placeholder class looks like this:
%class-name{
}
For example, to specify the width, height, font color, and spacing for each button, we can create a placeholder class called %buttonLayout and write our styling in there.
%buttonLayout {
width: 15em;
height: 15em;
color: #ffffff;
margin-right: 10%;
}
A selector needs to be created for each button. Code from the placeholder class can be called with @extend %class-name
.gettingStartedButton, .nestingButton, .extendButton {
@extend %buttonLayout;
}
Mixin
- Buttons were given a background using background: radial-gradient().
- Another approach for coding the background is through the use of a mixin.
- A mixin is similar to extend, as it creates a reusable code template.
- Mixins can also accept parameters for dynamic styling.
- In the example of the buttons, all three buttons have a gradient background, but with different background colors.
Mini-hack
Write out a mixin in SASS that takes in a color and a font size as the parameter. Within the mixin, set the background color and font color to the color parameter, and set the font size to the font size parameter. Then create a selector that calls the mixin, and pass in a color and font size of your choice as the arguments.
Import
- There is a way to split your code into multiple files and import them into one file.
For instance, to put the styling for function.html in another SASS file, first create a directory called _sass.
Within the directory, create another SASS file. In this case, the file is called functionStyle.scss
SASS Hacks
Take notes and complete the mini-hacks. (0.9)
Complete the quiz questions and provide your answers in this notebook. (0.9)
Use SASS to create something that uses either extend or mixin. (0.9)
Extra credit: Research other SASS features and blog about what you learned or add to your SASS project with any extra features not covered in this lesson. More points will be given if both are done.
What is SASS? b. A scripting language that has many styling operations
What is the difference between SASS and SCSS? a. They are very similar in their function, but their syntax is slightly different
What is an example of an advantage of using SASS over just CSS? a. SASS has more functions than CSS
What does SASS stand for? a. Systematically Arranged Sample Sheets
Which of the following is NOT an example of an available SASS directive? d. compute
The __ directive is used to share rules and relationships between selectors. b. extend
What is “@___” called? b. Directive