visit
"liveSassCompile.settings.formats":[
// You can add more
{
"format": "expanded",
"savePath": "/css"
}
],
"liveSassCompile.settings.generateMap": false
Before we start writing our first Sass code, we need to tell Live Sass to watch our Sass file and compile it on the fly. To do so, just press ctrl + shift + p and type Live Sass in the search area and select Live Sass: Watch Sass.
Now, whenever we write in our Sass file and save it the output, a CSS file with the same name as the Sass file, will be automatically generated in the css folder and this is the file we link in the head section of our HTML file.$variablename: value;
$base-font: 'Helvetica Neue', Arial, sans-serif;
$color-primary: #032f3e;
$gutter:20px;
As we can see, the first variable holds font-family, the second one hex value, and the third occupies pixel value.
So let’s declare our first variable in our style.scss file
$base-font: 'Helvetica Neue', Arial, sans-serif;
body {
font-family: $base-font;
}
body {
font-family: 'Helvetica Neue', Arial, sans-serif;
}
<link href="css/style.css" type="text/css" rel="stylesheet"/>
$color-primary: #032f3e;
$color-secondary: #888;
$color-tertiary: #383838;
$color-white: #fff;
$color-black: #000;
.main-nav {
background: $color-primary;
ul {
width: 100%;
display:flex;
}
}
.main-nav {
background: #032f3e;
}
.main-nav ul {
width: 100%;
display:flex;
}
.main-nav {
background: $color-primary;
ul {
width: 100%;
display:flex;
}
li{
flex: 1 1 14%;
a{
color: $color-white;
text-decoration: none;
padding: 16px;
text-align: center;
display: block;
}
}
}
.heading-1 {
color: red;
font-size: 40px;
font-weight: bold;
border: 1px solid blue;
}
.heading-2 {
color: red;
font-size: 30px;
font-weight: bold;
border: 1px solid blue;
}
@mixin common-heading-style {
color: red;
font-weight: bold;
border: 1px solid blue;
}
.heading-1 {
@include common-heading-style();
font-size: 40px;
}
.heading-2 {
@include common-heading-style();
font-size: 30px;
}
.heading-1 {
color: red;
font-weight: bold;
border: 1px solid blue;
font-size: 40px;
}
.heading-2 {
color: red;
font-weight: bold;
border: 1px solid blue;
font-size: 30px;
}
.main-nav {
background: $color-primary;
ul {
width: 100%;
display:flex;
flex-direction: row;
}
li{
flex: 1 1 (100%/7);
a{
color: $color-white;
text-decoration: none;
padding: 16px;
text-align: center;
display: block;
}
}
}
display:flex;
flex-direction: row;
@mixin d-flex($flex-direction){
display:flex;
flex-direction: $flex-direction;
}
.main-nav {
background: $color-primary;
ul {
width: 100%;
@include d-flex(row);
}
li{
flex: 1 1 (100%/7);
a{
color: $color-white;
text-decoration: none;
padding: 16px;
text-align: center;
display: block;
}
}
}
Similarly, in our HTML file, we have two banners: main-banner and pricing banner which have similar HTML structure but different content. Obviously, we can have a common style for both banners using a mixin and apply it to both of them. Then, we can add additional style to each of them as per the requirement.
Here is the mixin which will be used by both banners:
@mixin banner(){
width: 100%;
height: 400px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
padding-bottom: 20px;
margin-bottom: 20px;
color: $color-white;
h2,
p {
margin-bottom: 10px;
}
.btn {
margin-top: 10px;
margin-bottom: 10px;
padding: 10px 16px;
background: $color-secondary;
}
a {
color: $color-white;
text-decoration: none;
}
}
.main-banner {
@include banner();
background: url(../img/banner.jpg) no-repeat center center/cover;
background-position-y: bottom;
h2{
font-size: 3rem;
}
p {
font-size: 1.2rem;
}
}
.pricing-banner {
@include banner();
background: url(../img/banner.jpg) no-repeat center center/cover;
background-position-y: bottom;
h2{
font-size: 3rem;
}
p {
font-size: 1.2rem;
}
li{
text-transform: uppercase;
font-size: 20px;
max-width: 500px;
margin: 60px 0;
}
}
@mixin banner(){
width: 100%;
height: 400px;
@include d-flex(column);
justify-content: center;
align-items: center;
text-align: center;
padding-bottom: 20px;
margin-bottom: 20px;
color: $color-white;
h2,
p {
margin-bottom: 10px;
}
.btn {
margin-top: 10px;
margin-bottom: 10px;
padding: 10px 16px;
background: $color-secondary;
}
a {
color: $color-white;
text-decoration: none;
}
}
Enough theory, let’s separate our style.scss file into different files. But before that, there are files that start with an underscore and they are called partials. Partials are different from normal Sass files in that they are not compiled into a separate CSS file. These are the files we are going to create and import them in our main Sass file — style.scss.
So in the Sass folder let’s create the following four files: _variable.scss, _config.scss, _mixins.scss, and _utilities.scss and import them in the style.scss file.
_variable.scss will hold variable declarations so let’s cut all variable declarations and put them inside this file. We will also add some additional variables that will be used in other files. Similarly, _mixins.scss file will hold all our mixins in it.
_config.scss and _utilities.scss will hold some configurations styles and utility classes respectively; and they will be used as we progress.
So _variables.scss will look like this and whenever we want to create a new variable or change the value of an existing one, we go to this file and perform the necessary changes.
$base-font: 'Helvetica Neue', Arial, sans-serif;
$color-primary: #032f3e;;
$color-secondary: #888;
$color-tertiary: #383838;
$color-white: #fff;
$color-black: #000;
$max-width: 1200px;
$section-title: 2.5rem;
$gutter:20px;
$numberOfNavLinks: 7 ;
$grid-bp-md: 768px;
$grid-bp-lg: 992px;
$grid-bp-xl: 1200px;
$grid-cols:12;
Likewise, _mixins.scss will contain all our mixins inside it.
Now, in order to use these files, we need to import these files in our style.scss file as follows:@import 'variables';
@import 'mixins';
@import 'config';
@import 'utilities';
@for is a control directive that is used to loop through each iteration and it is similar to any other programming language for loop construct.
One form of the @for directive in Sass is @for $var from <start> through <end> which starts at <start> and loops through each iteration and ends at <end>.
Let’s put the following simple code inside _config.scss file.@for $i from 1 through 5 {
.m-#{$i} {
margin: #{$i}rem;
}
}
.m-1 {
margin: 1rem;
}
.m-2 {
margin: 2rem;
}
.m-3 {
margin: 3rem;
}
.m-4 {
margin: 4rem;
}
.m-5 {
margin: 5rem;
}
First thing first, before we create our own grid layout system we need to have a row. So let’s create it and put the following code inside the _utilities.scss file.
.row{
@include d-flex(row);
flex-wrap: wrap;
width: 100%;
}
_utilities.scsss file in addition to the above row class, it also has the following Sass codes:
.container {
max-width: $grid-bp-md;
margin: 0 auto;
&-fluid{
margin: 0;
max-width: 100%;
}
}
.text-center {
text-align: center;
}
@for $i from 1 through $grid-cols{
.col-#{$i}{
flex-basis: 100*$i / ($grid-cols)*1%;
}
}
What the above piece of code does is: first since Bootstrap uses 12 grid columns, $grid-cols is a variable we set to 12 in our variables file. Next, on the first iteration, the value of $i is set to 1 and the placeholder #{$i} is replaced by 1. Then, some math is used to generate a calculated value for flex-basis by performing the math operation 100*1/12 and appending % symbol at the end of the calculated result. Finally, such classes as .col-1 as shown below are generated in our style.css file.
.col-1 {
flex-basis: 8.33333%;
}
@for $i from 1 through $grid-cols{
.col-#{$i}{
flex-basis: 100*$i / ($grid-cols)*1%;
}
.col-offset-#{$i}{
margin-left:100*$i / ($grid-cols)*1% ;
}
}
a {
color: $color-white;
padding: 12px 16px;
text-decoration: none;
}
a {
background: $color-primary;
color: $color-white;
padding: 12px 16px;
text-decoration: none;
&:hover {
background: #00f;
}
}
.btn {
margin-top: 10px;
margin-bottom: 10px;
padding: 10px 16px;
background: $color-secondary;
transition: all ;
&:hover{
background: darken($color-secondary, 20%);
}
}
@content is a directive used in combination with mixins to make our code more reusable and DRY. Sometimes we want to inject a chunk of styles to mixin in order to create something. This can be achieved using a @content directive.
One use case for @content directive along with mixins is to style a specific element at a certain break point. For instance, let’s change the size of the text in the h2 tag of our banners at a given break point. To do so we can create a mixin as follows:
@mixin mQ($grid-bp){
@media screen and (max-width: $grid-bp){
@content;
}
}
@include mQ(768px){
h2 {
font-size: 2rem;;
}
}
@media screen and (max-width: 768px) {
.main-banner h2 {
font-size: 2rem;
}
}
The power of @content directive and mixin is clearly evident when such style is generated using a few lines of codes. Hence before we end this tutorial let’s see what we can do more with the @content directive.
$pages: 'home', 'about', 'mission', 'products', 'contact';
$bps: (600px, 768px, 992px);
$weights: (100, 200, 300, 400, 500, 600, 700, 800, 900);
@each $weight in $weights {
.font-weight-#{$weight} {
font-weight: #{$weight};
}
}
@each is another looping construct in Sass that we use to iterate a list. Thus, the above code will generate such font weight classes as
.font-weight-100 { font-weight: 100 }, .font-weight-200 { font-weight: 200 }
and so on for all elements of the $weights list. Now, we can apply one of these classes, .font-weight-900
, in the title of the mission section of the page.Another type available in Sass is the Map Function. Unlike lists, maps store key/value pairs and parentheses are required when creating them. They look like a dictionary in some other programming languages. The syntax for maps is:$map: (
key: value,
other-key: other-value
);
$grid-breakpoints-map: (
md: 768px,
lg: 992px,
xl: 1200px
);
@mixin configure-col-classes($modifier, $grid-cols, $breakpoint) {
@include mQ($breakpoint) {
@for $i from 1 through $grid-cols {
.col-#{$modifier}-#{$i} {
flex-basis: (100 / ($grid-cols / $i) ) * 1%;
}
.col-#{$modifier}-offset-#{$i} {
margin-left: (100 / ($grid-cols / $i) ) * 1%;
}
}
}
}
Now, we can use this mixin in _config.scss file as follows:
@each $bp-modifier, $breakpoint in $grid-breakpoints-map {
@include configure-col-classes($bp-modifier, $grid-cols, $breakpoint);
}
$grid-breakpoints is a map that holds a break point modifier as a key and the value of the breakpoint in pixels value. It is declared in the _variables.scss file. Now, for each iteration of this map, the mixin is called using break point key value of the map, grid columns variable, and the break point value of the map to generate a full grid system for both medium and large screen devices.
Now, if we refresh our page, the mission section will behave as if we have imported Bootstrap in our web page. But, the fact is we have just created our own grid system similar to what Bootstrap uses in its grid layout system.If you find this tutorial interesting share it and give it a clap. You can also write a comment in the comment section. Thanks for reading.References:Previously published at