selector.nest() belongs to the built-in sass:selector module. It combines selectors as if you nested them in a stylesheet—usually with a descendant space, or with & for compounds and BEM. This page covers lists, vs selector.append, and five compiled examples.
01
Concept
Nest like SCSS
02
Module
@use "sass:selector"
03
Returns
Selector value
04
Args
$selectors...
05
Parent &
Allowed after first
06
Practice
5 examples
Concept
What Is selector.nest()?
Official docs: combines $selectors as though they were nested within one another in the stylesheet. Without &, that usually means a descendant combinator (a space): ul li, not ulli.
Takes one or more selector arguments ($selectors...).
Placeholder selectors are allowed.
Unlike most selector functions, every argument except the first may contain parent selectors (&).
See also selector.append() when you want no space between pieces.
💡
Beginner tip
Picture writing nested SCSS, then flattening it with a function. nest(".alert", "&:hover") is the same idea as nesting &:hover inside .alert.
Foundation
📝 Syntax
styles.scss
@use "sass:selector";
// Recommended
selector.nest($selectors...)
// Legacy global name
selector-nest($selectors...)
Parameters
Parameter
Type
Required
Description
$selectors...
Selectors
Yes (one or more)
Selectors to nest. Later args may include &; the first usually should not.
Return value
Type
Result
Selector
A nested selector (often usable inside #{…})
Modules
📦 Loading sass:selector
styles.scss
// Recommended — namespaced (Dart Sass 1.23+)
@use "sass:selector";
$sel: selector.nest("ul", "li"); // ul li
// Optional — bring members into scope
@use "sass:selector" as *;
$sel: nest(".alert", "&:hover"); // .alert:hover
// Legacy global
$sel: selector-nest("ul", "li");
Details
🎨 Official Patterns
Call
Result
nest("ul", "li")
ul li
nest(".alert, .warning", "p")
.alert p, .warning p
nest(".alert", "&:hover")
.alert:hover
nest(".accordion", "&__copy")
.accordion__copy
The last two show why & matters: without it, nesting would insert a space; with it, you get compounds and BEM-style names—the same way nested SCSS works.
Compare
⚖️ nest vs append
Helper
Typical result
Use when
selector.nest
.alert .warn or .alert:hover with &
Nested stylesheet behavior / parent selectors
selector.append
.alert.warn (no space; no &)
Same element / glue without nesting rules
For BEM, both can produce .block__el: nest with &__el, or append with __el. Prefer the style that matches how you think about nesting.
Support
🛠 Compatibility
This is about Sass compilers, not browsers. The combination runs at compile time.
Implementation
nest
Dart Sass
Yes — prefer selector.nest (module since 1.23.0)
LibSass
Legacy selector-nest may exist; no @use "sass:selector"
Ruby Sass
Legacy selector-nest may exist; no @use "sass:selector"
Prefer current Dart Sass so the module API matches the official docs.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Import selector
@use "sass:selector";
Descendant
selector.nest("ul", "li")
List parent
selector.nest(".a, .b", "p")
Pseudo with &
selector.nest(".alert", "&:hover")
BEM with &
selector.nest(".block", "&__el")
No space / no &
selector.append(…) instead
Hands-On
Examples Gallery
Each example uses selector.nest at compile time (Dart Sass 1.23+). Open View Compiled CSS for verified output.
Each loop item includes &, so results stay compounds on .nav—elements and a hover state in one pass.
Applications
🚀 Real-World Use Cases
Programmatic nesting — build descendant selectors from string parts in mixins.
State helpers — nest &:hover, &:focus-visible, or &.is-open.
BEM tooling — nest &__el / &--mod onto a block name.
List expansion — nest children under comma-separated parents in one call.
Design-system APIs — accept a base selector and return nested variants at compile time.
🧠 How Compilation Works
1
Pass nested pieces
Call selector.nest(".card", ".title") or use &.
Source
2
Sass nests them
Applies the same rules as nested SCSS, including parent substitution.
Compile
3
Interpolate into rules
Use #{…} as the selector for a style block.
Emit
4
✓
CSS ships
Browsers only see finished selectors like .card .title or .btn:hover.
Watch Out
⚠️ Common Pitfalls
Expecting no space — without &, nest usually inserts a descendant space; use append or &.
Putting & in the first argument — parent selectors belong in later arguments.
Passing :hover without & — that nests as a descendant, not a compound.
Forgetting interpolation — wrap results in #{…} to use them as rule selectors.
Old compilers without modules — use Dart Sass 1.23+ for @use "sass:selector".
Pro Tips
💡 Best Practices
✅ Do
Use @use "sass:selector" and selector.nest()
Use & for compounds, pseudos, and BEM on the same element
Use nest for descendant / nested-stylesheet behavior
Prefer append when you want glue with no nesting rules
Prefer Dart Sass 1.23+
❌ Don’t
Assume every nest call inserts a space—check for &
Put parent selectors in the first argument
Confuse nest with string concatenation
Expect browsers to evaluate the call
Rely on LibSass for the module API
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about selector.nest()
Combine selectors like nested SCSS—spaces by default, & when you need compounds.
5
Core concepts
📝01
API
nest($selectors...)
Call
📦02
Module
sass:selector
@use
🔗03
Default
descendant space
Output
📚04
Parent
& after first arg
Detail
✓05
Alt
append for no space
Compare
❓ Frequently Asked Questions
selector.nest($selectors...) combines selectors as though they were nested within one another in the stylesheet. Example: nest("ul", "li") becomes ul li.
nest follows nesting rules (often a descendant space, or & for compounds). append glues pieces with no space: append(".alert", ".warn") is .alert.warn, while nest(".alert", ".warn") is .alert .warn.
Yes for every argument except the first. Official docs: unlike other selector functions, all of them except the first may also contain parent selectors. Example: nest(".alert", "&:hover") becomes .alert:hover.
Nesting expands across lists like nested SCSS. nest(".alert, .warning", "p") becomes .alert p, .warning p.
Yes. selector-nest($selectors...) is the legacy global name. Prefer selector.nest after @use "sass:selector".
Dart Sass 1.23+. LibSass and Ruby Sass do not load sass:selector with @use; they may still offer the legacy global name.
Did you know?
Official Sass docs call out that selector.nest is special: it is one of the few selector helpers that allows parent selectors—on every argument except the first.
selector.nest() combines selectors like nested SCSS: descendants by default, compounds and BEM when you pass & after the first argument. Reach for selector.append when you want glue without nesting rules.