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
Concept
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.
Foundation
📝 Syntax
styles.scss
@use "sass:string";
// Recommended
string.unique-id()
// Legacy global name
unique-id()
Parameters
Parameter
Type
Required
Description
None — call with empty parentheses.
Return value
Type
Result
String (unquoted)
A valid CSS identifier unique within this compilation
Modules
📦 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();
Details
🎨 Official Patterns
Call
Sample result
unique-id()
uabtrnzug (docs sample)
unique-id() again
u6w1b1def (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.
Compare
⚖️ Call Twice vs Store Once
Approach
What you get
When to use
Call unique-id() twice
Two different identifiers
Independent tokens
$id: unique-id(); then reuse $id
The same identifier everywhere
Matching @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.
Support
🛠 Compatibility
This is about Sass compilers, not browsers. The identifier is created at compile time and ships as plain CSS text.
Implementation
unique-id
Dart Sass
Yes — prefer string.unique-id (module since 1.23.0)
LibSass
Legacy unique-id may exist; no @use "sass:string"
Ruby Sass
Legacy unique-id may exist; no @use "sass:string"
Prefer current Dart Sass so the module API matches the official docs.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Import string
@use "sass:string";
Make one ID
string.unique-id()
Reuse the same ID
$id: string.unique-id();
Animation pair
animation-name: $id; + @keyframes #{$id}
ID selector
##{$id} { ... }
Unique class in a mixin
.#{string.unique-id()} { ... }
Hands-On
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.
Each loop iteration appends a fresh ID. All three custom properties are different within this compilation.
Applications
🚀 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.
Watch Out
⚠️ 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.
Pro Tips
💡 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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about string.unique-id()
Generate a unique CSS identifier at compile time.
5
Core concepts
📝01
API
unique-id()
Call
📦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.
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.