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 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

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
5 Ways to Loop Through JavaScript ArraysJavaScriptWordPress: How to print ACF array field values?WordPressHow to use $INSERT command in Envision Basic?Envision BasicJavaScript Number MethodsJavaScriptFor In Loop in Swift for BeginnersSwiftHow to check if a link has http or https in it in JavaScript?JavaScriptAlternate Style Sheets in CSSCSSThe order Property in Flex Items in CSSCSSConditional operator in JavaScript (aka ternary operator)JavaScriptHow to catch .keypress() on body using jQuery?jQueryHow to delete a local and a remote Git branches?MiscellaneousHow to create a Recent Posts function in WordPress?WordPress