Sass string.unique-id() Function

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

What You’ll Learn

string.unique-id() belongs to the built-in sass:string module. It invents a random, unquoted CSS identifier that is unique for the current compilation. This page covers reusing IDs with variables, animation names, mixins, and five compiled examples.

01

Concept

Unique CSS id

02

Module

@use "sass:string"

03

Returns

Unquoted string

04

Args

None

05

Reuse

Store in a variable

06

Practice

5 examples

What Is string.unique-id()?

Official docs: returns a randomly generated unquoted string that is guaranteed to be a valid CSS identifier and to be unique within the current Sass compilation.

  • Takes no arguments—just call string.unique-id().
  • Each call creates a new identifier.
  • Save it in a variable when animation names, IDs, or classes must match.
  • Values can change when you recompile—sample CSS below is illustrative.
💡
Beginner tip

Think of a ticket printer at a bakery. Every press gives a new number that will not collide with another ticket in this batch. Press again for a different number; keep the paper if you need the same ticket twice.

📝 Syntax

styles.scss
@use "sass:string";

// Recommended
string.unique-id()

// Legacy global name
unique-id()

Parameters

ParameterTypeRequiredDescription
None — call with empty parentheses.

Return value

TypeResult
String (unquoted)A valid CSS identifier unique within this compilation

📦 Loading sass:string

styles.scss
// Recommended — namespaced (Dart Sass 1.23+)
@use "sass:string";
$id: string.unique-id(); // e.g. uiu6815

// Optional — bring members into scope
@use "sass:string" as *;
$id: unique-id();

// Legacy global
$id: unique-id();

🎨 Official Patterns

CallSample result
unique-id()uabtrnzug (docs sample)
unique-id() againu6w1b1def (docs sample—different)

Official Sass docs show two different identifiers from two calls. Your values will look similar (often starting with u) but will not match these samples exactly.

⚖️ Call Twice vs Store Once

ApproachWhat you getWhen to use
Call unique-id() twiceTwo different identifiersIndependent tokens
$id: unique-id(); then reuse $idThe same identifier everywhereMatching @keyframes, IDs, classes
⚠️
Sample CSS note

The “View Compiled CSS” panels below show one real compile. Re-running Sass will usually print different identifiers. Focus on the pattern, not the exact letters.

🛠 Compatibility

This is about Sass compilers, not browsers. The identifier is created at compile time and ships as plain CSS text.

Implementationunique-id
Dart SassYes — prefer string.unique-id (module since 1.23.0)
LibSassLegacy unique-id may exist; no @use "sass:string"
Ruby SassLegacy unique-id may exist; no @use "sass:string"

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

⚡ Quick Reference

GoalCode
Import string@use "sass:string";
Make one IDstring.unique-id()
Reuse the same ID$id: string.unique-id();
Animation pairanimation-name: $id; + @keyframes #{$id}
ID selector##{$id} { ... }
Unique class in a mixin.#{string.unique-id()} { ... }

Examples Gallery

Each example uses string.unique-id at compile time (Dart Sass 1.23+). Open View Compiled CSS for a sample output—your IDs will differ on rebuild.

📚 Getting Started

Two independent calls, then a matching animation name pair.

Example 1 — Two Calls, Two IDs

Each call invents a different identifier.

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

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

How It Works

--a and --b are different because Sass called unique-id twice. The values are unquoted identifiers, not quoted strings.

Example 2 — Matching Animation Name

Store one ID so animation-name and @keyframes stay in sync.

styles.scss
@use "sass:string";

$anim: string.unique-id();

.loader {
  animation-name: $anim;
}

@keyframes #{$anim} {
  from { opacity: 0; }
  to { opacity: 1; }
}

How It Works

One variable, two uses. If you called unique-id() again inside @keyframes, the names would not match.

📈 Practical Patterns

ID selectors, mixin-scoped classes, and a short list of IDs.

Example 3 — Custom Property + ID Selector

Reuse one ID as a CSS variable value and as a #id selector.

styles.scss
@use "sass:string";

$id: string.unique-id();

.panel {
  --scope: #{$id};
}

##{$id} {
  scroll-margin-top: 4rem;
}

How It Works

##{$id} becomes a normal CSS ID selector. The same token is stored in --scope for HTML or JS to reference.

Example 4 — Unique Class From A Mixin

Each include can emit a one-off class name.

styles.scss
@use "sass:string";

@mixin once-class {
  $name: string.unique-id();
  .#{$name} {
    @content;
  }
}

@include once-class {
  display: grid;
}

How It Works

Handy for generated utility wrappers. Capture $name if you also need to print the class into HTML from Sass tooling.

Example 5 — Build A List Of Unique IDs

Generate several identifiers in a loop and read them with list.nth.

styles.scss
@use "sass:list";
@use "sass:string";

$ids: ();
@for $i from 1 through 3 {
  $ids: list.append($ids, string.unique-id());
}

.box {
  --id-1: #{list.nth($ids, 1)};
  --id-2: #{list.nth($ids, 2)};
  --id-3: #{list.nth($ids, 3)};
}

How It Works

Each loop iteration appends a fresh ID. All three custom properties are different within this compilation.

🚀 Real-World Use Cases

  • Keyframe names — avoid collisions when many mixins emit animations.
  • Generated classes — one-off utility wrappers from mixins.
  • Fragment IDs — unique #id selectors for skip links or anchors.
  • Scoped tokens — put unique strings into custom properties for JS.
  • Test fixtures — invent throwaway identifiers while prototyping.

🧠 How Compilation Works

1

Call with no arguments

Write string.unique-id().

Source
2

Sass invents an identifier

Guaranteed unique for this compilation; valid as a CSS name.

Compile
3

Reuse via a variable

Assign once, then interpolate into selectors and properties.

Emit
4

CSS ships

Browsers only see finished names like u4pmoi2.

⚠️ Common Pitfalls

  • Calling twice by accident — animation and keyframes will not match.
  • Expecting stable IDs across builds — uniqueness is per compilation.
  • Thinking it is a browser API — the call never appears in CSS.
  • Expecting quoted output — results are unquoted identifiers.
  • Using it for secrets — these are CSS names, not cryptographic tokens.

💡 Best Practices

✅ Do

  • Use @use "sass:string" and string.unique-id()
  • Store the result when the same ID must appear twice
  • Use it for keyframes, generated classes, and scoped tokens
  • Treat sample CSS as patterns, not fixed values
  • Prefer Dart Sass 1.23+

❌ Don’t

  • Call it twice when names must match
  • Assume IDs stay identical after every rebuild
  • Use it as a password or security token
  • Expect browsers to evaluate the call
  • Rely on LibSass for the module API

Key Takeaways

Knowledge Unlocked

Five things to remember about string.unique-id()

Generate a unique CSS identifier at compile time.

5
Core concepts
📦 02

Module

sass:string

@use
🔗 03

Result

unquoted CSS name

Output
📚 04

Scope

per compilation

Rule
05

Reuse

store in a variable

Pattern

❓ 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.
Did you know?

Official Sass docs print two different results from two unique-id() calls (uabtrnzug and u6w1b1def)—a clear reminder that each call is a fresh identifier.

Conclusion

string.unique-id() creates a random, unquoted CSS identifier that is unique within the current Sass compilation. Call it once and store the result whenever the same name must appear in more than one place.

Continue with string.unquote() or the Sass introduction.

Next: unquote strings

Learn string.unquote() to remove quotes from Sass strings 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.

5 people found this page helpful