Sass Selector Functions
Sass treats selectors as data you can inspect and combine. The sass:selector module provides functions to nest, append, unify and parse selectors. Most stylesheets never need them, but they are powerful inside mixins that build selectors dynamically — and they explain how nesting and & work under the hood.
The functions
| Function | What it does | Example → Result |
|---|---|---|
selector.nest($sels...) |
Nests selectors like Sass nesting does | nest(".a", ".b") → .a .b |
selector.append($sels...) |
Joins without a space | append(".btn", "--lg") → .btn--lg |
selector.unify($a, $b) |
A selector matching both | unify("a", ".x") → a.x |
selector.is-superselector($super, $sub) |
Does $super match everything $sub does? |
(".a", ".a.b") → true |
selector.replace($sel, $orig, $new) |
Swaps part of a selector | |
selector.extend($sel, $extendee, $extender) |
Applies @extend logic to a selector |
|
selector.parse($sel) |
Converts a string to selector data | |
selector.simple-selectors($sel) |
Splits a compound selector | ".a.b" → .a, .b |
nest and append
@use "sass:selector";
.demo {
--nest: #{selector.nest(".card", ".title")};
--nest-parent: #{selector.nest(".card", "&:hover")};
--nest-list: #{selector.nest(".a, .b", ".c")};
--append: #{selector.append(".btn", "--primary")};
--append-list: #{selector.append(".x, .y", ":focus")};
}.demo {
--nest: .card .title;
--nest-parent: .card:hover;
--nest-list: .a .c, .b .c;
--append: .btn--primary;
--append-list: .x:focus, .y:focus;
}
selector.nest(".a, .b", ".c") returns .a .c, .b .c — exactly what nesting .c inside a rule for .a, .b would produce.
Using selector functions with @at-root
@at-root writes a rule at the root of the stylesheet instead of nesting it. Combined with selector functions, you can build exact selectors from inside a nested rule:
@use "sass:selector";
@mixin when-tag($tag) {
// Turn ".field" into "input.field"
@at-root #{selector.unify(&, $tag)} {
@content;
}
}
.field {
border: 1px solid #ccc;
@include when-tag(input) {
padding: 6px 10px;
}
@include when-tag(textarea) {
min-height: 80px;
}
}.field {
border: 1px solid #ccc;
}
input.field {
padding: 6px 10px;
}
textarea.field {
min-height: 80px;
}
Without selector.unify, writing input#{&} would have to know the parent's exact shape. unify handles compound and list selectors correctly.
Reading the parent selector & in SassScript
Inside a rule, & used in an expression returns the current selector as a list. If there is no parent, it is null — handy for mixins that work both at the root and nested:
@mixin show-selector {
@if & {
--selector: "#{&}";
} @else {
:root { --selector: "root level"; }
}
}
@include show-selector;
.nav .item {
@include show-selector;
}:root {
--selector: "root level";
}
.nav .item {
--selector: ".nav .item";
}
is-superselector and unify
@use "sass:selector";
.checks {
--super: #{selector.is-superselector(".btn", ".btn.primary")};
--not-super: #{selector.is-superselector(".btn.primary", ".btn")};
--unify: #{selector.unify(".btn", ".active")};
--impossible: #{selector.unify("#a", "#b") == null};
}.checks {
--super: true;
--not-super: false;
--unify: .btn.active;
--impossible: true;
}
selector.unify("#a", "#b") returns null, because no element can have two different IDs.
Tip: If you only need
.parent:hoveror.block--modifier, plain nesting with&is clearer than any selector function. Reach forsass:selectorwhen you write reusable mixins that must work with any parent selector.
Test yourself
What does selector.append(".card", "__body") return?
- .card .__body
- .card__body
- .card, __body
- __body.card
What does & evaluate to in a mixin included at the root of a file (outside any rule)?
- An empty string
- null
- :root
- An error
Which directive outputs a rule at the stylesheet root from inside a nested rule?
- @root
- @at-root
- @top
- @global