<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Darius Njihia</title>
    <description>The latest articles on DEV Community by Darius Njihia (@kihuha).</description>
    <link>https://dev.to/kihuha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F274047%2F46004a38-3a84-44b6-a7d3-cd41bd92f581.jpeg</url>
      <title>DEV Community: Darius Njihia</title>
      <link>https://dev.to/kihuha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kihuha"/>
    <language>en</language>
    <item>
      <title>🏊🏽‍♂️Dive into Sass/SCSS - A PRACTICAL approach - Part Two </title>
      <dc:creator>Darius Njihia</dc:creator>
      <pubDate>Sat, 25 Apr 2020 18:29:04 +0000</pubDate>
      <link>https://dev.to/kihuha/dive-into-sass-scss-a-practical-approach-part-two-4h4f</link>
      <guid>https://dev.to/kihuha/dive-into-sass-scss-a-practical-approach-part-two-4h4f</guid>
      <description>&lt;h2&gt;
  
  
  Recap
&lt;/h2&gt;

&lt;p&gt;Part One - &lt;a href="https://dev.to/kihuha/sass-for-beginners-a-practical-approach-1m3b"&gt;https://dev.to/kihuha/sass-for-beginners-a-practical-approach-1m3b&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We first learnt about the difference between .sass and .scss, learnt how to define a variable and how to separate your styles into different files for separation of concerns.&lt;/p&gt;

&lt;p&gt;Let's continue &lt;/p&gt;

&lt;h2&gt;
  
  
  3. SASS to CSS
&lt;/h2&gt;

&lt;p&gt;There are two ways of converting your .scss files to CSS. &lt;/p&gt;

&lt;h3&gt;
  
  
  3. a) Using an application
&lt;/h3&gt;

&lt;p&gt;For those of you who prefer graphical user interfaces, both paid and free applications can be used. You can check out the following free to use applications. They are fairly straightforward to use and sufficient documentation is available in the apps' homepages&lt;/p&gt;

&lt;p&gt;FREE APPS&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Koala =&amp;gt; &lt;a href="http://koala-app.com/"&gt;http://koala-app.com/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Scout App =&amp;gt; &lt;a href="https://scout-app.io/"&gt;https://scout-app.io/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. b) Using the command line
&lt;/h3&gt;

&lt;p&gt;To use the command-line tool, you need to ensure that you have at least either of the following tools pre-installed&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Node =&amp;gt; &lt;a href="https://nodejs.org/en/"&gt;https://nodejs.org/en/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Choco for windows =&amp;gt; &lt;a href="https://chocolatey.org/"&gt;https://chocolatey.org/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Brew for Mac OSX  =&amp;gt; &lt;a href="https://brew.sh/"&gt;https://brew.sh/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you have either of the tools installed globally, you can install sass by running:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;npm install -g sass&lt;/code&gt; for Node&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;choco install sass&lt;/code&gt; for windows&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;brew install sass/sass/sass&lt;/code&gt; for Mac OS X&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Confirm your installation by running:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sass --version&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To convert the .scss/.sass files to CSS, run the command listed below specifying the source location and the intended destination for the compiled CSS&lt;/p&gt;

&lt;p&gt;&lt;code&gt;sass [source-file-location]/main.scss [destination-location]/index.css&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The above command will look for the .main.scss file and transform it into plain CSS then save it in the directory specified as the destination and under the name specified in the command&lt;/p&gt;

&lt;h3&gt;
  
  
  3. c) Using Webpack - For more advanced users
&lt;/h3&gt;

&lt;p&gt;Webpack is a module bundling tool -&amp;gt; it analyses your project for dependencies within files and bundles them to produce a single file. For more info, read through the documentation at &lt;a href="https://webpack.js.org/"&gt;https://webpack.js.org/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Webpack allows you to convert .scss files to .css files and also bundles them together to produce a single .css file. As a developer, you can define this process in the webpack.config.js file by specifying the loaders that will process the file.&lt;/p&gt;

