SASS/SCSS / Operators and Control Structures

Using Operators for Math in SASS/SCSS

This tutorial will guide you through the process of using mathematical operators in SASS/SCSS to perform arithmetic operations in your stylesheets.

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

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

Using Operators for Math in SASS/SCSS

1. Introduction

This tutorial will guide you through the process of using mathematical operators in SASS/SCSS. SASS/SCSS, which is a CSS preprocessor, allows you to use mathematical operations in your stylesheets, which is something that is not possible with regular CSS.

By the end of this tutorial, you would have learned how to perform basic arithmetic operations such as addition, subtraction, multiplication, and division in SASS/SCSS.

Prerequisites: Basic understanding of CSS and SASS/SCSS.

2. Step-by-Step Guide

SASS/SCSS supports the following mathematical operators:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulo (%)

When using these operators, it is important to remember that SASS/SCSS performs operations only on compatible units, such as those of the same type (for example, two lengths or two times) or numbers without units.

3. Code Examples

Addition and Subtraction

$base: 10px;
$padding: 5px;

div {
  width: $base + $padding; // 15px
  height: $base - $padding; // 5px
}

In the above example, we have two variables $base and $padding. We're adding and subtracting these variables to get the width and height of a div.

Multiplication and Division

$base: 10px;
$multiplier: 2;

div {
  width: $base * $multiplier; // 20px
  height: $base / $multiplier; // 5px
}

Here, we're multiplying and dividing the $base variable with the $multiplier to get the width and height of a div.

Modulo

$base: 10;
$divider: 3;

div {
  margin-left: $base % $divider; // 1px
}

In this example, we're using the modulo operator to find the remainder of the division operation.

4. Summary

In this tutorial, we have learned how to use mathematical operators in SASS/SCSS. This can be very useful in creating dynamic and responsive layouts.

Moving forward, you can explore more complex calculations and operations in SASS/SCSS. For additional learning, you can check the official SASS documentation here.

5. Practice Exercises

  1. Create a .scss file and declare a variable $base with a value of 15px. Use the variable to set the width of a div to $base + 5px and the height to $base - 5px.

  2. Declare two variables $base and $multiplier with values of 20px and 3, respectively. Use these variables to set the width of a div to $base * $multiplier and the height to $base / $multiplier.

  3. Declare two variables $base and $divider with values of 30 and 7, respectively. Use these variables to set the margin-left of a div to $base % $divider.

Solutions

$base: 15px;

div {
  width: $base + 5px; // 20px
  height: $base - 5px; // 10px
}
$base: 20px;
$multiplier: 3;

div {
  width: $base * $multiplier; // 60px
  height: $base / $multiplier; // approximately 6.66667px
}
$base: 30;
$divider: 7;

div {
  margin-left: $base % $divider; // 2px
}

Keep practicing until you're comfortable with these operators and can use them in real-world projects. Good luck!

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

QR Code Generator

Generate QR codes for URLs, text, or contact info.

Use tool

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

Random Number Generator

Generate random numbers between specified ranges.

Use tool

Image Converter

Convert between different image formats.

Use tool

JavaScript Minifier & Beautifier

Minify or beautify JavaScript code.

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