Sass selector.append() Function

Intermediate
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
sass:selector · Dart Sass 1.23+

What You’ll Learn

selector.append() belongs to the built-in sass:selector module. It glues selectors together without a descendant space—perfect for compound selectors and BEM-style suffixes. This page covers selector lists, vs selector.nest, and five compiled examples.

01

Concept

Glue selectors (no space)

02

Module

@use "sass:selector"

03

Returns

Selector value

04

Args

$selectors...

05

vs nest

Append ≠ descendant

06

Practice

5 examples

What Is selector.append()?

Official docs: combines $selectors without descendant combinators—that is, without whitespace between them. Use it when you want a.disabled or .accordion__copy, not a .disabled.

  • Takes one or more selector arguments ($selectors...).
  • If any argument is a selector list, each complex selector is combined separately.
  • Placeholder selectors are allowed; parent selectors (&) are not.
  • See also selector.nest() when you want nesting / descendant behavior.
💡
Beginner tip

Think of tape, not folders: append sticks class names onto the same element. nest puts one selector inside another (usually with a space).

📝 Syntax

styles.scss
@use "sass:selector";

// Recommended
selector.append($selectors...)

// Legacy global name
selector-append($selectors...)

Parameters

ParameterTypeRequiredDescription
$selectors...SelectorsYes (one or more)Selectors to combine without descendant spaces. No parent selectors.

Return value

TypeResult
SelectorA combined selector (often usable inside #{…})

📦 Loading sass:selector

styles.scss
// Recommended — namespaced (Dart Sass 1.23+)
@use "sass:selector";
$sel: selector.append("a", ".disabled"); // a.disabled

// Optional — bring members into scope
@use "sass:selector" as *;
$sel: append("a", ".disabled");

// Legacy global
$sel: selector-append("a", ".disabled");

🎨 Official Patterns

CallResult
append("a", ".disabled")a.disabled
append(".accordion", "__copy").accordion__copy
append(".accordion", "__copy, __image").accordion__copy, .accordion__image

The list case is the key detail: each side of the comma is appended separately to .accordion.

⚖️ append vs nest

HelperTypical resultUse when
selector.append.alert.warn (no space)Same element / BEM suffix / compound class
selector.nest.alert .warn (descendant) or .alert:hover with &Nested stylesheet behavior / parent selectors

🛠 Compatibility

This is about Sass compilers, not browsers. The combination runs at compile time.

Implementationappend
Dart SassYes — prefer selector.append (module since 1.23.0)
LibSassLegacy selector-append may exist; no @use "sass:selector"
Ruby SassLegacy selector-append may exist; no @use "sass:selector"

Prefer current Dart Sass so the module API matches the official docs.

⚡ Quick Reference

GoalCode
Import selector@use "sass:selector";
Compound classselector.append("a", ".disabled")
BEM elementselector.append(".block", "__el")
List of suffixesselector.append(".block", "__a, __b")
Use as a selector#{selector.append(".btn", ".primary")} { … }
Need a space / &selector.nest(…) instead

Examples Gallery

Each example uses selector.append at compile time (Dart Sass 1.23+). Open View Compiled CSS for verified output.

📚 Getting Started

Official docs samples, then real CSS rules.

Example 1 — Official Append Samples

Compound class, BEM suffix, and a suffix list.

styles.scss
@use "sass:selector";

.probe {
  --disabled: #{selector.append("a", ".disabled")};
  --bem: #{selector.append(".accordion", "__copy")};
  --list: #{selector.append(".accordion", "__copy, __image")};
}

How It Works

No spaces are inserted. The comma list expands into two fully appended selectors.

Example 2 — Build Rule Selectors

Interpolate the result to create real style rules.

styles.scss
@use "sass:selector";

#{selector.append(".btn", ".primary")} {
  background: teal;
}

#{selector.append(".btn", ".primary, .danger")} {
  font-weight: 700;
}

How It Works

The second call appends each list item to .btn, producing a comma-separated rule list.

📈 Practical Patterns

Mixins, BEM loops, and a clear contrast with nest.

Example 3 — State / Pseudo Mixin

Append a modifier or pseudo-class onto a base selector.

styles.scss
@use "sass:selector";

@mixin state($base, $mod) {
  #{selector.append($base, $mod)} {
    outline: 2px solid currentColor;
  }
}

@include state(".card", ".focus");
@include state("a", ":hover");

How It Works

Classes and pseudo-classes both append onto the same compound selector—no descendant space.

Example 4 — BEM Elements Loop

Build several block__element selectors from one block name.

styles.scss
@use "sass:selector";

$block: ".menu";
$parts: "__item", "__link", "__icon";

@each $part in $parts {
  #{selector.append($block, $part)} {
    display: block;
  }
}

How It Works

Official accordion sample scaled up: each __* suffix is glued onto .menu.

Example 5 — Append vs Nest Side by Side

Same arguments, different combinators.

styles.scss
@use "sass:selector";

.compare {
  --append: #{selector.append(".alert", ".warn")};
  --nest: #{selector.nest(".alert", ".warn")};
}

How It Works

append keeps one compound selector. nest inserts a descendant combinator (space) between the two classes.

🚀 Real-World Use Cases

  • BEM tooling — build block__element and block--mod names from parts.
  • State helpers — append .is-active, :hover, or :focus-visible.
  • Component APIs — accept a base selector string and glue modifiers in mixins.
  • Selector lists — expand one prefix across several suffixes in one call.
  • Design-system codegen — loop tokens into consistent class names at compile time.

🧠 How Compilation Works

1

Pass selector pieces

Call selector.append(".btn", ".primary").

Source
2

Sass glues them

No descendant space; lists expand per complex selector.

Compile
3

Interpolate into rules

Use #{…} as the selector for a style block.

Emit
4

CSS ships

Browsers only see finished selectors like .btn.primary.

⚠️ Common Pitfalls

  • Expecting a space — use selector.nest for descendants.
  • Parent selectors& is not allowed in append arguments.
  • Forgetting interpolation — wrap results in #{…} to use them as rule selectors.
  • Accidental spaces in strings" .mod" can produce odd compounds; keep pieces clean.
  • Old compilers without modules — use Dart Sass 1.23+ for @use "sass:selector".

💡 Best Practices

✅ Do

  • Use @use "sass:selector" and selector.append()
  • Use it for compounds, BEM suffixes, and pseudos
  • Pass clean strings without leading spaces
  • Prefer list arguments for shared prefixes
  • Prefer Dart Sass 1.23+

❌ Don’t

  • Pass parent selectors (&)
  • Use append when you need a descendant combinator
  • Confuse it with string concatenation in every case—lists expand specially
  • Expect browsers to evaluate the call
  • Rely on LibSass for the module API

Key Takeaways

Knowledge Unlocked

Five things to remember about selector.append()

Glue selectors together without a descendant space—great for compounds and BEM.

5
Core concepts
📦 02

Module

sass:selector

@use
🔗 03

Result

no-space selector

Output
📚 04

Lists

combine separately

Detail
05

Alt

nest for spaces / &

Compare

❓ Frequently Asked Questions

selector.append($selectors...) combines selectors without descendant combinators—no whitespace between them. Example: append("a", ".disabled") becomes a.disabled.
append glues pieces together (a.disabled, .block__el). nest combines as if nested in the stylesheet, usually with a descendant space (ul li) unless you use &.
Official docs: if any argument is a selector list, each complex selector is combined separately. append(".accordion", "__copy, __image") becomes .accordion__copy, .accordion__image.
No. Official docs: arguments may contain placeholder selectors, but not parent selectors. Use selector.nest when you need &.
Yes. selector-append($selectors...) is the legacy global name. Prefer selector.append 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 highlight BEM-style suffixes like __copy because append is one of the few built-ins that builds those names without forcing a descendant combinator.

Conclusion

selector.append() combines selectors without descendant spaces—ideal for compounds, BEM suffixes, and pseudo-classes. Use lists to expand shared prefixes, avoid &, and reach for selector.nest when you need nesting behavior.

Continue with selector.extend() or the Sass introduction.

Next: extend selectors

Learn selector.extend() to rewrite selectors with @extend-style unification.

selector.extend() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful