{
    "componentChunkName": "component---src-templates-post-js",
    "path": "/you-must-know-about-sass/",
    "result": {"data":{"markdownRemark":{"html":"<h1 id=\"understanding-sass-css-made-awesome\" style=\"position:relative;\"><a href=\"#understanding-sass-css-made-awesome\" aria-label=\"understanding sass css made awesome permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Understanding Sass: CSS Made Awesome</h1>\n<p>Everything you should know about Sass</p>\n<p>While CSS is getting powerful day by day, it still lacks certain features (such as code reuse) that makes it harder to maintain when the projects get bigger and more complex.</p>\n<p>There are many CSS methodologies (such as OOCSS, SMACSS) that help us write more maintainable CSS code, but these methodologies alone aren’t enough to solve the problem of maintaining large CSS codebases.</p>\n<p>That’s where Sass comes into play. Sass provides us features and tools that regular CSS doesn’t have, using Sass we can write more readable, maintainable and reusable CSS code.</p>\n<p>So in this article, I will dive into what Sass is, how to compile Sass code into regular CSS and discuss it’s features that makes it so powerful.</p>\n<p>Suggestion: Use <a href=\"https://github.com/teambit/bit\">**Bit</a>** to build faster with components. It turns components into reusbale building blocks you can easily share, use and develop from any project. It can help your team share and collaborate on your components.\n<a href=\"https://bit.dev/\"><strong>Component Discovery and Collaboration · Bit</strong>\n*Bit is where developers share components and collaborate to build amazing software together. Discover components shared…*bit.dev</a></p>\n<p>So without further ado, let’s get started :)</p>\n<h2 id=\"what-is-sass\" style=\"position:relative;\"><a href=\"#what-is-sass\" aria-label=\"what is sass permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>What is Sass?</h2>\n<p>If you know CSS or if you are learning CSS, then you must have heard about Sass. Sass is a <strong>CSS preprocessor</strong>, a superset of CSS that adds features that aren’t available in regular CSS.</p>\n<p>A <strong>CSS preprocessor</strong> is a language that extends CSS, adds features to CSS and gets compiled into regular CSS. A CSS preprocessor helps developers to write cleaner and easier to understand CSS code.</p>\n<p>Some well known CSS preprocessors are: <strong>SASS</strong>, <strong>LESS</strong>, and <strong>Stylus</strong>.</p>\n<h3 id=\"sass-or-scss\" style=\"position:relative;\"><a href=\"#sass-or-scss\" aria-label=\"sass or scss permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Sass or SCSS?</h3>\n<p>There are two syntaxes available for Sass. 1) **SCSS **and 2) <strong>Sass</strong></p>\n<ol>\n<li>\n<p>SCSS known as Sassy CSS is a newer and most used version of Sass. It has a similar syntax to CSS because it’s a language extension or a superset of CSS. So a valid CSS is also a valid SCSS. Its file extension is .scss.</p>\n</li>\n<li>\n<p>Sass is an older syntax of Sass. It uses indentation rather than brackets. Its file extension is .sass.</p>\n</li>\n</ol>\n<p>In this article, I will use SCSS syntax of Sass.</p>\n<h2 id=\"installing-sass\" style=\"position:relative;\"><a href=\"#installing-sass\" aria-label=\"installing sass permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Installing Sass</h2>\n<p>Browsers do not understand Sass, in order for browsers to understand the Sass code we write, we have to convert it into regular CSS code. We do this by running Sass compiler which converts our Sass code to into regular CSS code.</p>\n<h3 id=\"getting-started\" style=\"position:relative;\"><a href=\"#getting-started\" aria-label=\"getting started permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Getting Started:</h3>\n<p>Before starting, you must have npm installed on your computer, which comes bundled with <a href=\"https://nodejs.org/en/\">Node.js</a>, you can install it from <a href=\"https://nodejs.org/en/download/\">here</a>.</p>\n<h3 id=\"folder-structure\" style=\"position:relative;\"><a href=\"#folder-structure\" aria-label=\"folder structure permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Folder Structure:</h3>\n<p>Let’s first create the below folder in which we will install Sass.</p>\n<p><img src=\"https://cdn-images-1.medium.com/max/2000/1*WY6tgTbn-ol-W9SwEec-0A.png\" alt=\"Folder Structure\"><em>Folder Structure</em></p>\n<p>You can create the above directories with these commands.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">mkdir sass-demo\ncd sass-demo\nmkdir -p sass css</code></pre></div>\n<h3 id=\"initialize-the-project-directory\" style=\"position:relative;\"><a href=\"#initialize-the-project-directory\" aria-label=\"initialize the project directory permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Initialize the Project Directory:</h3>\n<p>All projects that use the node package manager(npm) must be initialized. To initialize the project enter below command in a terminal. This will create a <strong>package.json</strong> file.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">npm init -y</code></pre></div>\n<h3 id=\"installing-node-sass\" style=\"position:relative;\"><a href=\"#installing-node-sass\" aria-label=\"installing node sass permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Installing node-sass</h3>\n<p>node-sass is a sass compiler. This command will install node-sass as dev dependency.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">npm install node-sass --save-dev</code></pre></div>\n<h3 id=\"compiling-sass-code-to-css\" style=\"position:relative;\"><a href=\"#compiling-sass-code-to-css\" aria-label=\"compiling sass code to css permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Compiling Sass Code to CSS</h3>\n<p>Create an npm script to compile the Sass code into CSS code. Add this script inside the script section of the <strong>package.json</strong> file.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">\"compile-sass\": \"node-sass sass/main.scss css/style.css\"</code></pre></div>\n<p>Here main.scss is our main Sass file, and style.css is the compiled CSS file.</p>\n<p>We can also add a --watch flag. The watch flag tells Sass compiler to watch your source files for changes, and re-compile CSS each time you save your Sass files. For example:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">\"compile-sass\": \"node-sass sass/main.scss css/style.css --watch\"</code></pre></div>\n<p>Finally, we can compile our Sass code into CSS by running below command in the terminal.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">npm run compile-sass</code></pre></div>\n<h2 id=\"sass-features\" style=\"position:relative;\"><a href=\"#sass-features\" aria-label=\"sass features permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Sass Features</h2>\n<p>Now that we know what Sass is, and how to install Sass, let’s dive into various features of Sass that makes it so powerful.</p>\n<h2 id=\"1-variables\" style=\"position:relative;\"><a href=\"#1-variables\" aria-label=\"1 variables permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>1# Variables</h2>\n<p>Variables are one of the most useful features of Sass. Variables allow us to define certain values and reuse them across the code. Variables in Sass are similar to variables in JavaScript. In Sass, variables always start with a $.</p>\n<p>For example:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$variable-name: variable-value;</code></pre></div>\n<p>Let’s take a look at below HTML:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">&lt;div class=\"heading\">\n  &lt;h1 class=\"heading-primary\">This is a primary heading&lt;/h1>\n  &lt;h2 class=\"heading-secondary\">This is a secondary heading&lt;/h2>\n&lt;/div></code></pre></div>\n<p>Let’s define two variables for primary and secondary colors:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$color-primary: #55c57a;\n$color-secondary: #7ed56f;</code></pre></div>\n<p>Let’s style the above headings in Sass:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.heading-primary {\n  color: $color-primary;\n  text-align: center;\n  margin-top: 10px;\n}\n\n.heading-secondary {\n  color: $color-secondary;\n  text-align: center;\n  margin-top: 15px;\n}</code></pre></div>\n<p>The above code will be compiled into below CSS code:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.heading-primary {\n  color: #55c57a;\n  text-align: center;\n  margin-top: 10px;\n}\n\n.heading-secondary {\n  color: #7ed56f;\n  text-align: center;\n  margin-top: 15px;\n}</code></pre></div>\n<p>After compiling, the variables will be replaced by their actual values in CSS.</p>\n<h2 id=\"2-nesting\" style=\"position:relative;\"><a href=\"#2-nesting\" aria-label=\"2 nesting permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>2# Nesting</h2>\n<p>Nesting is another most useful feature of Sass. CSS doesn’t support selector nesting. Using nesting, we can nest child selectors inside the parent selector.</p>\n<p>For example, consider the below HTML:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">&lt;nav class=\"navigation\">\n  &lt;ul>\n    &lt;li>Home&lt;/li>\n    &lt;li>About&lt;/li>\n    &lt;li>Contact&lt;/li>\n  &lt;/ul>\n&lt;/nav></code></pre></div>\n<p>Without Sass, we’d have to write the following CSS code:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.navigation {\n  background-color: yellow;\n  padding: 20px;\n}\n\n.navigation ul {\n  list-style: none;\n}\n\n.navigation li {\n  text-align: center;\n  margin-top: 20px;\n}</code></pre></div>\n<p>Notice that we have to repeat the navigation class name every time we want to style its children. With Sass, we can write cleaner and less repetitive code. For example:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.navigation {\n  background-color: yellow;\n  padding: 20px;\n\n  ul {\n    list-style: none;\n  }\n\n  li {\n    text-align: center;\n    margin-top: 20px;\n  }\n}</code></pre></div>\n<h2 id=\"3-mixins\" style=\"position:relative;\"><a href=\"#3-mixins\" aria-label=\"3 mixins permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>3# Mixins</h2>\n<p>Mixins are yet another powerful feature of Sass. Using mixins, we can reduce code redundancy and increase code reuse. A mixin is a reusable piece of code which is similar to functions. For example, we can define a mixin as following:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">@mixin mixin_name() {\n  /* code */\n}</code></pre></div>\n<p>Let’s take a look at below HTML:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">&lt;div class=\"box\">\n  &lt;div>\n  &lt;/div>\n&lt;/div></code></pre></div>\n<p>Sass code:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.box {\n  background-color: red;\n  width: 100%;\n  height: 100vh;\n  position: relative;\n  \n  div {\n    width: 200px;\n    height: 200px;\n    background-color: blue;\n    position: absolute;\n    top: 50%;\n    left: 50%;\n    transform: translate(-50%, -50%);\n  }\n}</code></pre></div>\n<p>Here we are positioning the inner div at the center of the outer div. We can place the code inside a mixin called absCenterPosition. For example:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">@mixin absCenterPosition() {\n  position: absolute;\n  top: 50%;\n  left: 50%;\n  transform: translate(-50%, -50%);\n}</code></pre></div>\n<p>We can include the above mixin in our code by using @include directive, for example:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.box {\n  background-color: red;\n  width: 100%;\n  height: 100vh;\n  position: relative;\n  \n  div {\n    width: 200px;\n    height: 200px;\n    background-color: blue;\n    @include absCenterPosition;\n  }\n}</code></pre></div>\n<p>We can also pass arguments to the mixins. For example:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">@mixin box-shadow($x, $y, $blur, $spread) {\n  -webkit-box-shadow: $x $y $blur $spread;\n  -moz-box-shadow: $x $y $blur $spread;\n  -ms-box-shadow: $x $y $blur $spread;\n  box-shadow: $x $y $blur $spread;\n}</code></pre></div>\n<p>We can include the mixin as:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.box {\n  background-color: red;\n  width: 100%;\n  height: 100vh;\n  position: relative;\n  \n  div {\n    width: 200px;\n    height: 200px;\n    background-color: blue;\n    @include absCenterPosition;\n    *@include* box-shadow(0, 5px, 5px, rgba(0, 0, 0, 0.7));\n  }\n}</code></pre></div>\n<h2 id=\"4-functions\" style=\"position:relative;\"><a href=\"#4-functions\" aria-label=\"4 functions permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>4# Functions</h2>\n<p>Functions in Sass are similar to functions in JavaScript. Just like JavaScript, functions in Sass can accept arguments and return some value. For example:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">@function divide($a, $b) {\n  @return $a / $b;\n}\n\ndiv {\n  margin: divide(60, 2) * 1px;\n  height: 200px;\n  width: 200px;\n}</code></pre></div>\n<h2 id=\"5-partials-and-import\" style=\"position:relative;\"><a href=\"#5-partials-and-import\" aria-label=\"5 partials and import permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>5# Partials and Import</h2>\n<p>Partials in Sass are separate files containing Sass code that make our code modular. Using partials, we can have separate Sass files for different components.</p>\n<p>A partial’s name always starts with an underscore(_). We use an @import directive to import partial into another Sass file. For example, we can import the _header.scss file into main.scss file.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">// main.scss\n@import 'header';</code></pre></div>\n<p><em>When importing a partial, we can omit the underscore(_) and .scss file extension.</em></p>\n<p>Unlike CSS imports, Sass imports don’t send another HTTP request for fetching the partial, because during compiling all the imports are placed in a single CSS file.</p>\n<h2 id=\"7-inheritanceextend\" style=\"position:relative;\"><a href=\"#7-inheritanceextend\" aria-label=\"7 inheritanceextend permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>#7 Inheritance/Extend</h2>\n<p>In Sass, we can extend CSS properties from one selector to another. For this, we use the @extend directive.</p>\n<p>Generally, for inheritance we use placeholder classes which starts with a %, the advantage of using placeholder classes is that it only prints in compiled CSS when it is extended, and can help keep your compiled CSS neat and clean.</p>\n<p>For example:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">%panel {\n  background-color: blue;\n  border: 2px solid red;\n}\n\n.small-panel {\n  @extend %panel;\n  width: 70px;\n  height: 70px;\n}\n\n.big-panel {\n  @extend %panel;\n  width: 200px;\n  height: 200px;\n}</code></pre></div>\n<p>This will be compiled into following CSS.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.small-panel,\n.big-panel {\n  background-color: blue;\n  border: 2px solid red;\n}\n\n.small-panel {\n  width: 70px;\n  height: 70px;\n}\n\n.big-panel {\n  width: 200px;\n  height: 200px;\n}</code></pre></div>\n<h2 id=\"6-ampersand--operator\" style=\"position:relative;\"><a href=\"#6-ampersand--operator\" aria-label=\"6 ampersand  operator permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>6# Ampersand (&#x26;) operator</h2>\n<p>The ampersand (&#x26;) operator is another useful feature of Sass. This operator is particularly useful when you are using <a href=\"https://css-tricks.com/bem-101/\">BEM</a> methodology.</p>\n<p>Let’s take a look at below HTML:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">&lt;button class=\"btn btn--green\">Hello World&lt;/button></code></pre></div>\n<p>If we have to style it without the &#x26; operator, we’d do something like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.btn {\n  display: inline-block;\n  padding: 5px 8px;\n}\n\n.btn--green {\n  background-color: green;\n}\n\n.btn:hover {\n  background-color: transparent;\n}</code></pre></div>\n<p>But we &#x26; operator we can do it like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.btn {\n  display: inline-block;\n  padding: 5px 8px;\n\n  &amp;--green {\n    background-color: green;\n  }\n\n  &amp;:hover {\n    background-color: transparent;\n  }\n}</code></pre></div>\n<p>So using &#x26; operator, we can nest selectors that share the same name, and during compile time the &#x26; operator will be replaced by its enclosing selector name.</p>\n<h2 id=\"5-directives\" style=\"position:relative;\"><a href=\"#5-directives\" aria-label=\"5 directives permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>5# Directives</h2>\n<p>There are many directives in Sass. For example, @extend, @mixin, and @import are some of the directives that we have seen above. There are many more directives in Sass, such as @if, @else, @for, @while.</p>\n<h3 id=\"if-and-else-directives\" style=\"position:relative;\"><a href=\"#if-and-else-directives\" aria-label=\"if and else directives permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>@if and @else directives</h3>\n<p>The @if and @else directives are similar to if and else statements in JavaScript. For example:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">@mixin text-color($val) {\n  @if $val == error {\n    color: red;\n  }\n  @else if $val == warning {\n    color: yellow;\n  }\n  @else if $val == success {\n    color: green;\n  }\n  @else {\n    color: black\n  }\n}\n\n.heading {\n  @include text-color(error);\n}</code></pre></div>\n<p>Here, we are using a mixin text-color which accepts an argument $val. We can have a different color for the heading depending on the value passed to the mixin.</p>\n<h3 id=\"for-and-while-directive\" style=\"position:relative;\"><a href=\"#for-and-while-directive\" aria-label=\"for and while directive permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>@for and @while directive</h3>\n<p>The @for and @while directives are similar to for and while loops in JavaScript. For example:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">@for $i from 1 through 4{\n  .col-#{$i} {\n     width: 100% / $i;\n  }\n}</code></pre></div>\n<p>We use #{} to use variables inside a selector name and other CSS functions. This will be compiled into following CSS.</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.col-1 {\n  width: 100%; \n}\n\n.col-2 {\n  width: 50%; \n}\n\n.col-3 {\n  width: 33.33333%; \n}\n\n.col-4 {\n  width: 25%; \n}</code></pre></div>\n<p>We can also implement the above code using the @while directive. For example:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">$i: 1;\n@while $i &lt; 5 {\n  .col-#{$i} {\n     width: 100% / $i;\n  }\n  $i: $i + 1;\n}</code></pre></div>\n<p>This will be compiled into following CSS:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">.col-1 {\n  width: 100%; \n}\n\n.col-2 {\n  width: 50%; \n}\n\n.col-3 {\n  width: 33.33333%; \n}\n\n.col-4 {\n  width: 25%; \n}</code></pre></div>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>We have learned what Sass is, how to install it locally and discussed it’s features that makes it so powerful. Sass adds basic programming capabilities to CSS.</p>\n<p>Using Sass, we can perform basic calculations, reuse code using mixins, and create functions. Sass makes maintenance of CSS so much easier in bigger projects.</p>\n<p>That’s it and if you found this article helpful, please click the clap 👏button, you can also follow me on <a href=\"https://medium.com/@tkssharma\">Medium</a> and <a href=\"https://twitter.com/tkssharma\">Twitter</a>, and if you have any doubt, feel free to comment! I’d be happy to help :)</p>","excerpt":"Understanding Sass: CSS Made Awesome Everything you should know about Sass While CSS is getting powerful day by day, it still lacks certain…","fields":{"slug":"/you-must-know-about-sass/"},"frontmatter":{"title":"Everything you should know about Sass","date":"June 01, 2022","tags":["web-design","sass","css"],"description":"Everything you should know about Sass"}}},"pageContext":{"slug":"/you-must-know-about-sass/","previous":null,"next":{"id":"fdeed646-8e02-5f4d-b1e7-20109f4a61db","frontmatter":{"title":"Using React Hooks for Your Forms with Validation","tags":["reactjs","javascript"],"categories":["reactjs","javascript"],"template":"post"},"fields":{"slug":"/using-react-hook-forms-for-validation/"}}}},
    "staticQueryHashes": ["352937542"]}