Generated Content: the content Property, Quotes and attr()
What the content property can insert: strings, escapes and line breaks, images, attribute values with attr() and typed attr() in any property, counters, language-aware quotation marks, replacing element content, and alternative text.
kvi5hnuCode Sprout team member7 min readOn this page
Generated content is content that comes from CSS instead of the HTML: list markers are one example, and ::before and ::after (article 13) let you add your own. This article covers what content can insert, and the related quotes property.
Where generated boxes go

::before and ::after create boxes inside the element, as its first and last children. So:
- They sit inside the element's border and padding, and its background shows behind them.
- They inherit inherited properties, like
colorandfont, from the element, but not borders or backgrounds. Tested in Chrome: in a green paragraph with a 2px border, the::beforetext was green, and its own border was 0px. - They're inline by default, but accept any
display:blockto put a label on its own line,flexorgrid, orposition: absoluteto place them anywhere relative to a positioned element.
They don't work on replaced elements like <img> and <input>, which have no content box for children.
What content can contain
content takes a list of values, which are joined together:
.price::after { content: " incl. VAT"; } /* a string */
.new::before { content: url(badge.svg) " "; } /* an image, then a space */
h2::before { content: counter(section) ". "; } /* a counter (article 96) */
a::after { content: " (" attr(href) ")"; } /* an attribute value */
blockquote::before { content: open-quote; } /* a quotation mark */
| Value | Inserts |
|---|---|
"text" | the text exactly as written: markup like <em> is shown literally, not parsed |
url(...), image-set(), a gradient | an image, at its natural size (a gradient has none, so it gets a default size); it can't be resized from content |
counter(), counters() | a counter's value (article 96) |
attr(name) | the attribute's value as text |
open-quote, close-quote | quotation marks from quotes |
no-open-quote, no-close-quote | change the quote nesting level without drawing a mark |
normal or none | no box is generated |
Escapes and line breaks
Inside strings, a backslash followed by hexadecimal is a Unicode character: "\2192" is →, "\00A0" a non-breaking space. A line break is "\A", but white space in generated content collapses like any other text. Tested in Chrome: a ::before with content: "one\A two" was one line, 18px tall; adding white-space: pre made it two lines, 36px.
.address::after { content: "Floor 3\A Building B"; white-space: pre; }
A backslash at the end of a line continues a long string onto the next line in the stylesheet.
Alternative text
Screen readers read generated text. Add alternative text after a slash, or an empty one to hide decoration:
.external::after { content: "↗" / "opens in a new tab"; }
.star::before { content: "★" / ""; }
It works in all browsers. Content that matters should still be in the HTML: generated text can't be selected or copied in most browsers, and it's lost if CSS fails.
attr()
attr(name) inserts an attribute's value. A missing attribute gives an empty string. Tested in Chrome: content: "[" attr(data-x) "]" on an element without data-x showed [].
@media print {
a[href]::after { content: " (" attr(href) ")"; } /* show link targets on paper */
}
abbr[title]::after { content: " (" attr(title) ")"; }
[data-tip]:hover::after { content: attr(data-tip); position: absolute; /* … */ }
A fallback comes after a comma: attr(data-label, "Untitled"). Tested in Chrome: an element without the attribute showed the fallback text. It works in all browsers since Firefox 119 and Safari 18.4.
Typed attr() in any property
Traditionally attr() worked only in content and always gave a string. Now it can be used in any property, and parsed as a type:
<div class="bar" data-value="72" data-color="#2f7d32"></div>
.bar {
inline-size: attr(data-value %, 0%); /* 72%, with a unit */
background: attr(data-color type(<color>), gray); /* parsed as a color */
}
The second argument is either a unit (px, %, deg, s and others), which makes the value a number with that unit, or type() with a type such as <color>, <length>, <number>, <angle> or <custom-ident>. If parsing fails, the fallback is used. Tested in Chrome: color: attr(data-color type(<color>), blue) with data-color="green" computed to green, and width: attr(data-w px, 10px) with data-w="77" gave 77px. Without a type, attr() gives a string, which most properties won't accept.
Typed attr() works in Chrome 133, Firefox 155 and Safari 27.3. It's handy for values that belong to the content, like progress percentages, colors from a CMS, or grid positions. As with any data in CSS, don't put untrusted URLs in attributes: attr() values can't be used in url(), for security reasons. Tested in Chrome: both url(attr(data-u)) and attr(data-u type(<url>)) left background-image as none, while width: attr(data-n %, 0%) with data-n="40" gave 80px in a 200px box.

