EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

SASS if else

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » CSS Tutorial » SASS if else

SASS if else

Definition of SASS if else

SASS Syntactically Awesome Style Sheet is a CSS pre-processor with if-else is defined as a control – flow statement to give out the true statements concerning the conditions they apply in the stylewhichconverts CSSsyntax to the W3C CSS recommendation syntax and helps in creating style sheets faster also compatible with all versions of CSS. Sass makes use of features like mixin, inheritance, variables, and rules which are apart from CSS. It is a built-in function and the code is not understood by the browser, so the process called transpiling a pre-processor converts sass code to CSS code. The condition we check here is either a Booleantrue or boolean false result.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Saas doesn’t require any braces and semicolons, instead, it prefers indentation. The general syntax is given below:

The syntax for if to be used in SAAS it is defined by using the prefix ‘@’

@if <any condition statement>
{
…...// content of CSS codes
}

The syntax for else if

if expression statement {
// content of CSS codes
} @else if condition statement {
// content of CSS codes
} @else {
// content of CSS codes
}

Flowchart

Following are the flow chart of if else statement:

1. Flowchart for if

Flowchart for if

2. Flowchart for else

Flowchart for else

How if-else Works in SASS?

This SASS control directives are meant for decision making to provide flow logic on the functions. This is done in a few ways.

  • Compiling CSS using Ruby software by installing the gem.
  • With through GUI application.
  • With NPM via node-sass

Here if and else works with if directive and else directive and sass file is saved as. scss extension. Below implementation is done with ruby and the scss file created is convertor to css either by sass watch or using online editor convertor.

Popular Course in this category
All in One Software Development Bundle (600+ Courses, 50+ projects)600+ Online Courses | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (3,144 ratings)
Course Price

View Course

Related Courses
CSS Training (9 Courses, 9+ Projects)Bootstrap Training (2 Courses, 6+ Projects)jQuery Training (8 Courses, 5 Projects)

Examples

Let’s see the implementation of if and else in Saas.

In the below example Saas if is created for the tag <p>. Here we met with three conditions using if, if any of the below three expressions is true, the respective statement is created in CSS file. Next, when you execute in Html file, the CSS Style is reflected in the output.

Note: Create .scss file and convert them into .css file through editors then finally reflect the changes in HTML file.

The respective Sass File is

pp.scss

p {
@if 6 + 6 == 12 { border: 2px solid;  }
@if 4 < 2      { border: 1px dotted; }
@if null       { border: 2px double; }
}

pd.css

p {
border: 2px solid;
}

cla.html

<!DOCTYPE html>
<html>
<head>
<title>if Directive Demo</title>
<link rel="stylesheet" type="text/css" href="pd.css"/>
</head>
<body>
<div class="container">
<h2>Welcome to the page</h2>
<p>Worlds wiser thinkers are timeless. </p>
</div>
</body>
</html>

Output:

SASS if else-1.1

The paragraph border is reflected in the browser.

Example #2

In the below implementation the header <h2> is changed. using single if() which returns color case blue if the expression is true or else red.

ff.html

<html>
<head>
<title>Example demo on SASS conept</title>
<link rel = "stylesheet" type = "text/css" href = "new.css"/>
</head>
<body>
<h2>EDUCBA - Online Course</h2>
</body>
</html>

col.scss

h2 {
color: if( 3 + 2 == 5 , blue , red);
}

new.css

h2 {
color: blue;}

Execute HTML file finally to get the output in the browser.

Output:

SASS if else-1.2

Example #3 – Using else – if

new.html

<!DOCTYPE html>
<html>
<head>
<title>if -else Example</title>
<link rel="stylesheet" type="text/css" href="str.css"/>
</head>
<body>
<div class="container">
<h2>Welcome to EDUCBA</h2>
<p>Topic on SASS If- else Directive. </p>
</div>
</body>
</html>

