SASS/SCSS / Introduction to SASS/SCSS

Introduction to SASS and SCSS

This tutorial introduces the basics of using SASS and SCSS, powerful CSS pre-processors that can make your stylesheets more readable, maintainable, and DRY.

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

Covers the basics of SASS and SCSS, including their advantages over CSS.

Introduction

This tutorial aims to introduce you to SASS and SCSS, two powerful CSS pre-processors that help in writing clean, easy-to-maintain, and DRY (Don't Repeat Yourself) CSS code. By the end of this tutorial, you will be able to understand the basics of SASS/SCSS, write SASS/SCSS code, and compile it to CSS.

Prerequisites: Basic knowledge of HTML and CSS is required.

Step-by-Step Guide

SASS (Syntactically Awesome Stylesheets) and SCSS (Sassy CSS) are both CSS pre-processors. They allow you to use features that don't exist in CSS yet like variables, nesting, mixins, inheritance, and more.

SASS vs SCSS
- SASS has a loose syntax with white space and no semicolons, the syntax is similar to Python.
- SCSS syntax is similar to CSS. It uses brackets and semicolons.

Installation
To start with SASS/SCSS, you need to have Node.js installed on your machine. Then, install SASS globally by running the following command in your terminal:

npm install -g sass

Compiling
SASS/SCSS files cannot be directly used in a browser. They need to be compiled to CSS. To compile your SCSS file to CSS, use this command:

sass input.scss output.css

Using Variables
In SASS/SCSS, you can use variables to store colors, font stacks, or any CSS value that you want to reuse.

$font-stack: Helvetica, sans-serif;
$body-color: #333;

body {
  font: 100% $font-stack;
  color: $body-color;
}

Code Examples

1. Nesting
SASS/SCSS allows you to nest your CSS selectors in a way that follows the same visual hierarchy of your HTML.

nav {
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }

  li { display: inline-block; }

  a {
    display: block;
    padding: 0 10px;
    text-decoration: none;
  }
}

When compiled to CSS, it becomes:

nav ul {
  margin: 0;
  padding: 0;
  list-style: none;
}
nav li {
  display: inline-block;
}
nav a {
  display: block;
  padding: 0 10px;
  text-decoration: none;
}

2. Mixins
A mixin lets you make groups of CSS declarations that you want to reuse throughout your site.

@mixin border-radius($radius) {
  -webkit-border-radius: $radius;
     -moz-border-radius: $radius;
      -ms-border-radius: $radius;
          border-radius: $radius;
}
.box { @include border-radius(10px); }

When compiled to CSS, it becomes:

.box {
  -webkit-border-radius: 10px;
  -moz-border-radius: 10px;
  -ms-border-radius: 10px;
  border-radius: 10px;
}

Summary

In this tutorial, we have learned the basics of SASS and SCSS, how to install them, and how to write SASS/SCSS code. We have also covered variables, nesting, and mixins.

For further learning, you can explore other features like inheritance, operators, functions, etc. You can refer to the official SASS documentation (https://sass-lang.com/guide) for more details.

Practice Exercises

  1. Create a SCSS file, declare a color variable, and use it to set the color of text and background.
  2. Create a mixin for a transition effect and apply it to a class.
  3. Use nesting to style a nested list.

Solutions
1.

$color: #ff0000;

body {
  color: $color;
  background: lighten($color, 50%);
}
@mixin transition($property) {
  transition: $property;
}

.button {
  @include transition(all 0.2s ease-in-out);
}
ul {
  list-style: none;

  li {
    margin-bottom: 10px;

    ul {
      padding-left: 20px;
    }
  }
}

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

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

Countdown Timer Generator

Create customizable countdown timers for websites.

Use tool

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

Interest/EMI Calculator

Calculate interest and EMI for loans and investments.

Use tool

EXIF Data Viewer/Remover

View and remove metadata from image files.

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