SASS Nesting makes it easier and more efficient to write CSS by allowing you to nest your selectors inside one another, mimicking the structure of your HTML.
This removes the need to repeatedly write out parent selectors, improving readability and maintainability of your styles. SASS will compile the nested styles into standard CSS by automatically combining the selectors.
Basic Nesting in SASS
When writing nested selectors, you can structure your SASS code similarly to the HTML hierarchy. This allows for cleaner and more intuitive styling, especially for complex components or layouts.
Example 1: Basic Nesting
Let's look at a basic example of SASS nesting where we style an unordered list with nested list items and links.
SASS file:
ul {
list-style-type: none;
li {
display: inline-block;
a {
text-decoration: none;
display: inline-block;
padding: 10px 15px;
color: blue;
}
}
}
Compiled CSS file:
ul {
list-style-type: none;
}
ul li {
display: inline-block;
}
ul li a {
text-decoration: none;
display: inline-block;
padding: 10px 15px;
color: blue;
}
Nesting with Combinators
SASS also allows nesting with combinators like the adjacent sibling (+), child (>), and general sibling (~) combinators. These can be nested inside selectors for more complex relationships between elements.
Example 2: Nesting with Combinators
Here, we demonstrate how to use different combinators inside SASS nesting.
SASS file:
ul {
+ li {
display: inline-block;
}
}
li {
> {
a {
text-decoration: none;
}
}
}
p ~ {
span {
text-decoration-line: underline;
text-decoration-style: double;
}
}
Compiled CSS file:
ul + li {
display: inline-block;
}
li > a {
text-decoration: none;
}
p ~ span {
text-decoration-line: underline;
text-decoration-style: double;
}
Similar Reads
SASS Interpolation Interpolation is an insertion. Interpolation allows us to interpolate SASS expressions into a simple SASS or CSS code. This means you can define ( some part or the whole ) selector name, property name, CSS at-rules, quoted or unquoted strings, etc, as a variable. Interpolation is a new principle and
1 min read
SASS @if and @else SASS provides flow control mechanisms similar to JavaScript, allowing you to conditionally compile CSS code based on logical expressions. Using @if, @else, and @else if, you can create dynamic styles that change based on the values passed to your mixins or variables.@if DirectiveThe @if directive wo
3 min read
SASS Introduction Sass is the short form of Syntactically Awesome Style Sheet. It is an upgrade to CSS. Cascading Style Sheets (CSS) serve as the foundation of web styling, dictating the visual aesthetics and layout of websites. However, as web development projects increase in complexity, managing CSS files can becom
3 min read
SASS | Map Functions The SASS Map data-type is used to display one or more key-value pairs. Along with the map functions shown in the below lists, you can also use any of the SASS list functions with maps as well. The following lists contains all map functions in SASS: map-has-key($map, $key) function: This function ret
1 min read
Sass @function Rule Functions can define complex operations on SassScript values that can be reused throughout the stylesheet. It makes it easier to abstract out the common formulas and behaviors in some readable way. Functions can be defined using @function at-rule. Syntax: html @function <function name>(<arg
2 min read
SASS | List Functions The SASS list functions are used to modify new lists. The lists need to be created inside circular brackets in order to differentiate from others. SASS list can not be changed hence in some cases new lists are created.Just like the string functions, SASS lists are one-based (not zero-based) indexed
3 min read