Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • CSS Tutorial
  • CSS Exercises
  • CSS Interview Questions
  • CSS Selectors
  • CSS Properties
  • CSS Functions
  • CSS Examples
  • CSS Cheat Sheet
  • CSS Templates
  • CSS Frameworks
  • Bootstrap
  • Tailwind
  • CSS Formatter
Open In App
Next Article:
SASS @if and @else
Next article icon

SASS @if and @else

Last Updated : 07 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

SASS provides flow control mechanisms similar to JavaScript, allowing you to conditionally compile CSS code based on logical expressions. Using @if, @else, and @else if, you can create dynamic styles that change based on the values passed to your mixins or variables.

@if Directive

The @if directive works similarly to conditional statements in programming languages. If the expression inside the @if block evaluates to true, the block is compiled; otherwise, it’s skipped.

Syntax:

@if <conditional_expression> {
// styles go here
}

Example 1: Using @if in a SASS Mixin

In this example, we create a mixin that formats a button, and we conditionally apply a border-radius if the button is meant to be round.

@mixin button-format( $round-button, $size ) {     color: white;     background-color: blue;     width: $size;      @if $round-button {         height: $size;         border-radius: $size / 2;      } }  .mybutton{     @include button-format(false, 100px); } 
Compiled CSS file
.mybutton {   color: white;   background-color: blue;   width: 100px; } 

Here, the @if condition checks if $round-button is true. Since we passed false, the conditional block is not executed, resulting in a square button.

Example 2: Applying @if for Round Buttons

If we change the argument to true, the button becomes round.

SASS File:

@mixin button-format( $round-button, $size ) {     color: white;     background-color: blue;     width: $size;      @if $round-button {         height: $size;         border-radius: $size / 2;      } }   .mybutton{     @include button-format(true, 100px); } 
Compiled CSS file
.mybutton {     color: white;     background-color: blue;     width: 100px;     height: 100px;     border-radius: 50px; }

@else Directive

The @else block is executed when all the previous @if and @else if conditions evaluate to false. It’s a fallback option that runs when no other conditions are met.

Syntax:

@else {
// styles go here
}

Example: Using @else for Conditional Theme

Here’s a mixin that applies different background colors based on the value of the $theme-decide variable. The @else block will be executed if the condition in the @if block is false.

SASS File:

@mixin theme ($theme-decide, $r, $g, $b) {     // light background     @if $theme-decide {         background-color: rgba($r, $g, $b, 0.5);     }     // dark background     @else {         background-color: rgba($r, $g, $b, 1); // red color     }  }  .myblock {     @include theme(false, 255, 0, 0); } 
Compiled CSS file:
.myblock {     background-color: red;     // if true value is passed then rgba(255, 0, 0, 0.5); } 

In this case, since $theme-decide is false, the @else block is executed, resulting in a dark background (fully opaque red).

@else if Directive

The @else if directive allows for multiple conditional branches. It’s used when you need to check for multiple conditions sequentially. The first @else if condition that evaluates to true is executed, and the remaining blocks are skipped.

Syntax:

@else if <conditional_expression> { ... }

Example: Multiple Themes with @else if

In this example, we create a mixin that supports three different themes: light, medium-dark, and dark. We use @if, @else if, and @else to handle each theme based on the value of $theme-decide.

SASS File:

@mixin theme ($theme-decide, $r, $g, $b) {     // light background     @if $theme-decide == 1 {         background-color: rgba($r, $g, $b, 0.5);     }     // medium-dark background     @else if $theme-decide == 2 {         background-color: rgba($r, $g, $b, 0.7);     }     // dark background     @else {         background-color: rgba($r, $g, $b, 1);     }  }  .myblock {     @include theme(2, 0, 255, 0); } 
Compiled CSS file:
.myblock {   background-color: rgba(0, 255, 0, 0.7); } 


Next Article
SASS @if and @else

A

Aakash_Panchal
Improve
Article Tags :
  • Web Technologies
  • CSS
  • CSS-Advanced
  • SASS

Similar Reads

    Sass | At-rules
    At-rules are simply some CSS statements that instructs the CSS how to behave in particular conditions. They start with an "@" sign, followed by an identifier and ends with a semicolon (in case of SCSS), or the next CSS statement (in case of SASS), depending upon the Sass syntax used.  Syntax: css @i
    2 min read
    Sass @extend Rule
    The sass @extend rule can be used when it is necessary for a class to have the styles of another class, along with its own specific styles. This rule tells Sass that one selector should inherit the class from another selector.Syntax:@extend <selector>Example: This example shows the use of the
    3 min read
    SASS | @forward rule
    The @forward rule processes the Sass stylesheets and makes their functions, mixins, and variables available in the cases when the stylesheet is loaded using @use rule. Organizing Sass libraries across many files are made possible, along with providing users to process a single entry point file. Synt
    3 min read
    Sass @each Rule
    The @each rule is used to emit styles or evaluate codes for each element of a list or each pair of a map. It is most advantageous for repeating styles that have a few differences in them. The section is evaluated for every element of the list or pair of the map which has been assigned to the provide
    2 min read
    SASS @for and @while Rule
    @for rule is used to count up or down from one number to another and check the section for every number that comes in between the given range. Every number is assigned a given variable name. In order to exclude the last number, use to and in order to include it, use through. Syntax: css @for <var
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences