Home Sass Parent selector Sass Parent Selector Definition
Definition and Usage The parent selector & refers to the outer selector inside a nested rule. Sass replaces & with that outer selector instead of the default descendant join (a space).
Official sample idea. &:hover becomes .alert:hover; [dir=rtl] & becomes [dir=rtl] .alert.
BEM suffixes. If the parent ends with a class/ID/element name, append text: &__copy, &--open.
Not span&. & must start a compound selector where a type selector would be allowed. span& is invalid.
Syntax
Syntax .alert {
&:hover { ... } // .alert:hover
&.is-active { ... } // .alert.is-active
[dir=rtl] & { ... } // [dir=rtl] .alert
:not(&) { ... } // :not(.alert)
&__copy { ... } // .alert__copy
}
// In SassScript: & is the current parent (or null outside rules)
@if & { ... } Pattern: nest under a parent → place & where the outer selector should appear → compile attached selectors.
Code
SCSS Examples Open View Compiled CSS for verified Dart Sass output (matches official Sass docs samples where shown).
1. Official 2. BEM 3. Modifier 4. Mixin 5. unify Example 1 — Official Parent Samples Pseudo-class, outer context, and :not(&).
.alert {
&:hover {
font-weight: bold;
}
[dir=rtl] & {
margin-left: 0;
margin-right: 10px;
}
:not(&) {
opacity: 0.8;
}
} .alert:hover {
font-weight: bold;
}
[dir=rtl] .alert {
margin-left: 0;
margin-right: 10px;
}
:not(.alert) {
opacity: 0.8;
} How It Works &:hover attaches a pseudo-class.[dir=rtl] & places the parent after a context.:not(&) passes the parent into a pseudo-class.Matches the official Sass docs sample.Example 2 — BEM Suffixes Append __ / -- text when the parent ends with a name.
.accordion {
max-width: 600px;
&__copy {
display: none;
&--open {
display: block;
}
}
} .accordion {
max-width: 600px;
}
.accordion__copy {
display: none;
}
.accordion__copy--open {
display: block;
} How It Works &__copy appends to .accordion.Nested &--open appends to .accordion__copy.No space between block and element.Matches the official BEM-style sample idea.Example 3 — Same-Element Modifier Attach another class to the parent with &.is-active.
.btn {
color: #111;
&.is-active {
color: #000;
}
} .btn {
color: #111;
}
.btn.is-active {
color: #000;
} How It Works Without & , nesting would make a descendant.&.is-active keeps both classes on one element.Result is .btn.is-active.Common for state classes.Example 4 — Detect Nesting in a Mixin Outside a rule, & is null (falsey)—branch accordingly.
@mixin app-background($color) {
@if & {
&.app-background {
background-color: $color;
}
} @else {
.app-background {
background-color: $color;
}
}
}
@include app-background(#036);
.sidebar {
@include app-background(#c6538c);
} .app-background {
background-color: #036;
}
.sidebar.app-background {
background-color: #c6538c;
} How It Works Root include sees & == null.Nested include sees the parent selector.&.app-background attaches when nested.Matches the official mixin-detection idea.Example 5 — selector.unify + @at-root Advanced: combine & with an element selector without double-nesting.
@use "sass:selector";
@mixin unify-parent($child) {
@at-root #{selector.unify(&, $child)} {
@content;
}
}
.wrapper .field {
@include unify-parent("input") {
border: 1px solid;
}
} .wrapper input.field {
border: 1px solid;
} How It Works unify merges & with input.@at-root prevents wrapping the outer selector again.Result is .wrapper input.field.Details on selector.unify() . Edge Cases & Pitfalls Most & bugs are forgetting it for states or misplacing it in a compound selector.
space Default nesting adds a space Without &, nested :hover is not the same as &:hover. See nesting .
span& Cannot suffix a type onto & span& is invalid. Keep & at the start of the compound selector.
null & is null outside style rules Useful in mixins; do not assume a parent always exists.
@at-root Interpolating & still nests Use @at-root when combining with selector functions so Sass does not wrap again.
Toolchain
Sass Support Parent selector is core Sass nesting on Dart Sass.
Feature Status & in nested selectorsSupported BEM-style suffixes Supported when parent ends with a name & outside nestingDart Sass 1.100+ emits plain CSS & SassScript & Parent selector value, or null outside rules
Compile with the Dart Sass CLI .
Remember
Key Takeaways & stands in for the outer selector.
&:hover / &.mod / &__el — no extra space.
Context & like [dir=rtl] & places the parent later.
null outside rules — great for mixin branches.
One line: use & whenever nesting should attach to the parent instead of creating a descendant.
Practice
Compile Notes Practical checklist:
1. Nest under a parent rule
2. Write &:hover / &.is-active / &__el as needed
3. Put context selectors before & when required
4. Remember & is null outside style rules
5. Use @at-root with selector functions that interpolate & Frequently Asked Questions What is the Sass parent selector? The ampersand & is a special selector that refers to the outer (parent) selector inside a nested rule. Sass replaces & with that outer selector instead of using normal nesting.
How do I write a hover state with &? Nest &:hover { ... } inside the parent rule. .button { &:hover { opacity: 0.9; } } compiles to .button:hover { opacity: 0.9; }.
What are BEM-style suffixes with &? If the outer selector ends with a class, ID, or element name, you can append text: &__element and &--modifier become .block__element and .block__element--modifier.
Can I put & after another selector? Yes for context. [dir=rtl] & becomes [dir=rtl] .alert. But span& is not allowed—& must start a compound selector where a type selector would be allowed.
What does & mean in SassScript? As an expression, & returns the current parent selector as a list of lists of unquoted strings. Outside any style rule it returns null, which is useful to detect whether a mixin was included inside a rule.
Why do I need @at-root with selector.unify(&, ...)? When you interpolate &, Sass still nests the outer selector automatically. @at-root tells Sass not to wrap again, so advanced selector math stays correct.
Next: Sass Placeholder Selectors Create silent % selectors to share styles with @extend.
Placeholders → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion