selector.unify($selector1, $selector2) returns a selector that matches only elements matched by both arguments—or null if there is no expressible overlap. Load with @use "sass:selector".
Official sample idea.
unify("a", ".disabled") → a.disabled; unify("a", "h1") → null; complex pairs can expand into a list.
Guard null.
Store the result and check with @if $u before interpolating it as a CSS selector.
Complex caveat.
Like @extend, when both sides are complex the result is not guaranteed to cover every possible shared element. Need a coverage boolean instead? Use is-superselector().
Syntax
Syntax
SCSS
@use "sass:selector";
selector.unify($selector1, $selector2) //=> selector | null
selector.unify("a", ".disabled"); // a.disabled
selector.unify("a.disabled", "a.outgoing"); // a.disabled.outgoing
selector.unify("a", "h1"); // null
selector.unify(".warning a", "main a");
// .warning main a, main .warning a
// Safe use:
$u: selector.unify($a, $b);
@if $u {
#{$u} { ... }
}
// Global alias (legacy): selector-unify($selector1, $selector2)
// Prefer selector.unify after @use "sass:selector"
Pattern: pass two selectors → get overlap or null → guard before emitting CSS.
Guard with @if before interpolating as a CSS selector.
vs is-superselector — that one returns a boolean only.
Prefer @use "sass:selector" (Dart Sass 1.23+).
One line: to get a selector for the overlap of two selectors, use selector.unify($selector1, $selector2).
Practice
Compile Notes
Practical checklist:
checklist.txt
1. @use "sass:selector"
2. Pass two selectors to unify
3. Store the result in a variable
4. Guard with @if before #{$u}
5. Use is-superselector when you only need true/false
Frequently Asked Questions
selector.unify($selector1, $selector2) returns a selector that matches only elements matched by both selectors. Example: unify("a", ".disabled") becomes a.disabled.
It returns null if the two selectors do not match any of the same elements, or if there is no selector that can express their overlap. Example: unify("a", "h1") is null.
Complex pairs can produce a list of unified forms. Example: unify(".warning a", "main a") becomes .warning main a, main .warning a. The result is not guaranteed to cover every possible shared element when both sides are complex.
Yes. selector-unify($selector1, $selector2) is the legacy global name. Prefer selector.unify after @use "sass:selector".
Store the result in a variable and guard with @if $u { ... } before interpolating it as a selector.
Dart Sass 1.23+. LibSass and Ruby Sass do not load sass:selector with @use; they may still offer the legacy global name.