π CSS string-set β Complete Tutorial
π What Is string-set?
The string-set property stores text from an element into anamed CSS string. This stored text can later be inserted elsewhere using thestring() function β most commonly in **print styles**, **running headers**, **footers**, and **paged media**.
>>βstring-set captures text from your HTML and reuses it anywhere in CSS β perfect for print layouts.β
Note
β Works with paged media (books, PDFs, docs)
β Works with @page margin boxes
β Useful for repeating chapter titles, captions, labels
β Works with @page margin boxes
β Useful for repeating chapter titles, captions, labels
π¦ Syntax
Basic Syntax
selector {
string-set: name value;
}| Part | Meaning |
|---|---|
| name | Name of the string variable |
| value | Text to store (from content(), attr(), or plain text) |
π§ How It Works
- string-set stores text inside a named string.
- string() retrieves that string somewhere else.
- Often used in @page headers/footers.
π 1. Capture Heading Text
Capture H1 text
h1 {
string-set: chapterTitle content();
}This stores the actual text of each <h1> element into a named string called chapterTitle.
π 2. Use the Stored String in a Printed Header
Display stored string in page header
@page {
@top-center {
content: string(chapterTitle);
}
}Every printed page now shows the current chapter title at the top.
π 3. Capture Attribute Values
Using attr()
img {
string-set: captionText attr(alt);
}Using string()
figure::after {
content: string(captionText);
}This auto-generates captions from alt text.
π 4. Use Multiple Strings
Multiple stored strings
h1 {
string-set: title content(), subtitle attr(data-subtitle);
}π 5. Capture Generated Content
Capture pseudo-element content
h2::before {
content: "Section: ";
string-set: sectionLabel content();
}Stores the content produced by ::before.
π 6. Running Footers (e.g., βCurrent Sectionβ)
Footer using string()
@page {
@bottom-left {
content: string(sectionLabel);
}
}π 7. Different Strings for Even/Odd Pages
Odd/even headers
@page :left {
@top-left { content: string(chapterTitle); }
}
@page :right {
@top-right { content: string(chapterTitle); }
}π§ͺ Real-World Examples
1οΈβ£ Book Layout: Chapter Title on Each Page
Book-style running header
h1 {
string-set: chapterTitle content();
}
@page {
@top-center {
content: string(chapterTitle);
}
}2οΈβ£ Automatic Figure Captions
Figure caption from alt
figure img {
string-set: captionText attr(alt);
}
figure::after {
content: "Figure: " string(captionText);
}3οΈβ£ Dynamic Document Sections
Section headers
h2 {
string-set: sectionName content();
}
@page {
@top-right {
content: string(sectionName);
}
}β οΈ Limitations
- β Browser support is limited β best in print engines (Paged.js, PrinceXML, Vivliostyle)
- β Only meaningful in paged media or advanced print styling
- β Cannot use strings like variables in normal layout (only in content)
Note
In normal screens, you may not see the effect unless using a paged CSS engine.
π₯ Summary
string-set is a powerful tool for creating professional print layouts, storing text, and generating running headers, captions, and document metadata entirely through CSS.
>>βUse string-set to automate text flow in printed and paged documents.β
Learn more βW3C Spec π