string.length() belongs to the built-in sass:string module. It returns how many characters a string has. This page covers empty strings, quoted vs unquoted values, length guards, truncate helpers, and five compiled examples.
01
Concept
Count characters
02
Module
@use "sass:string"
03
Returns
Number
04
Empty
0
05
Quotes
Not counted
06
Practice
5 examples
Concept
What Is string.length()?
Official docs: returns the number of characters in $string. Spaces count. An empty string has length 0.
Works on quoted and unquoted Sass strings.
The surrounding quotes are not characters in the value.
Use it before string.slice when you only want to keep the first n characters.
Pair with @if for max-length guards on labels and class names.
💡
Beginner tip
Think of counting letters on a name tag. "Helvetica Neue" has 14 characters (including the space). bold has 4.
Foundation
📝 Syntax
styles.scss
@use "sass:string";
// Recommended
string.length($string)
// Legacy global name
str-length($string)
Parameters
Parameter
Type
Required
Description
$string
String
Yes
The string whose characters you want to count.
Return value
Type
Result
Number
Character count (unitless). Empty string → 0
Modules
📦 Loading sass:string
styles.scss
// Recommended — namespaced (Dart Sass 1.23+)
@use "sass:string";
$count: string.length("Helvetica Neue"); // 14
// Optional — bring members into scope
@use "sass:string" as *;
$count: length("Helvetica Neue");
// Legacy global
$count: str-length("Helvetica Neue");
⚠️
Name clash tip
With @use "sass:string" as *;, bare length() can collide with list.length if you also import list members. Prefer the namespaced form string.length().
Details
🎨 Official Patterns
Call
Result
length("Helvetica Neue")
14
length(bold)
4
length("")
0
These are the official Sass documentation samples. Notice both quoted and unquoted inputs are valid strings.
Compare
⚖️ Quotes, Spaces & Empty Strings
Value
Length
Why
"Roboto"
6
Quotes are not counted
Roboto (unquoted)
6
Same characters
"a b"
3
Space counts as one character
""
0
Empty string
Support
🛠 Compatibility
This is about Sass compilers, not browsers. Length is computed at compile time.
Implementation
length
Dart Sass
Yes — prefer string.length (module since 1.23.0)
LibSass
Legacy str-length may exist; no @use "sass:string"
Ruby Sass
Legacy str-length 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";
Count characters
string.length("Helvetica Neue")
Empty check
string.length($s) == 0
Max-length guard
@if string.length($s) > 10 { ... }
Size with ch
string.length($label) * 1ch
Truncate
string.slice($s, 1, $max) after a length check
Hands-On
Examples Gallery
Each example uses string.length at compile time (Dart Sass 1.23+). Open View Compiled CSS for verified output.
📚 Getting Started
Official docs samples, then quoted vs unquoted lengths.
14 > 10, so the @if branch wins. All of this is decided at compile time—browsers never see string.length.
Example 4 — Size With ch Units
Use character count to set a minimum inline size.
styles.scss
@use "sass:string";
$label: "Save";
.btn::after {
content: $label;
// Use length as a ch-based hint for min inline size
min-inline-size: string.length($label) * 1ch;
}
string.length() returns how many characters a Sass string contains. Empty strings are 0, spaces count, and quote marks do not. Use it for guards, ch-based sizing, and truncate helpers with string.slice.