SASS/SCSS / Nesting in SASS/SCSS

How to Nest Selectors in SASS/SCSS

This tutorial will guide you through the process of nesting selectors in SASS/SCSS. You will learn how to create hierarchical relationships between selectors to reflect your HTML …

Tutorial 1 of 5 5 resources in this section

Section overview

5 resources

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

Introduction

The goal of this tutorial is to introduce you to the concept of nesting selectors in SASS/SCSS. By the end of this tutorial, you will be able to nest selectors to create hierarchical relationships that mirror your HTML structure. This will result in cleaner and more organized code.

The only prerequisites for this tutorial are basic knowledge of HTML, CSS, and a general understanding of SASS/SCSS.

Step-by-Step Guide

Nesting selectors in SASS/SCSS means that you can nest your CSS selectors in a way that follows the same visual hierarchy of your HTML. Be aware that excessive nesting can lead to more complex and specific selectors, which can be harder to maintain and can lead to unintended results. So it's best to keep nesting to a maximum of three levels deep.

Here's an example of how to nest selectors:

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

  li { display: inline-block; }

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

This compiles into the following CSS:

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

nav li { display: inline-block; }

nav a {
  display: block;
  padding: 6px 12px;
  text-decoration: none;
}

Code Examples

Here are some practical examples to help you understand the concept better:

  1. Nesting selectors with pseudo-classes:
.btn {
  padding: 5px 10px;
  text-decoration: none;

  &:hover {
    background: #f5f5f5;
  }
}

The & symbol refers to the parent selector. In this case, &:hover refers to .btn:hover.

This compiles to:

.btn {
  padding: 5px 10px;
  text-decoration: none;
}

.btn:hover {
  background: #f5f5f5;
}
  1. Nesting selectors with media queries:
.container {
  width: 100%;

  @media (min-width: 768px) {
    width: 750px;
  }
}

This compiles to:

.container {
  width: 100%;
}

@media (min-width: 768px) {
  .container {
    width: 750px;
  }
}

Summary

In this tutorial, you've learned how to nest selectors in SASS/SCSS, how to use the & symbol to refer to the parent selector, and how to nest media queries. These skills will help you write more organized and cleaner code.

To continue learning about SASS/SCSS, you could delve into topics like the use of variables, mixins, or functions. You can find more resources at the official SASS documentation.

Practice Exercises

  1. Create a SASS/SCSS file with nested selectors for a simple webpage layout (header, main, and footer).

  2. Write SASS/SCSS code that includes nested selectors with pseudo-classes for a navigation menu.

  3. Create a responsive grid layout where the number of columns changes based on the viewport size using media queries and nested selectors.

Here's a solution for the first exercise:

body {
  margin: 0;
  padding: 0;

  header {
    height: 50px;
    background: #333;
  }

  main {
    padding: 20px;
  }

  footer {
    height: 50px;
    background: #333;
  }
}

This will create a webpage layout with a header and footer of fixed height with a background color, and a main content area with some padding. You can try the other exercises yourself, and if you get stuck, you can always refer to the examples in this tutorial. Happy coding!

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

PDF Splitter & Merger

Split, merge, or rearrange PDF files.

Use tool

JSON Formatter & Validator

Beautify, minify, and validate JSON data.

Use tool

HTML Minifier & Formatter

Minify or beautify HTML code.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

File Size Checker

Check the size of uploaded 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