SASS/SCSS / Nesting in SASS/SCSS

Using Parent Selector with Nesting

In this tutorial, we'll explore the use of the parent selector (&) with nesting in SASS/SCSS. We'll cover how to use the & symbol to refer to the parent element, and how this can …

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Covers how to nest selectors in SASS/SCSS for better structure and readability.

1. Introduction

1.1 Tutorial Goal

In this tutorial, we aim to give you a detailed understanding of the parent selector (&) in SASS/SCSS and how to effectively use it with nesting. The parent selector can be a powerful tool in your SASS/SCSS workflow, as it can simplify your code and make it more DRY (Don't Repeat Yourself).

1.2 Learning Outcomes

By the end of this tutorial, you will be able to:

  • Understand the use of the parent selector (&) in SASS/SCSS.
  • Implement nesting with the parent selector (&).
  • Simplify your SASS/SCSS code by using the parent selector effectively.

1.3 Prerequisites

Before starting this tutorial, you should have a basic understanding of CSS and a working knowledge of SASS/SCSS.

2. Step-by-Step Guide

2.1 Parent Selector

The parent selector (&) in SASS/SCSS is used to reference the parent selector of a nested rule. It can be very useful for pseudo-classes, pseudo-elements, and modifier classes.

2.2 Nesting with Parent Selector

In SASS/SCSS, you can nest your CSS rules inside one another. This nesting can be combined with the parent selector (&) for more complex and dynamic styles.

3. Code Examples

3.1 Basic Usage of Parent Selector

.btn {
  &--primary {
    background-color: blue;
  }

  &--danger {
    background-color: red;
  }
}

In this example, the & symbol represents the parent selector (.btn). The output CSS will be:

.btn--primary {
  background-color: blue;
}

.btn--danger {
  background-color: red;
}

3.2 Using Parent Selector with Pseudo-Classes

.link {
  &::before {
    content: "";
  }

  &:hover {
    color: orange;
  }
}

In this example, the & symbol represents the parent selector (.link). The output CSS will be:

.link::before {
  content: "";
}

.link:hover {
  color: orange;
}

4. Summary

In this tutorial, we've gone over the parent selector (&) and how you can combine it with nesting in SASS/SCSS. You've learned how to use the & symbol to refer to the parent selector, and we've seen how this can simplify your code, making it more DRY.

5. Practice Exercises

5.1 Exercise 1

Given the following CSS, convert it to SASS/SCSS using the parent selector where appropriate:

.nav-item.active {
  color: red;
}

.nav-item.disabled {
  color: grey;
}

5.2 Solution 1

.nav-item {
  &.active {
    color: red;
  }

  &.disabled {
    color: grey;
  }
}

5.3 Exercise 2

Given the following CSS, convert it to SASS/SCSS using the parent selector where appropriate:

.button::after {
  content: '';
}

.button:hover {
  background-color: yellow;
}

5.4 Solution 2

.button {
  &::after {
    content: '';
  }

  &:hover {
    background-color: yellow;
  }
}

Keep practicing with the parent selector and nesting. Try it out on your projects and see how it can simplify your SASS/SCSS code!

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

EXIF Data Viewer/Remover

View and remove metadata from image files.

Use tool

AES Encryption/Decryption

Encrypt and decrypt text using AES encryption.

Use tool

Image Converter

Convert between different image formats.

Use tool

File Size Checker

Check the size of uploaded files.

Use tool

Watermark Generator

Add watermarks to images easily.

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