Sass selector.parse() Function

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

What You’ll Learn

selector.parse() belongs to the built-in sass:selector module. It turns a selector string into Sass’s selector value format—a nested list you can inspect, loop, and pass to other selector helpers. This page covers that structure and five compiled examples.

01

Concept

String → selector list

02

Module

@use "sass:selector"

03

Returns

Selector (list)

04

Args

$selector

05

Structure

complex / compound

06

Practice

5 examples

What Is selector.parse()?

Official docs: returns $selector in the selector value format. Whenever sass:selector returns a selector, it is always a comma-separated list (the selector list) that contains space-separated lists (the complex selectors) that contain unquoted strings (the compound selectors).

For example, .main aside:hover, .sidebar p becomes two complex selectors: .main aside:hover and .sidebar p. The first complex selector has two compounds: .main and aside:hover.

  • Input may already be a string or another selector value—parse normalizes it.
  • Other selector functions often accept strings directly; parse shines for inspection and loops.
  • Interpolating the result usually prints a normal CSS selector string again.
💡
Beginner tip

Think of parse as “split the selector into Sass lists.” Commas separate complex selectors; spaces inside one complex selector separate compounds.

📝 Syntax

styles.scss
@use "sass:selector";

// Recommended
selector.parse($selector)

// Legacy global name
selector-parse($selector)

Parameters

ParameterTypeRequiredDescription
$selectorSelector / stringYesSelector to convert into the Sass selector value format.

Return value

TypeResult
Selector (list)Comma list of complex selectors; each complex selector is a space-separated list of compound strings

📦 Loading sass:selector

styles.scss
// Recommended — namespaced (Dart Sass 1.23+)
@use "sass:selector";
$parsed: selector.parse(".main aside:hover, .sidebar p");

// Optional — bring members into scope
@use "sass:selector" as *;
$parsed: parse(".card .title");

// Legacy global
$parsed: selector-parse(".card .title");

🎨 Official Pattern & Structure

styles.scss
@use "sass:selector";

// Conceptual shape from the docs:
// ((unquote(".main") unquote("aside:hover")),
//  (unquote(".sidebar") unquote("p")))
$parsed: selector.parse(".main aside:hover, .sidebar p");
LevelSeparatorExample piece
Selector listComma.main aside:hover | .sidebar p
Complex selectorSpace.main then aside:hover
Compound selectorUnquoted string like .main

⚖️ When Do You Need parse?

GoalNeed parse?
Call nest / append with a stringUsually no—strings are accepted
Count complex selectors / walk compoundsYes—use list helpers on the parsed value
Loop each complex selector with @eachYes
Normalize mixed inputs in a mixin APIOften yes

🛠 Compatibility

This is about Sass compilers, not browsers. Parsing happens at compile time.

Implementationparse
Dart SassYes — prefer selector.parse (module since 1.23.0)
LibSassLegacy selector-parse may exist; no @use "sass:selector"
Ruby SassLegacy selector-parse 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";
Parse a stringselector.parse(".a .b, .c")
Count complex selectorslist.length(selector.parse($sel))
First complex selectorlist.nth(selector.parse($sel), 1)
Use with nestselector.nest(selector.parse(".card .title"), "&:hover")
Print as CSS text#{selector.parse($sel)} or meta.inspect(…)

Examples Gallery

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

📚 Getting Started

Official sample, then walk the nested lists.

Example 1 — Official Parse Sample

Parse a selector list and inspect type + printed form.

styles.scss
@use "sass:meta";
@use "sass:selector";

$parsed: selector.parse(".main aside:hover, .sidebar p");

.probe {
  --inspect: #{meta.inspect($parsed)};
  --type: #{meta.type-of($parsed)};
}

How It Works

The value is a Sass list. When printed into CSS, it looks like the original selector string again—the nested structure is for Sass code, not for browsers.

Example 2 — Walk Complex & Compound Parts

Use sass:list to measure both levels of the official sample.

styles.scss
@use "sass:list";
@use "sass:meta";
@use "sass:selector";

$parsed: selector.parse(".main aside:hover, .sidebar p");
$first: list.nth($parsed, 1);

.probe {
  --complex-count: #{list.length($parsed)};
  --compounds-in-first: #{list.length($first)};
  --compound-1: #{meta.inspect(list.nth($first, 1))};
  --compound-2: #{meta.inspect(list.nth($first, 2))};
}

How It Works

Two complex selectors (comma). The first has two compounds (space): .main and aside:hover.