&lt;p&gt;A typical webpack.config.js file looks like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
    entry: 'index.js',
    output: {
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist'),
    },
    module: {
        rules: [
            {
                test: /\.js/,
                use: 'babel-loader',
                exclude: /node_modules/,
            },
            {
                test: /\.scss/,
                loader: [
                    'css-loader',
                    'sass-loader'
                ]
            },
        ],
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The file above defines three main keys - entry, output and module. Pay close attention to the &lt;code&gt;module&lt;/code&gt; object. This is where we define loaders to handle the various type of files we have in our project. The second item in the rules is where we describe a regex to test against the files encountered by webpack. &lt;/p&gt;

&lt;p&gt;Whenever it encounters a .scss file, it will use the loaders inside the loader array to transform the file from one form to another starting with the loader in the last index of the array (i.e. the sass-loader for our case).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    test: /\.scss/,
    loader: [
        'css-loader',   --&amp;gt; This transforms the result of the sass-loader
        'sass-loader'   --&amp;gt; This transforms the file first
    ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the transformation has been done, you may &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Insert it into the HTML page  as a &lt;/li&gt;
&lt;li&gt;Output the final transformed css file to the project directory&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For option 1, include an additional loader (&lt;code&gt;style-loader&lt;/code&gt;) to insert the file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    test: /\.scss/,
    loader: [
        'style-loader'  --&amp;gt; Inserts css to the HTML &amp;lt;style&amp;gt; tag
        'css-loader',   --&amp;gt; This transforms the result of the sass-loader
        'sass-loader'   --&amp;gt; This transforms the file first
    ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For Option 2, you need to install an additional plugin (mini-css-extract-plugin) to handle the outputting for us. mini-css-extract-plugin also comes with a loader which is added to the loaded array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const MiniCssExtractPlugin = require('mini-css-extract-plugin')
module.exports = {
...
...
{
    test: /\.scss/,
    loader: [
        MiniCssExtractPlugin.loader,  --&amp;gt; outputs css file to a directory
        'css-loader',  
        'sass-loader'   
    ],
    plugin: [
        new MiniCssExtractPlugin()  --&amp;gt; Specify the plugin instance here
    ]
}
...
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For more info on the mini-css-extract-plugin plugin, start here - &lt;a href="https://webpack.js.org/plugins/mini-css-extract-plugin/"&gt;https://webpack.js.org/plugins/mini-css-extract-plugin/&lt;/a&gt; or go to their github repo on &lt;a href="https://github.com/webpack-contrib/mini-css-extract-plugin"&gt;https://github.com/webpack-contrib/mini-css-extract-plugin&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;RECAP&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You can use a GUI application to transform .scss files to .css&lt;/li&gt;
&lt;li&gt;There is a node package called 'sass' that can compile your .scss files for you by specifying the right commands on a terminal&lt;/li&gt;
&lt;li&gt;Alternatively, webpack can be used to compile and 
a) insert your css to an HTML file
b) output your css as a .css file&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>css</category>
      <category>scss</category>
      <category>sass</category>
      <category>webpack</category>
    </item>
    <item>
      <title>🏊🏽‍♂️Dive into Sass/SCSS - A PRACTICAL approach</title>
      <dc:creator>Darius Njihia</dc:creator>
      <pubDate>Fri, 13 Dec 2019 20:51:08 +0000</pubDate>
      <link>https://dev.to/kihuha/sass-for-beginners-a-practical-approach-1m3b</link>
      <guid>https://dev.to/kihuha/sass-for-beginners-a-practical-approach-1m3b</guid>
      <description>&lt;h2&gt;
  
  
  Intro to Sass/SCSS  - PART ONE
&lt;/h2&gt;

&lt;p&gt;SASS - Syntactically Awesome Style Sheets&lt;/p&gt;

&lt;p&gt;Sass is a CSS preprocessor - which is just a fancy term for changing code/stylesheets from one form to another. In other words, Sass provides a way to extend the features of CSS allowing you to enjoy some of the features native to a programming language such as variables, functions, modules, mixins, inheritance, operators just to mention a few.&lt;/p&gt;

&lt;p&gt;Before we dive in, lets first get the confusing part out of the way, shall we?&lt;/p&gt;

&lt;h3&gt;
  
  
  Sass (.sass) and Scss (.scss) ? Difference?
&lt;/h3&gt;

&lt;p&gt;Sass is the older syntax for writing stylesheets inspired by HAML (&lt;a href="http://haml.info/"&gt;http://haml.info/&lt;/a&gt;) - an HTML templating language that focuses on DRY, well indented and clear markup. A Sass stylesheet follows the same principles as shown in the code below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// .sass
body
  font-family: Helvetica, sans-serif
  color: #000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how each of the style declarations is indented and no ';' is used to mark the end of the declaration. I have to say, it does look quite concise making it easier to read.&lt;/p&gt;

&lt;p&gt;This approach has, however, been criticised because it looks and feels very different from actual plain CSS because:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CSS uses ';' to mark the end of a style declaration&lt;/li&gt;
&lt;li&gt;Also, in CSS, curly braces ('{' and '}') are used to indicate a block of CSS rather than indentation of the statements&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;SCSS - Sassy CSS&lt;br&gt;
Scss addresses the criticized aspects of sass by providing a more 'CSS' like way of writing stylesheets. For the above block of code, a .scss file would look more like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// .scss
body {
  font-family: Helvetica, sans-serif;
  color: #000;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The block above looks and feels like CSS. In fact, for this particular snippet, no major transformation needs to happen to transform it to CSS (Like literally, it already passes as CSS)&lt;/p&gt;

&lt;p&gt;At the end of the day, people always have different perspectives of which of the two is 'better' so just go with whatever version you prefer. I personally want to feel like I am writing CSS when writing sass stylesheets so I always go with .scss&lt;/p&gt;

&lt;p&gt;Let's take a look at how CSS is extended by sass&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Variables
&lt;/h3&gt;

&lt;p&gt;The term 'variable' refers to a storage unit for a value that is intended to be referenced in more than one places. &lt;/p&gt;

&lt;p&gt;Take for instance a scenario where you want to define a uniform and consistent padding across all buttons and input values in the 'login.css' and 'signup.css' file (Assume that the login.css is for a login page and signup.css is for the signup page).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// login.css
button {
 padding: 1rem;
}

input {
 padding: 1rem;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// signup.css
button {
 padding: 1rem;
}

input {
 padding: 1rem;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In older versions of CSS (before the advent of css-variables and sass variables), you had to define and remember the value in all the padding definitions. Should you want to change or adjust the padding, you have to go to each and every definition of the style as shown below.&lt;br&gt;
Sass provides a way to define a variable and reference it in all locations. &lt;/p&gt;

&lt;p&gt;This means that you can simply change the value of the variable and this subsequently changes the value in all the locations where it is referenced.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// login.scss

// DEFINE VARIABLE INSIDE THE FIRST .scss FILE
$input-btn-padding: 1rem;

button {
 padding: $input-btn-padding;
}

input {
 padding: $input-btn-padding;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// signup.scss

// VARIABLE IS STILL ACCESSIBLE IN THE SECOND FILE
button {
 padding: $input-btn-padding;
}

input {
 padding: $input-btn-padding;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is always a good practice to define a separate .scss file to hold all the variables. In such a case, ensure the variables file should be placed/loaded before all the other .scss files that require the variable values&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@import 'variables.scss';    // LOADED BEFORE THE OTHER FILES
@import 'login.scss';
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Import
&lt;/h3&gt;

&lt;p&gt;As you may have noticed from the above descriptions, Sass allows you to split the styles into multiple files and consolidate them into one file. This is quite useful where you want to ensure:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Separation of concerns&lt;/li&gt;
&lt;li&gt;Easier debugging of styles&lt;/li&gt;
&lt;li&gt;Logical organization of styles&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The separated files; better know as 'partials', take the form shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;_login.scss .    // _[name-of-file].scss
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The underscore before the file name serves as an indicator that this file is not the main file but rather, a partial file that is intended to be consolidated into the main file. The directory would look something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;|
|- styles-folder
|--&amp;gt; _variables.scss  // VARIABLES LOADED FIRST
|--&amp;gt; _login.scss
|--&amp;gt; _signup.scss
|--&amp;gt; main.scss        // MAIN SCSS FILE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the main.scss file would consolidate the styles as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@import 'variables';
@import 'login';
@import 'signup';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that the underscore and the extension need not be defined when importing the separate styles into the main file.&lt;/p&gt;

&lt;h2&gt;
  
  
  CONCLUSION
&lt;/h2&gt;

&lt;p&gt;We have barely scratched the surface of the power of sass but let's take a breather. By now you should be able to &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Distinguish between .sass and .scss formats of writing sass&lt;/li&gt;
&lt;li&gt;Define a sass variable&lt;/li&gt;
&lt;li&gt;Write separate sass stylesheets and consolidate them into one &lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  NEXT
&lt;/h2&gt;

&lt;p&gt;We will now delve into learning how to transform the .sass files to css and learn two more ways in which sass extends css&lt;/p&gt;

</description>
      <category>css</category>
      <category>sass</category>
      <category>scss</category>
    </item>
  </channel>
</rss>
