CSS to SASS
Why is SASS better?
Sass is basically CSS but with the ability to use variables
A typical CSS rules
Nesting
Sass allows nesting of CSS rules
Mixin
We can group CSS declarations into a Mixin and reuse it.
Instead of repeating this for all elements with a box-shadow, we can define a Mixin. Mixins are like functions for CSS.
A mixin is called with the **@include **directive:
Use @if and @else to Add Logic To Your Styles
The @if directive in Sass is useful to test for a specific case - it works just like the if statement in JavaScript.
Example usage with html:
Use @for to Create a Sass Loop
The @for directive adds styles in a loop, very similar to a for loop in JavaScript.
- start through end: includes the end number as part of the count
- start to end: excludes the end number as part of the count
The above translate the following in CSS:
Example, to create 5 fonts with increasing size:
Use @each to Map Over Items in a List
We can do in this way too
Apply a Style Until a Condition is Met with @while
The @while directive is an option with similar functionality to the JavaScript while loop. It creates CSS rules until a condition is met.
Split Your Styles into Smaller Chunks with Partials
Partials in Sass are separate files that hold segments of CSS code. These are imported and used in other Sass files.
This is a great way to group similar code into a module to keep it organized.
Names for partials start with the underscore (_) character, which tells Sass it is a small segment of CSS and not to convert it into a CSS file.
Also, Sass files end with the .scss file extension.
To bring the code in the partial into another Sass file, use the @import directive.
Extend One Set of CSS Styles to Another Element
Sass has a feature called extend that makes it easy to borrow the CSS rules from one element and build upon them in another.
Sample HTML: