Contact!

@If, @Else, @Else if and @If not: Sass conditional statements

Conditional statements in Sass

We can use conditional statements in Sass in the form of @if, @else, @else if and @if not. Much like other programming languages this will write a CSS statement only if the condition is met. They are really useful statements to use in loops and mixins where we are likely to want to check whether a condition evaluates to true or false.

@if, @else and @else if

We open our conditional with @if and can follow it with @else if or @else. When a condition gets met then the css gets generated:

@if $something {
    // do something
} @else if $something-else {
    // do something else
} @else {
    // or do this
}

In Sass ‘null’ and ‘false’ are false; everything else is true

In my last post we looked at loops in Sass, so lets use the theme loop in our example.

$themeColours: (
    "pink": #DC51AC,
    "red": #D64651,
    "orange": #E55937,
);

@each $themeColour, $i in $themeColours {
    body {
        &.#{$themeColour} {
            background-color: $i;

            @if $themeColour == "pink" {
                color: #000;
            } @else if $themeColour == "red" {
                color: #fff;
            } @else {
                color: #DEEDEE;
            }
        }	
    }
}

Here we are looping through an array of theme colours to change the body text colour. We are using an if statement to check the key ($themeColour) for the values “pink” and “red” and otherwise setting a default colour. The output looks like this:

/* Output */

body.pink {
    background-color: #DC51AC;
    color: #000; 
}

body.red {
    background-color: #D64651;
    color: #fff; 
}

body.orange {
    background-color: #E55937;
    color: #DEEDEE; 
}

@if not

You can also evaluate for false by using @if not. For @if not to work you have to put parenthesis around the argument:

$width: 50px;

div {
    width: $width;

    @if not ($width == 100px) {
        background: red;
    } else {
        background: blue;
    }
}

This statement sets the background to ‘red’ if $width is not equal to 100px and to ‘blue’ if $width is equal to 100px.

/* Output */

div {
    width: 99px;
    background: red;
}

Join the discussion!

You might like:

Create, register and use shortcodes in WordPress

Create, register and use shortcodes in WordPress

by Gav 18/03/2023

Learn how to create and register your own WordPress shortcodes to add dynamic content to your posts and pages.

How to use guard clauses in JavaScript

How to/why use guard clauses in JavaScript

by Gav 16/03/2023

Learn how to improve code readability and performance by using guard clauses in JavaScript. Discover their benefits and best practices.

Implements and Extends, Object Oriented TypeScript

Implements and Extends, Object Oriented TypeScript

by Gav 15/03/2023

Learn the difference between implements and extends in TypeScript. Use Implements to implement interfaces and types, and extends to inherit from classes.

Reading/Parsing and Writing YAML files in PHP, Symfony

Reading/Parsing and Writing YAML files, PHP Symfony

by Gav 14/03/2023

In this tutorial we will look at using YAML in PHP. Learn about Parsing and Writing YAML files using Symfony's YAML component.

Measuring code execution performance in JavaScript

Measuring code execution performance in JavaScript

by Gav 13/03/2023

Measuring code execution performance is an important way to identify bottlenecks. Use these methods in JavaScript to help optimise your code.

Measuring script/code execution time in PHP using microtime

Measuring script/code execution time in PHP, microtime

by Gav 06/03/2023

Find bottlenecks, optimise and clean your code, and speed up your apps by measuring the execution time of your PHP scripts using microtime.

Regenerate WordPress media image sizes, programmatically

Regenerate WordPress media image sizes, programmatically

by Gav 25/02/2021

Learn how to regenerate and update WordPress media and image sizes both programmatically (without plugin), and also with a handy plugin.

Magic Constants in PHP. What they are and how to use them.

Magic Constants in PHP. What they are and how to use them

by Gav 15/02/2021

Ever seen constants like __DIR__ and __FILE__ being used in PHP? These are 'Magic Constants', and this is how we can use them.

Detecting Keypress JavaScript

Detect single and multiple keypress events: JavaScript

by Gav 16/10/2019

Learn how to use event listeners to detect and handle single and multiple keypress events in JavaScript. Add modifier keys to your application!