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
HTML canvas methodsHTMLJavaScript arrays: a separate data type or Objects?JavaScriptSocial Media Colors: Sass VariablesMiscellaneousColleague UI Basics: The Context AreaColleagueCanvas Drawing in HTML5HTMLFor In Loop in Swift for BeginnersSwiftHow to add a Line Chart in Angular App?AngularMake sure your links have descriptive textUI/UXLearn to Make a Simple Contact Us Form using PHP and PDO-MySQLPHPUse SwiperJS to create mobile touch sliders fastMiscellaneousHow to make a curtain slider using jQuery and CSS?CSSPagination in CSS with multiple examplesCSS