This activity will serve to familiarize us with some of the features of the Sass CSS precompiler. We'll look at variables, partials, mixins, and functions
OK!With SCSS, the // comments don't copy over to the final CSS file while the /* */ do. So, we're using the double slash in our SCSS files to localize them there
What this does is tell Sass to watch your scss folder for changes. The syntax is
sass --watch source_folder:destination_folder
When you modify a .scss file, Sass will recompile it into your CSS folder. We could, and in the next lab, will, I think, keep all of the files in a single folder, say, for example, the css folder. Then the command would be:
sass --watch css:css
But I like to keep them in separate folders for the sake of (what I think is) easier maintenance
Take a look at the command line display. Nothing's happened? But Sass is supposed to watch for changes in .scss files. What gives?
The first line imports the partial, _variables.scss, into the main.scss file. When we're importing partials, just the name of the file is required - leave out the underscore and the file extension
Notice also that a { ... } is nested inside the body { ... } selector and a:hover{ ... } is, in turn, nested within the a { ... } selector. With respect to a:hover, remember that the ampersand is equivalent to this, a self-reference
Turns out, the @import directive is now deprecated. The folks at sass-lang.com recommend using @use instead. The as * part is for defining namespaces. If you have a bunch of files you're importing, defining namespaces for them protects you from problems that would arise out of having, for example, duplicate class names in different files. We're not going that far with this lab, obviously, hence the as *
You may have noticed that we included a bunch more variables and classes in _variables.scss & main.scss than we actually ended up using. What I'm trying to demonstrate here is extensibility - we could use this code for future projects, which may benefit from the other variables and classes. So, when I make a class, for example, to define font size as large (1.6em), I go ahead and create additional classes for small (1em), medium (1.2em), and extra-large (2em). This keeps the class names consistent and gives me additional classes I can use in the future, if I need them
Every time you make a change to one of your scss files, the command window will log the fact and notify you that a new CSS file has been compiled. It will also provide the line number of the first error it encounters, if any
What happens is this (and I had a bit of a problem adjusting to it when I first started using Sass): If you edit your .CSS file, it'll work. But when you make any changes to the SCSS file later on, the changes you just made will be overwritten by Sass. You'll see my personal fix below, but real quick, I started saving my CSS files in compressed format, which reminds me not to mess with them directly
Real quick... return to your command window. Type Ctrl+c (you may have to do it twice). When it asks you if you want to exit, say yes. Then repeat the command (hit the ↑ key), but add --style expanded to the end before hitting Enter
What the heck do you think this means?
If you haven't already heard me [pendantically] preaching about this yet: The reason that we compress CSS and JS files like this before deploying them to production is that, while browsers ignore extra white space in source files, they still have to parse it out when the page loads. This takes a little time. BUT! But on a high-traffic production server, that 'little time' adds up and can affect load times. Negatively. Which. You don't want. So, we leave all the whitespace in while developing the site to help with readability and troubleshooting. Then, right before deployment, we minify the file to get rid of the extra white space. The browser doesn't care; but our users will
This is one of Sass's built-in functions, which will modify the color of an element. Sass has a number of built in functions which further simplify the task of creating CSS
your_id.csci1720.net/labs/lab6_sass1/
. For example: ramseyjw.csci1720.net/labs/lab6_sass1/
Remember, since our landing page is named index.html, we don't have to specify the file name in the URL - The browser will automagically load it
Have a nice day 😊