A lesson on CSS preprocessors, specifically SASS and using it with Webpack
Let’s first dive into a little background.
A pre-processor is a program that takes one type of data and converts it to another. So some common pre-processor you’ve probably seen…
JavaScript
HTML
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
Now, let’s dive in on how to get SASS up and running.
sudo su -c "gem install sass"
gem install sass
, if you encounter an error, try it with sudo sudo gem install sass
.sass -v
, you should have Sass 3.4.22npm init
in the terminal. Go into your package.json and under scripts add this line under the test ,"convert-styles": "sass --watch dist/stylesheet.scss:dist/stylesheet.css"
npm run convert-styles
. You can edit the script files at any time, or name them to your preference.Now, let’s learn a little bit about why SASS is so cool…
$primary-fontcolor: rgb(107, 124, 64);
$primary-font: Helvetica, sans-serif;
body {
font: $primary-font,
color: $primary-fontcolor
}
<div class="testing">
<div class="inside-testing">
hi from inside div1
<div>
</div>
Now on our .scss file we can single out the inside-div1 with nesting…
.testing {
.inside-testing {
font-size: 5em;
}
}
<div class="fake-nav">
<ul>
<li>HOME</li>
<li>LOGIN</li>
</ul>
</div>
.fake-nav {
height: 150px;
width: 100%;
background: rgb(44, 142, 175);
}
@import 'nav';
at the top of your stylesheet.scss file.
<div class="square1">
</div>
<div class="square2">
</div>
@mixin style-box($length-width, $color) {
height: $length-width;
width: $length-width;
background-color: $color;
}
.square1 {@include style-box(40px, rgb(184, 53, 158))}
.square2 {@include style-box(100px, rgb(81, 70, 83))}
.message {
border: 1px solid #ccc;
padding: 10px;
color: #333;
}
.success {
@extend .message;
border-color: green;
}
+, -, * & %
..fake-nav {
width: 100% / 2;
}