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
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.
Reference
Call vs Reuse
When to call again vs keep one value:
Pattern
Result
Use when
string.unique-id() twice
Two different IDs
Independent names that must not collide
$id: string.unique-id()
Same ID everywhere you use $id
Keyframe name + animation-name, or class + rule body
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().
Practice
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.