SASS/SCSS / Operators and Control Structures

Adding Conditional Logic with @if and @else

In this tutorial, you'll learn how to use the @if and @else directives in SASS/SCSS to add conditional logic to your stylesheets.

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Teaches the use of operators and control structures to add logic to stylesheets.

Adding Conditional Logic with @if and @else in SASS

1. Introduction

The goal of this tutorial is to introduce you to the use of @if and @else directives in SASS/SCSS to add conditional logic to your stylesheets. You will learn how to create dynamic stylesheets that can adapt to different conditions.

Prerequisites: You should have a basic understanding of CSS and a general knowledge of SASS/SCSS.

2. Step-by-Step Guide

In SASS, the @if and @else directives allow you to apply styles conditionally based on the evaluation of certain conditions. This can make your stylesheets more dynamic and adaptable.

The syntax for the @if directive is as follows:

@if condition {
  // styles to apply if condition is true
} @else {
  // styles to apply if condition is false
}

The condition can be any SASS expression that returns a boolean value (true or false).

3. Code Examples

Example 1:

$bg-color: blue;

body {
  @if $bg-color == blue {
    background-color: blue;
  } @else {
    background-color: white;
  }
}

In the above example, the background color of the body will be blue if the $bg-color variable is 'blue'. Otherwise, the background color will be white.

Example 2:

$width: 800px;

.container {
  @if $width > 768px {
    width: 80%;
  } @else if $width > 480px {
    width: 90%;
  } @else {
    width: 100%;
  }
}

Here, if the $width variable is greater than 768px, the width of the .container will be 80%. If the $width is greater than 480px but less than or equal to 768px, the width will be 90%. For all other cases, the width will be 100%.

4. Summary

In this tutorial, we covered how to use the @if and @else directives in SASS/SCSS to add conditional logic to your stylesheets. You should now be able to write more dynamic and adaptable styles.

Next, you can explore other SASS features like mixins, functions, and loops. You can also learn more about CSS preprocessors in general.

5. Practice Exercises

Exercise 1: Write a SASS script to set the font size of a paragraph to 16px if the screen width is less than 768px. If the screen width is greater than or equal to 768px, set the font size to 20px.

Solution:

$screen-width: 800px;

p {
  @if $screen-width < 768px {
    font-size: 16px;
  } @else {
    font-size: 20px;
  }
}

Exercise 2: Define a SASS variable $theme with a value of 'dark'. If the $theme is 'dark', set the background color of the body to black and the color to white. If the $theme is 'light', set the background color to white and the color to black.

Solution:

$theme: dark;

body {
  @if $theme == dark {
    background-color: black;
    color: white;
  } @else if $theme == light {
    background-color: white;
    color: black;
  }
}

Keep practicing with different conditions and styles to get a good grasp of using @if and @else in SASS/SCSS.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Color Palette Generator

Generate color palettes from images.

Use tool

PDF Password Protector

Add or remove passwords from PDF files.

Use tool

CSS Minifier & Formatter

Clean and compress CSS files.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

PDF to Word Converter

Convert PDF files to editable Word documents.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help