📈 Practical Patterns

Feed parsed values into other helpers, then loop complexes.

Example 3 — Parse Then Nest

Parse a descendant selector, then nest a parent pseudo onto it.

styles.scss
@use "sass:selector";

$parsed: selector.parse(".card .title");

#{selector.nest($parsed, "&:hover")} {
  color: teal;
}

How It Works

Parsed selectors are first-class inputs for selector.nest. Here &:hover attaches to the whole complex selector.

Example 4 — String vs Parsed Input

Many helpers accept either—parse when you need the list form.

styles.scss
@use "sass:selector";

.compare {
  --from-string: #{selector.append(".btn", ".primary")};
  --from-parsed: #{selector.append(selector.parse(".btn"), ".primary")};
}

How It Works

Results match. Use the string path for simple calls; parse when you will also walk or transform the structure.

Example 5 — Loop Each Complex Selector

Parse a list, then @each over the complex selectors.

styles.scss
@use "sass:meta";
@use "sass:selector";

@mixin dump-complex($selector) {
  $parsed: selector.parse($selector);
  @each $complex in $parsed {
    .dump {
      content: meta.inspect($complex);
    }
  }
}

@include dump-complex(".hero h1, .hero p");

How It Works

The comma splits the outer list. Each iteration sees one complex selector (.hero h1, then .hero p).

🚀 Real-World Use Cases

  • Mixin APIs — accept a selector string, parse once, then branch on structure.
  • Validation — count complex selectors or compounds before generating CSS.
  • Codegen loops@each over complexes to emit per-branch rules.
  • Debugging — pair with meta.inspect / meta.type-of.
  • Pipeline helpers — normalize input before nest, append, or extend.

🧠 How Compilation Works

1

Pass a selector

Call selector.parse(".main aside:hover, .sidebar p").

Source
2

Sass builds nested lists

Comma → complex selectors; space → compounds inside each complex.

Compile
3

Inspect or transform

Use list, @each, or other selector helpers.

Use
4

CSS ships

Browsers only see finished selector strings, never the Sass list structure.

⚠️ Common Pitfalls

  • Expecting a plain string typemeta.type-of reports list.
  • Parsing only to call nest/append — those helpers already accept strings.
  • Confusing commas and spaces — commas split complexes; spaces split compounds.
  • Printing structure with inspect — inspect often looks like CSS text; use list.nth to see parts.
  • Old compilers without modules — use Dart Sass 1.23+ for @use "sass:selector".

💡 Best Practices

✅ Do

  • Use @use "sass:selector" and selector.parse()
  • Parse when you need structure, loops, or normalization
  • Pair with sass:list for length / nth
  • Pass parsed values into other selector helpers when useful
  • Prefer Dart Sass 1.23+

❌ Don’t

  • Parse every string “just in case”
  • Assume browsers see the nested list format
  • Forget that a single complex selector is still a comma list of length 1
  • Confuse compounds with simple selectors (simple-selectors is a different helper)
  • Rely on LibSass for the module API

Key Takeaways

Knowledge Unlocked

Five things to remember about selector.parse()

Turn selector strings into Sass lists you can inspect, loop, and transform.

5
Core concepts
📦 02

Module

sass:selector

@use
🔗 03

Shape

list of lists

Format
📚 04

Tools

list + @each

Inspect
05

Print

looks like CSS again

Emit

❓ Frequently Asked Questions

selector.parse($selector) returns $selector in the Sass selector value format: a comma-separated list of complex selectors, each a space-separated list of compound selectors (unquoted strings).
Most selector helpers accept strings. parse is useful when you want to inspect structure with list.length / list.nth, loop complex selectors, or normalize input before further selector work.
parse(".main aside:hover, .sidebar p") becomes a list of two complex selectors: (.main aside:hover) and (.sidebar p). Each complex selector is itself a list of compounds.
Yes. selector-parse($selector) is the legacy global name. Prefer selector.parse after @use "sass:selector".
Yes. Interpolating a parsed selector with #{$parsed} (or meta.inspect) usually prints a normal selector string again.
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 document the selector value format once for the whole sass:selector module—parse is the helper that turns a plain string into that shared nested-list shape.

Conclusion

selector.parse() converts a selector into Sass’s nested list format so you can inspect compounds, loop complex selectors, and feed structured values into other sass:selector helpers. Skip it when a plain string already works.

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

Next: replace selectors

Learn selector.replace() to swap selector pieces with @extend-style unification.

selector.replace() →

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