SASS is stand for Syntactically Awesome Style Sheets, it is a pre-processor scripting language that is interpreted or compiled into Cascading Style Sheets (CSS). SassScript is the scripting language itself. For more info
Sass consists of two syntaxes.
- SASS (original syntax,
indented syntax
similar to Haml). - SCSS (newer syntax,
Sassy CSS
, uses block formatting like that of CSS.)
- It is a pre-processor, to compile the css from .scss or .sass extension.
- Need to install Sass compiler in computer where you are working click here for installation guide.
- Write a command to watch sass
sass --watch input.scss output.css
.
Variables are used to store the values and use it anytime and everywhere.
<h1>Hello World</h1>
<p>Animals</p>
<ul>
<li>Cat</li>
<li>Dog</li>
</ul>
<a href="#">Click me</a>
// SCSS SYNTAX
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
font: 100% $font-stack;
color: $primary-color;
}
/* CSS */
body {
font: 100% Helvetica, sans-serif;
color: #333;
}
It's a hierarchy based syntax.
// scss
body {
ul {
margin: 0;
padding: 0;
list-style: none;
li { display: inline-block; }
}
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}
/*css*/
body ul {
margin: 0;
padding: 0;
list-style: none;
}
body ul li {
display: inline-block;
}
body a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
- Partial Sass files that contain little snippets of CSS that you can include in other Sass files.
- Underscore
_
should be denoted before first letter when you create partial files. - Use
@use
to import*.scss
file.If @use is NOT working try with @import rule.
- This rule loads another Sass file as a
module
- No need to write the extension of
module
file when importing.
// _variables.scss
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
// section.scss
@use "./_variables";
body {
font: 100% $font-stack;
color: $primary-color;
}
/* CSS */
body {
font: 100% Helvetica, sans-serif;
color: #333;
}
- A mixin lets you make groups of CSS declarations that you want to reuse throughout your site.
- It helps keep your Sass very DRY. Don't Repeat Yourself.
@mixin theme($theme: DarkGray) {
background: $theme;
box-shadow: 0 0 1px rgba($theme, .25);
color: #fff;
}
.info {
@include theme;
}
.alert {
@include theme($theme: DarkRed);
}
.success {
@include theme($theme: DarkGreen);
}
.info {
background: DarkGray;
box-shadow: 0 0 1px rgba(169, 169, 169, 0.25);
color: #fff;
}
.alert {
background: DarkRed;
box-shadow: 0 0 1px rgba(139, 0, 0, 0.25);
color: #fff;
}
.success {
background: DarkGreen;
box-shadow: 0 0 1px rgba(0, 100, 0, 0.25);
color: #fff;
}
- It's
Inherit
the properties fromselectors
. - Use
%
keyword to share the properties. Like example:%flex{ display: flex; }
. - Use
@extend
keyword to extend the shared property. - You cannot extend a nested selector.
- You cannot extend a selector that is wrapped in a media query (which is another reason to do this)
/* This CSS will print because %message-shared is extended. */
%message-shared {
border: 1px solid #ccc;
padding: 10px;
color: #333;
}
// This CSS won't print because %equal-heights is never extended.
%equal-heights {
display: flex;
flex-wrap: wrap;
}
.message {
@extend %message-shared;
}
.success {
@extend %message-shared;
border-color: green;
}
.error {
@extend %message-shared;
border-color: red;
}
.warning {
@extend %message-shared;
border-color: yellow;
}
/* This CSS will print because %message-shared is extended. */
.warning, .error, .success, .message {
border: 1px solid #ccc;
padding: 10px;
color: #333;
}
.success {
border-color: green;
}
.error {
border-color: red;
}
.warning {
border-color: yellow;
}
- We can use Operators/expressions for doing Math calculations.
- They are
+
,-
,*
,math.div()
, and%
.
@use "sass:math";
.container {
display: flex;
}
article[role="main"] {
width: math.div(600px, 960px) * 100%;
}
aside[role="complementary"] {
width: math.div(300px, 960px) * 100%;
margin-left: auto;
}
.container {
display: flex;
}
article[role=main] {
width: 62.5%;
}
aside[role=complementary] {
width: 31.25%;
margin-left: auto;
}
- Write
@media queries
inside a class. - Don't use
css
media queriess instead ofscss
media queries syntax
/* Don't do this */
@media (min-width: $screen-sm-min) {
.my-class {
padding-right: $sc-base-margin;
}
}
/* Do this instead */
.my-class {
@media (min-width: $screen-sm-min) {
padding-right: $sc-base-margin;
}
}
- Use Class names that describe the meaning of the content, rather than its presentation.
a. item-details-sku-container b. header-menu-search-icon c. profile-information-label
- Some guidelines collected from https://sass-lang.com/guide/.