Introduction
In this article we will see how we can perform further operations of Sass ( Systematically Awesome Style Sheets) in our style. A basic introduction to get started with Sass is given
here.
Below are the operations that we are going to perform.
- Variables in sass file
- Nesting of styles
- Use of mixins
- Importing files
- Pseudo classes
Prerequisite
Before learning SASS, you must have the basic knowledge of HTML and CSS.
Use of variables
Create a file variable.scss under scss folder.
Add the below contents,
- // using variables
- $backgroundColor: blue;
- $color: white;
- $width: 400px;
- $height: 200px;
- .section{
- background-color: $backgroundColor;
- color: $color;
- height: $height;
- width: $width;
- }
- h2{
- background-color: $backgroundColor;
- color: $color;
- width:$width;
- }
Variables can be declared using ‘$’ symbol. This is useful when we are utilizing the same set of values in multiple areas of stylings.
Css file generated under css folder as follow.
- .section {
- background-color: blue;
- color: white;
- height: 200px;
- width: 400px; }
-
- h2 {
- background-color: blue;
- color: white;
- width: 400px; }
We don’t need to consider what the css file is generating. This is the beauty of ScSS -- that it is soo easy and reliable to create the css file and its contents.
Create variable.html and add the below contents.
- <html>
- <head>
- <link href="css/variable.css" rel="stylesheet"/>
- </head>
- <body>
- <h2>Sass ---- Variable Example</h2>
- <div class="section">
- This is section with css attributes set via variable.css with the use of variables.
- </div>
- </body>
- </html>
Open this file in the browser and you can see the output. It will be pretty cool as expected.
Nesting of Styles
Create a file nestingStyle.scss under scss folder.
Add the below content:
- // Nesting of styles
-
- $backgroundColor : Blue;
- $color: white;
-
- .first{
- background-color: $backgroundColor;
- height:400px;
- width: 400px;
- float: left;
-
- .second{
- margin:10px;
- padding:20px;
- height: 300px;
- width: 300px;
- color: yellow;
- background-color: green;
- .third{
- margin:10px;
- padding: 20px;
- height: 100px;
- width: 150px;
- background-color: white;
- color: blue;
- }
- }
- }
Save the file and you will get the generated nestingStyle.css under css folder with the below content.
- .first {
- background-color: Blue;
- height: 400px;
- width: 400px;
- float: left; }
- .first .second {
- margin: 10px;
- padding: 20px;
- height: 300px;
- width: 300px;
- color: yellow;
- background-color: green; }
- .first .second .third {
- margin: 10px;
- padding: 20px;
- height: 100px;
- width: 150px;
- background-color: white;
- color: blue; }
Create a nestingStyle.html and add the below content:
- <html>
- <head>
- <link href="css/nestingStyles.css" rel="stylesheet"/>
- </head>
- <body>
- <div class="first">
- Class first applied.
- <div class="second">
- Class second applied.
- <div class="third">
- Class third applied.
- </div>
- </div>
- </div>
- </body>
- </html>
Open this file in the browser and you will see the output with applied class and properties.
A simple programming logic has been applied in scss file and the styles has been generated.
Mixin and its use
Mixin is a chunk of reusable css or Sass that we need to inject to any element. This is something where we create a set of styles for a particular section or form and inject to any element by import statement.
Mixin can be created as follows,
@mixin mixin_name{
}
Let us create a scss file “mixInStyle.scss” and add the below content,
- //mixin
-
- $headingWidth : 40%;
- $headingBackgroundColor: cyan;
- $contentWidth: 100%;
- $contentBackgroundColor: magenta;
-
- @mixin heading{
- h2{
- width: $headingWidth;
- background-color: $headingBackgroundColor;
- }
- div{
- width: $contentWidth;
- background-color: $contentBackgroundColor;
- }
- }
-
- @mixin heading-description{
- span{
- background-color: green;
- text-decoration: underline;
- }
- }
-
- .normal-banner{
- @include heading;
- }
-
- .standard-banner{
- @include heading;
- @include heading-description;
- }
Save the file and you will get the css file “mixInStyle.css” with the below content,
- .normal-banner h2 {
- width: 40%;
- background-color: cyan; }
-
- .normal-banner div {
- width: 100%;
- background-color: magenta; }
-
- .standard-banner h2 {
- width: 40%;
- background-color: cyan; }
-
- .standard-banner div {
- width: 100%;
- background-color: magenta; }
-
- .standard-banner span {
- background-color: green;
- text-decoration: underline; }
create html file “testMixin.html” with the below content,
- <html>
- <head>
- <link href="css/mixinInStyle.css" rel="stylesheet"/>
- </head>
- <body>
- <div class="normal-banner">
- <h2>Heading one!</h2>
- <div>This is description. This is description. This is description. This is description. This is description. This is description. This is description. </div>
- </div>
- <div class="standard-banner">
- <h2>Heading two!</h2>
- <span>This is small descriptions for the heading.</span>
- <div>This is description. This is description. This is description. This is description. This is description. This is description. This is description. </div>
- </div>
- </body>
- </html>
Open this file in the browser you will see the classes of scss defined styles applied on html contents.
Importing files in SaSS
Now we are going to learn how to import one scss file to another scss file.
Let us create three different files (variables.scss, mixin.scss, rootStyle.scss).
Under variables.scss we will be having all the variables used in rootStyle.scss.
Under mixin.scss we will have
Under rootStyle.scss we will import variables.scss and mixin.scss file.
Let’s start.
Create file variable.scss and add below contents.
- //variables declaration
-
- $headingWidth : 40%;
- $headingBackgroundColor: cyan;
- $contentWidth: 100%;
- $contentBackgroundColor: magenta;
Create file mixin.scss and add the below contents.
- @mixin heading{
- h2{
- width: $headingWidth;
- background-color: $headingBackgroundColor;
- }
- div{
- width: $contentWidth;
- background-color: $contentBackgroundColor;
- }
- }
-
- @mixin heading-description{
- span{
- background-color: green;
- text-decoration: underline;
- }
- }
Create file rootStyle.scss and add the below contents,
-
-
- @import "variables";
- @import "mixin";
-
- .normal-banner{
- @include heading;
- }
-
- .standard-banner{
- @include heading;
- @include heading-description;
- }
Open prePros application and you will see the files that are being imported by some other css file will be configured as not to compile and generate css file. You can manually check to compile anyway.
Create testImport.html file and add below contents,
- <html>
- <head>
- <link href="css/rootStyle.css" rel="stylesheet"/>
- </head>
- <body>
- <div class="normal-banner">
- <h2>Heading one!</h2>
- <div>This is description. This is description. This is description. This is description. This is description. This is description. This is description. </div>
- </div>
- <div class="standard-banner">
- <h2>Heading two!</h2>
- <span>This is small descriptions for the heading.</span>
- <div>This is description. This is description. This is description. This is description. This is description. This is description. This is description. </div>
- </div>
- </body>
- </html>
Open the html file in browser to see the output that will apply the styles from rootStyle.css.
Pseudo Classes
Pseudo classes are use to add extra effect like hover, after effect ect.
Create a file under scss folder named pseudo_style.css and add the below content,
- ul{
- list-style-type: none;
- margin: 0;
- padding: 0;
- overflow: hidden;
- background-color: #333;
-
- }
- li{
- float:left;
- }
- li{
- a{
- display: block;
- color:white;
- text-align: center;
- padding: 14px 16px;
- text-decoration: none;
-
- }
- }
- li {
- a{
- &:hover{
- background-color: #111;
- }
- }
-
- }
Save the file and you will get pseudo_style.css under css folder with the below generated content,
- ul {
- list-style-type: none;
- margin: 0;
- padding: 0;
- overflow: hidden;
- background-color: #333; }
-
- li {
- float: left; }
-
- li a {
- display: block;
- color: white;
- text-align: center;
- padding: 14px 16px;
- text-decoration: none; }
-
- li a:hover {
- background-color: #111; }
Create testPseudoStyle.html and add the below content,
- <html>
- <head>
- <link href="css/pseudo_style.css" rel="stylesheet"/>
- </head>
- <body>
- <ul>
- <li><a class="active" href="#home">Home</a></li>
- <li><a href="#news">News</a></li>
- <li><a href="#contact">Contact</a></li>
- <li><a href="#about">About</a></li>
- </ul>
- </body>
- </html>
Open this file in the browser and you will see the output with defined class properties.