file.scss (created a variable sample and the expression is checked.

$sample: 10;
p {
@if $sample > 3 {
color: brown;
} @else if $sample == 3 {
color: orange;
} @else {
color: red;
}
}

str.css

p {
color: brown;
}

Output:

SASS if else-3.1

Example #4

si.scss

body{
margin: 1;
padding: 0.5;
font-family: 'Times Roman';
font-size: 1.2em;
}
p {
padding: 13px 3px;
margin:1;
color: pink;
}
$Rose: if(true, gold, blue);
$Jasmine: if(false, papayawhip, darksalmon);
$Lotus: if(6 > 3, green, black);
p:nth-of-type(1) {
background: $Rose;
@if (lightness($Rose) > 80%) {
color: grey;
} @else {
color: white;
}
}
p:nth-of-type(2) {
@if 2 + 2 == 4 { background: $Jasmine;  }
@if 6 > 5      { border: 1px dotted $Lotus; }
@if true       { font-family: 'Dosis'; }
}
p:nth-of-type(3) {
background: $Lotus;
@if (lightness($Lotus) > 65%) {
color: red
} @else {
color: white;
}
}
p:nth-of-type(4) {
@if 4 + 4 == 8 { background: darken($Jasmine, 50%); }
@else if 5 + 5 == 10 { background: $Jasmine; }
@else if 7> 4      { border: 1px dotted $Lotus; }
}

col.css

body {
margin: 1;
padding: 0.5;
font-family: 'Times Roman';
font-size: 1.2em;
}
p {
padding: 13px 3px;
margin: 1;
color: pink;
}
p:nth-of-type(1) {
background: gold;
color: white;
}p:nth-of-type(2) {
background: darksalmon;
border: 1px dotted green;
font-family: 'Dosis';
}
p:nth-of-type(3) {
background: green;
color: white;
}
p:nth-of-type(4) {
background: #56200e;
}

sc.html

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="col.css">
</head>
<body>
<p>This is yellow color with rose title.</p>
<p> @if statements demo.</p>
<p>This is greenish leaves.</p>
<p>@if and else together example.</p>
</body>
</html>

Output:

SASS if else-4.1

Conclusion

Therefore we have seen how to implement if and else construct using Sass, which makes writing CSS easier. With less code and with limited time all the execution is performed perfectly. It prominently provides a lot of features which makes front-end development flexible. Therefore, it makes the coding process simple and efficient.

Recommended Articles

This is a guide to SASS if-else. Here we also discuss the definition and how if-else works in sass along with different examples and its code implementation. You may also have a look at the following articles to learn more –

  1. SASS vs LESS
  2. SASS Nesting
  3. SASS @at-root
  4. SASS @media?

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
CSS Tutorial
  • sass
    • How to Install SASS
    • SASS Interview Questions
    • What is Sass
    • SASS Comments
    • Sass Variables
    • SASS Import
    • SASS if else
    • SASS Nesting
    • SASS @each
    • SASS @at-root
    • SASS @extend
    • SASS @media
    • SASS @for
    • SASS Map
    • SASS Selectors
    • SASS Color Functions
    • SASS Mixins
  • CSS
    • Introduction To CSS
    • What is CSS?
    • Uses Of CSS
    • Advantages of CSS
    • Career In CSS
    • CSS Commands
    • Is Css Case Sensitive
    • CSS object-fit
    • Types of CSS Selectors
    • CSS Radio Button
    • CSS Attribute Selector
    • CSS first child of class
    • CSS Selector nth Child
    • CSS Parent Selector
    • CSS Child Selector
    • CSS Not Selector
    • CSS Descendant Selector
    • CSS Inline Style
    • Checkbox CSS
    • CSS Appearance
    • CSS Font Properties
    • CSS font-variant
    • CSS Pagination
    • CSS Table Styling
    • CSS Table Cell Padding
    • CSS Padding Color
    • CSS Text Formatting
    • CSS for Text-Shadow
    • CSS text-stroke
    • CSS text-indent
    • CSS Rotate Text
    • CSS Text Color
    • CSS Center Div
    • CSS Arrow
    • CSS Arrow Down
    • CSS offset
    • CSS Cursor
    • CSS Layout
    • CSS Grid Layout
    • Button in CSS
    • CSS Button Border
    • text-align in CSS
    • CSS Horizontal Align
    • CSS Position
    • CSS Box Sizing
    • CSS box-shadow
    • CSS Text Underline
    • CSS Text Outline
    • CSS Blinking Text
    • Text Decoration CSS
    • CSS Vertical Align
    • CSS Word Wrap
    • CSS Padding
    • CSS Font Color
    • CSS Color Generator
    • CSS Margin Right
    • CSS Margin Color
    • CSS Color Codes
    • CSS Color Transparent
    • CSS Color Chart
    • CSS Link Color
    • CSS z-index
    • CSS Curved Border
    • CSS Border Left
    • CSS left
    • CSS Gradient Generator
    • Radial Gradient in CSS
    • CSS Shape Generator
    • CSS Triangle Generator
    • CSS background-color
    • CSS Background Image
    • CSS background-clip
    • CSS background-blend-mode
    • CSS Drop Shadow
    • CSS line height
    • CSS line break
    • Sticky Footer CSS
    • CSS Header Design
    • CSS Border Style
    • CSS Border Generator
    • Sticky Sidebar CSS
    • CSS Transparent Border
    • CSS Border Radius
    • CSS translate
    • CSS transform
    • CSS 3D Transforms
    • CSS Text Transform
    • CSS Transition Effects
    • CSS Transition Property
    • CSS Animation Transition
    • Negative Margin CSS
    • CSS Navigation Bar
    • CSS Overflow
    • CSS overflow-wrap
    • CSS Lists
    • CSS list-style
    • CSS Order
    • CSS Box Model
    • CSS Inner Border
    • CSS Icon
    • Menu Icon CSS
    • CSS Multiple Borders
    • Opacity in CSS
    • CSS Float Right
    • CSS Clear Float
    • CSS clip
    • CSS disabled
    • CSS Border Padding
    • Border Images in CSS
    • CSS Visibility
    • CSS Validator
    • CSS Clearfix
    • CSS Counter
    • CSS Letter Spacing
    • CSS root
    • CSS zoom
    • CSS calc()
    • CSS.supports()
    • CSS Loader
    • Media Query CSS
    • CSS @keyframes
    • CSS @bottom
    • CSS page-break-after Property
    • CSS page-break
    • CSS Position Fixed
    • CSS skew()
    • CSS Row
    • CSS Masking
    • CSS Scrollbar
    • CSS Overlay
    • CSS Important
    • CSS Cursor Hand
    • CSS Inherit
    • CSS Position Relative
    • CSS Compressor
    • CSS tricks
    • CSS Outline Property
    • CSS Flexbox Properties
    • CSS flex-direction
    • CSS content property
    • CSS Typography
    • CSS Formatter
    • CSS nowrap
    • CSS Column
    • GridView CSS
    • CSS Viewport
    • CSS Minify
    • CSS Combinators
    • CSS in React
    • CSS Matrix 
    • CSS Pseudo Elements
    • CSS Pseudo Classes
    • CSS Pointer Events
    • CSS Resize
    • CSS Inheritance
    • CSS Interview Questions
    • Cheat Sheet CSS
  • CSS3
    • What is CSS3?
    • CSS3 Interview Questions
    • Cheat sheet CSS3

Related Courses

CSS Training Course

Bootstrap Training Course

JQuery Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) Learn More