Sass string.unique-id()

Beginner
8 min read
Updated: Nov 2026
5 examples
Dart Sass 1.23+

Definition and Usage

string.unique-id() returns a randomly generated unquoted string that is a valid CSS identifier and unique within the current Sass compilation. Load with @use "sass:string".

Official sample idea.

Each call yields a new ID, e.g. uabtrnzug then u6w1b1def—exact characters change every compile.

Reuse with a variable.

Store the result once if the same class, keyframe, or ID must appear in more than one place.

Not stable across rebuilds.

Uniqueness is promised for one compilation only. Sample CSS below is illustrative—your output will differ.

Syntax

SCSS
@use "sass:string";

string.unique-id() //=> unquoted string (CSS identifier)

string.unique-id(); // e.g. uabtrnzug
string.unique-id(); // e.g. u6w1b1def  (different each call)

// Reuse the same ID:
$id: string.unique-id();
.#{$id} { ... }
@keyframes #{$id} { ... }

// Global alias (legacy): unique-id()
// Prefer string.unique-id after @use "sass:string"

Pattern: call once → store if needed → interpolate into selectors, keyframes, or custom properties.

Call vs Reuse

When to call again vs keep one value:

PatternResultUse when
string.unique-id() twiceTwo different IDsIndependent names that must not collide
$id: string.unique-id()Same ID everywhere you use $idKeyframe name + animation-name, or class + rule body

SCSS Examples

Examples use @use "sass:string". Open View Compiled CSS for verified Dart Sass output from one compile—IDs will differ on your machine.

Example 1 — Official Unique-ID Idea

Two calls produce two unquoted string identifiers.

SCSS
@use "sass:meta";
@use "sass:string";

.probe {
  --a: #{meta.inspect(string.unique-id())};
  --b: #{meta.inspect(string.unique-id())};
  --type: #{meta.type-of(string.unique-id())};
}

How It Works

  1. No arguments — empty call only.
  2. Unquoted identifiers appear without CSS quotes.
  3. Different calls → different IDs.
  4. type-of reports string.

Example 2 — Unique Class Selector

Interpolate one ID into a class name.

SCSS
@use "sass:string";

$id: string.unique-id();

.#{$id} {
  color: red;
}

How It Works

  1. Store the ID in $id.
  2. Interpolate into the selector.
  3. Valid CSS identifier — safe as a class name.
  4. Collision-resistant within this compile.

Example 3 — Reuse One ID Twice

Assign once so two places share the exact same identifier.

SCSS
@use "sass:meta";
@use "sass:string";

$id: string.unique-id();

.same {
  --x: #{meta.inspect($id)};
  --y: #{meta.inspect($id)};
}

How It Works

  1. One call creates the value.
  2. Variable copies the same string.
  3. --x and --y match exactly.
  4. Required for keyframes + animation-name pairs.

Example 4 — Two Calls Are Different

Compare two fresh IDs in the same compilation.

SCSS
@use "sass:meta";
@use "sass:string";

$a: string.unique-id();
$b: string.unique-id();

.diff {
  --same: #{$a == $b};
  --a: #{meta.inspect($a)};
  --b: #{meta.inspect($b)};
}

How It Works

  1. Two calls mint two IDs.
  2. Equality is false.
  3. Still unique within this compile.
  4. Do not expect the same characters next build.

Example 5 — Unique Keyframe Name in a Mixin

Avoid animation-name collisions when a mixin is included many times.

SCSS
@use "sass:string";

@mixin once-keyframe($from, $to) {
  $name: string.unique-id();

  @keyframes #{$name} {
    from { opacity: $from; }
    to { opacity: $to; }
  }

  animation-name: $name;
}

.fade {
  @include once-keyframe(0, 1);
}

How It Works

  1. Mixin generates a fresh name per include.
  2. $name ties @keyframes to animation-name.
  3. No hard-coded global animation label.
  4. Safe when the mixin ships in a library.

Edge Cases & Pitfalls

Most unique-id bugs are calling twice when you meant to reuse once.

reuse

Store the ID to reuse it

Calling unique-id() again creates a new name—use a variable for keyframe + property pairs.

rebuild

IDs change across compiles

Do not hard-code sample IDs into tests that expect byte-identical CSS every build.

unquoted

Result is unquoted

Ideal for selectors and animation names. Use string.quote only if you truly need a quoted string.

scope

Unique within one compilation

Guaranteed uniqueness is for the current Sass run—not a global UUID across machines or deploys.

Sass Support

Prefer the module form with current Dart Sass.

APIStatus
string.unique-id()Dart Sass 1.23+ via @use "sass:string"
Return typeUnquoted string (valid CSS identifier)
ArgumentsNone
Legacy globalunique-id()

Compile with the Dart Sass CLI.

Key Takeaways

  • Unquoted CSS identifier unique for this compile.
  • Each call is new — store in a variable to reuse.
  • Great for mixins — keyframes and collision-free class names.
  • Prefer @use "sass:string" (Dart Sass 1.23+).

One line: to mint a unique CSS identifier, use string.unique-id().

Compile Notes

Practical checklist:

checklist.txt
1. @use "sass:string"
2. Call string.unique-id() with no arguments
3. Store in a variable when the same ID must appear twice
4. Interpolate into selectors / @keyframes / animation-name
5. Expect different characters on the next rebuild

Frequently Asked Questions

string.unique-id() returns a randomly generated unquoted string that is a valid CSS identifier and unique within the current Sass compilation.
No. Call string.unique-id() with empty parentheses. There are no parameters.
No. Each call generates a new identifier. Store the result in a variable if you need to reuse the same ID in more than one place.
Not guaranteed. Official docs promise uniqueness within the current compilation. Recompiling often produces different IDs. Treat sample CSS on this page as examples, not fixed values.
Yes. unique-id() is the legacy global name. Prefer string.unique-id after @use "sass:string".
Dart Sass 1.23+. LibSass and Ruby Sass do not load sass:string with @use; they may still offer the legacy global name.

Next: Sass string.unquote()

Remove quotes from a Sass string (use carefully).

string.unquote() →

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.

8 people found this page helpful