SCSS syntax basics

Learn nesting, variables, comments, and compilation in SCSS to write clean, efficient CSS.

🎯 Introduction

If you are new to frontend development and have heard of SCSS but are unsure how to use it, this article is for you. You will learn its basic syntax, variables, nesting, and comments, along with how to compile SCSS into CSS that browsers understand. These tools make styling more approachable, keep code clear, and help you work more efficiently.


🌿 Nesting: express selector relationships

In traditional CSS, selectors are written separately, which can obscure their relationships and make code harder to follow. SCSS nesting organizes selectors into a hierarchy, keeping styles easier to read and maintain.

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

        li {
            display: inline-block;

            a {
                display: block;
                padding: 6px 12px;
                text-decoration: none;
                color: blue;

                &:hover {
                    color: red;
                }
            }
        }
    }
}

This navigation example nests ul inside nav, li inside ul, and a inside li. The :hover pseudo-class uses & to reference the parent selector, keeping the code concise and easy to follow.

Nesting pseudo-classes 👻

For pseudo-classes such as :hover, :active, and :focus, SCSS uses & to reference the current parent selector without repeating its name.

button {
    background-color: green;
    color: white;

    &:hover {
        background-color: darkgreen;
    }

    &:active {
        background-color: limegreen;
    }
}

💡 Declaring and using variables: reduce repetition and simplify maintenance

Colors, font sizes, and spacing are often reused in CSS. SCSS variables let you manage these values in one place. Change a variable, and every reference uses the updated value when compiled.

$primary-color: #3498db;
$padding-base: 16px;
$font-stack: 'Helvetica Neue', Helvetica, Arial, sans-serif;

body {
    font-family: $font-stack;
    color: $primary-color;
    padding: $padding-base;
}

button {
    background-color: $primary-color;
    padding: $padding-base / 2;
    border: none;
    border-radius: 4px;
    color: white;
}

Variable names must start with $. Use meaningful English names to keep code readable and maintainable.

Variables can hold colors, dimensions, strings, and even calculation results, making your styles more flexible.

📝 Comments: single-line vs. multiline

Comments help explain your code. SCSS supports two comment styles:

// This single-line comment does not appear in the compiled CSS

/*
This is a multiline comment
It can contain several lines of explanation
By default, it is preserved in the generated CSS file
*/
Use single-line comments (//) for comments that should not appear in the final CSS. Multiline comments are preserved by default and contribute to file size.

🚀 Ways to compile SCSS

Browsers cannot interpret SCSS directly, so it must be compiled into standard CSS.

1. Koala (a graphical tool)

Koala is a cross-platform GUI compiler suited to frontend beginners and developers who are unfamiliar with the command line.

Download: https://koala-app.com

2. Dart-sass

Dart Sass is maintained by the Sass team, supports the latest syntax, and is the only Sass implementation under active official development.

npm install -g sass
sass input.scss output.css

3. The official Sass Playground

Sass provides an online compiler for trying code, testing syntax, and demonstrating features. It compiles SCSS into CSS in real time.

Visit: https://sass-lang.com/playground/

4. Compiling with webpack

For project development, integrate SCSS compilation with a build tool such as webpack. This automates style dependencies, hot updates, and minification, making it suitable for medium-sized and large projects.


❓ Questions and answers

Q1: How do I nest pseudo-classes in SCSS?

Use & to reference the parent selector, followed by the pseudo-class name, such as &:hover.

Q2: How do I declare an SCSS variable?

Use $variable-name: value;. The variable name must start with $.

Q3: Do multiline comments appear in the final CSS?

They are preserved by default. Use single-line comments (//) if you do not want to include them.


🎉 You now know how to use nesting, variables, and comments in SCSS, and how to compile your styles!