- Sass Tutorial
- Sass - Home
- Sass - Overview
- Sass - Installation
- Sass - Syntax
- Using Sass
- Sass - CSS Extensions
- Sass - Comments
- Sass - Script
- Sass - @-Rules and Directives
- Control Directives & Expressions
- Sass - Mixin Directives
- Sass - Function Directives
- Sass - Output Style
- Extending Sass
- Sass Useful Resources
- Sass - Interview Questions
- Sass - Quick Guide
- Sass - Useful Resources
- Sass - Discussion
Sass - @else if Directive
Description
The @else if statements are used with the @if directive. Whenever the @if statement fails then the @else if statements are tried and if they also fail, then the @else is executed.
Syntax
@if expression { // CSS codes } @else if condition { // CSS codes } @else { // CSS codes }
Example
The following example demonstrates the the use of @else if directive −
<html> <head> <title>Control Directives & Expressions</title> <link rel = "stylesheet" type = "text/css" href = "style.css"/> </head> <body> <div class = "container"> <h1>Example for Control Directive & Expressions</h1> <p>SASS stands for Syntactically Awesome Stylesheet.</p> </div> </body> </html>
Next, create file style.scss.
style.scss
$type: audi; p { @if $type == benz { color: red; } @else if $type == mahindra { color: blue; } @else if $type == audi { color: green; } @else { color: black; } }
You can tell SASS to watch the file and update the CSS whenever SASS file changes, by using the following command −
sass --watch C:\ruby\lib\sass\style.scss:style.css
Next, execute the above command; it will create the style.css file automatically with the following code −
style.css
p { color: green; }
Output
Let us carry out the following steps to see how the above given code works −
Save the above given html code in if_else_directive.html file.
Open this HTML file in a browser, an output is displayed as shown below.
sass_control_directives_expressions.htm
Advertisements