Everything in color here comes from CSS. Note the first panel: the outlined ::before and ::after boxes sit inside the paragraph's border and padding, as its first and last children.
Quotation marks: quotes
<q> elements get quotation marks from ::before { content: open-quote } and ::after { content: close-quote } in the browser's styles. Which marks they are depends on quotes:
q { quotes: auto; } /* the default: from the language */
blockquote { quotes: "“" "”" "‘" "’"; } /* pairs for each nesting level */
.no-marks { quotes: none; }
quotes: auto picks the marks from the language of the text around the quotation, since quotation marks belong to the surrounding sentence. Tested in Chrome, with <q> inside elements with each lang: English gave “outer ‘inner’ text”, French «bonjour», German „hallo“ and Japanese 「こんにちは」. But <q lang="fr"> inside English text got English marks, “bonjour”. So setting lang correctly on the page and on blocks of other-language text is usually all you need.
With explicit pairs, each open-quote increases the nesting level, and each close-quote decreases it; the level chooses the pair, and deeper levels reuse the last pair. The level isn't tied to the document tree, which allows the traditional style for quotations spanning several paragraphs, where only the last paragraph gets a closing mark:
.excerpt > p::before { content: open-quote; }
.excerpt > p::after { content: no-close-quote; } /* close the level without a mark */
.excerpt > p:last-child::after { content: close-quote; }
Replacing an element's content
content also works on elements, not only pseudo-elements, with a single image: it replaces the element's content with the image. Tested in Chrome: a paragraph with content: url(...) showed only the 40 × 20px image, without its text. It's rarely useful, except for swapping a logo image in a print or theme stylesheet, and the original text is lost for screen readers unless you add alternative text.
body { font: 0.85rem/1.5 system-ui, sans-serif; margin: 1rem; }
p { margin: 0 0 0.6rem; }
.meter { position: relative; background: #eee; border-radius: 4px; padding: 0.2rem 0.4rem; overflow: hidden; }
.meter::before { /* typed attr(): a width and a color from data attributes */
content: ""; position: absolute; inset: 0 auto 0 0;
inline-size: attr(data-value %, 0%);
background: attr(data-color type(<color>), gray);
opacity: 0.35;
}
.meter::after { content: " · " attr(data-label) ": " attr(data-value) "%"; }
a[data-tip] { position: relative; }
a[data-tip]:hover::after, a[data-tip]:focus::after {
content: attr(data-tip); position: absolute; inset-block-end: 130%; inset-inline-start: 0;
white-space: nowrap; background: #2a2118; color: white; padding: 0.2rem 0.4rem; border-radius: 4px; font-size: 0.75rem;
}
abbr[title]::after { content: " (" attr(title) ")"; color: #666; }
.address::after { content: "\A Building B\A 12 Harbour Road"; white-space: pre; color: #8a3b12; }
.print a[href]::after { content: " (" attr(href) ")"; color: #666; }
The bars get their width and color from data attributes, the link shows a tooltip from data-tip on hover or focus, the quotes follow each element's language, and the address gets line breaks from \A.
What's current
- Typed
attr()in any property: Chrome 133, Firefox 155, Safari 27.3. attr()fallback: all browsers.- Alternative text for
content: all browsers. quotes: auto: all browsers.
Common questions
Why doesn't my \A create a new line?
Generated white space collapses. Add white-space: pre or pre-line to the pseudo-element.
Can I put HTML in content?
No. Strings are shown literally. Use real elements for markup.
Why does width: attr(data-w) not work?
Without a type or unit, attr() gives a string. Write attr(data-w px), in browsers that support typed attr().
How do I get French quotation marks?
Set lang="fr" on the page or on the element that contains the quotation: quotes: auto does the rest. A lang on the <q> itself only describes the quoted words, and the marks follow the surrounding text.
Summary
::beforeand::afterare inline children inside the element, inheriting its inherited properties.contentjoins strings, images, counters, attribute values and quote marks;normalornonegenerates no box.- Use
\Awithwhite-space: prefor line breaks, and/ "alt"for alternative text. attr()reads attributes, with a fallback; typedattr()with a unit ortype()works in any property.quotes: autofollowslang; explicit pairs set marks for each nesting level;no-close-quotecloses a level silently.
Practice questions
Write a print rule that shows each link's URL in parentheses after the link text.
Answer
@media print {
a[href]::after { content: " (" attr(href) ")"; }
}
What does this show for <p class="tag">News</p>, which has no data-count?
.tag::after { content: " (" attr(data-count, "0") ")"; }
Answer
"News (0)": the attribute is missing, so the fallback "0" is used.
Write CSS that sets a progress bar's width from data-progress="40".
Answer
.progress { inline-size: attr(data-progress %, 0%); }
The % unit makes 40 a percentage; browsers without typed attr() ignore the declaration, so set a fallback width before it, or use an inline custom property such as style="--p: 40%" instead.
With this rule, why does an inner <q> get single marks while the outer one gets double marks, and how would you make every level use double marks?
q { quotes: "“" "”" "‘" "’"; }
Answer
Each open-quote increases the nesting level, which selects the next pair; levels deeper than the list reuse the last pair, so the third level is single too. Listing only one pair, quotes: "“" "”", makes every level reuse it.
Previous: Styling Lists: list-style, Marker Position and ::marker
Next: CSS Counters: counter-reset, counter-increment and Nested Numbering

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.