Getting Started With Sass - 2
August 13, 2020
•
10 min read
In the earlier part of this post, I talked about what Sass is and how to get started easily for a beginner programmer. You can find it here. In this section, I assume you have set up your development environment and you are ready to get started coding with Sass.
Sass Variables
Variables are just placeholders. They hold data for future use. Sass Variables are simple and very elegant. You can just assign a value to a variable name: the value can be a color name, hexadecimal value of a color, size in px or rem, virtually any data useful can be stored.
To assign a variable, prefix $
infront of the variable name and that's it. It's that simple.
Sass variables follow the same syntax as assigning a css property to a value {$}{variable-name}: {value};
.
There are no special rules to Sass variables, they must be meaningful and they follow general variable rules. However, -
and _
are considered the same in sass.
For example, $primary-color
and $primary_color
are the same.
CSS also has variables, seperate from the Sass Variables. The two are not the same.
Sass Nesting
When writing CSS, take a situation wherehy you are creating a navbar, with the nav tag and in the nav tag are a list each containing a link. To select a link to apply a style to it, you need to select ll the parent tags(for specificity) to get a style work on a specific tag. Sass makes your life easier. You can just write the code for the tag in the parent. That's nesting
The above CSS can be written more easier in Sass and Sass will compile it to the same CSS above without any change and errors
You can also replace the tags with id
and classes
.
If the property does not apply during nesting, try parent selectors
&
.
The sytax for the parent selectors in nesting is by replacing the selector with the
&
symbol.
In applying nesting on pseudo-classes and pseudo-elements, you can apply the parent selector for much greater specificity. Also, the parent selector copies the parent name for the nested child.
The Sass will compile to CSS like the one below,
To extend a class
name or id
name, you can use the parent selector for that too. Assuming we have a .container
class and we have nested .container-fluid
in the .container
class, since both of the class names begin with .container
and it wont be neceesary to type .container
for the two class names. You can use the parent selector for it. As I said earlier, Sass makes your life much easier.
The compiled CSS will be as below.
Sass Mixins
Sass mixins are just like mixins in Ruby and functions in JavaScript. You can type in reusable blocks of code into the mixin and reuse it over and over again. Mixins can take an argument or not, It is not compulsory.
Note: Mixins are not he same as functions. Sass has functions too and the two are not the same.
The syntax to create a mixin is @mixin <mixin-name> { ... }
or @mixin <mixin-name>(< arguments...> ) { ... }
. To reuse the mixin after it has been created, you can just type @include <mixin-name>
or @include <mixin-name>( < arguments > )
at the place you want to include the mixin then it works. You can include the mixin more than once.
The order in which you arrange the arguments is not important. But when filling our the arguments with the values, it must be in the same order as you specified them, else you can interchange the value of one property with another.
Sass Mixins and Functions are not the same.
Sass Functions
Sass Functions look similiar to mixins but different. In Sass, mixins return a block of code, chunk of code that can be used over and over again. For functions, they are made to return a specific value. Single values, not blocks of code. Functions have a return statement, just like javascript but mixins do not.
The syntax for creating the function is as follows.
To use the function, in a code, you just call the function name and apply the arguments to it.
Functions are mainly used for calculations. For dynamic calculations od sizes, lengths, widths and even colours. Sass also has inbuilt functions to perform some actions.
Sass Modules
Modules are divisions of code into other sections amd partials. It prevents DRY code. Sass implements this system to make it easy to maintain the code and faster to debug. You create a main scss file, then you create a partial file, beginning the file name with _
, then you import the partial into the main file.
The partial can be located anywhere as long as you link the correct path in the main file.
To use a partial in a main file, use @import "<file_name>";
Do not add the _
and the .scss
when using the import. Sass identifies it automatically as long as you have the correct path._
With the above example, Sass will insert the variables
and the resets
partial in a single css file for the brower to render easily.
Sass also applies @use
for importing a module.@use
and @import
are similiar but there are differences.@import
is already a default css functions which makes it sometimes confusing. For@use
, you do not neccesarily insert the whole code but you can access specific functions,mixins or variables in the code.
You can also change the default value in a variable when you import it in the code.
To configure the values in a file,
!default
must be set after it to make it available for configuration.@use
brings a new feature when you can make a variable private to avoid it from being used or modified. To make a variable private,-
or_
must be prefixed to it and it makes private.
Sass Inheritance.
Sass allows you to inherit a block of code in a specific selector. It extends the same code into the new selector.
@extend
is used.@extend <selector>
is the syntax for extending a block into a new one.
The compiled CSS will look like this.
Values extended can be overidden. Just declare the property again with another value and that's it.
Sass is beautiful. Try it out.