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 …
Section overview
5 resourcesCovers 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:
- 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;
}
- 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
-
Create a SASS/SCSS file with nested selectors for a simple webpage layout (header, main, and footer).
-
Write SASS/SCSS code that includes nested selectors with pseudo-classes for a navigation menu.
-
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.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI 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 articleAI 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 articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article