Yogesh Chauhan's Blog

How to use @if and @else in SCSS?

in SCSS on June 15, 2021

if-else rule in SassScript is similar to if-else rule in any other programming language. Two blocks. Once for the condition success and another for everything else.


@if  { 
  ...
} @else{
  ...
}

We use if-else statement to control what to execute in different scenarios. Just like any other programing language, if-else returns true for the satisfied condition and false if not.

This is how we can easily place either a circle or a square using just if.

@if Example in SCSS


@if and @else Example in SCSS


Add few @else ifs

Once you add @else if, the blocks will only execute when the preceding @if‘s condition is the false and and the @else if’s condition is true.

If none of the conditions/expressions are true then it will execute the last @else block by default, if you add the @else block.

Let’s modify the first example:


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to make a Custom Login Page in WordPress?WordPressHow to use images instead of HTML radio buttons using CSS?CSSWordPress: How to display slider repeater fields in ACF?WordPressHow to import MySQL small sample database into phpMySQL?SQL/MySQLHow to check if the page is the home page in WordPress?WordPressHow to change your WordPress database prefix?WordPressHow to create a Random Hex Color generator using JavaScript?JavaScriptHow to add a Line Chart in Angular App?AngularComponents and Props in ReactReactHow to get start index and end index (as int) of substring in Swift?SwiftHow to install Gulp with WordPress?WordPressSQL Inner JoinSQL/MySQL