CSS Reference ยท Lists, Tables and Generated Content
The content property
Reference for the CSS content property: insert text, quotes, attribute values, counters and images with ::before and ::after, plus accessibility notes.
The content property inserts generated content into an element. It is most often used with the ::before and ::after pseudo-elements, where it is required: without a content value other than normal or none, the pseudo-element is not created at all. It also works in ::marker and, on regular elements, can replace their content with an image.
You can combine several pieces in one value: strings, attribute values, counters, quotation marks and images.
a[href^="http"]::after { content: " (" attr(href) ")"; } q::before { content: open-quote; } .icon-box::before { content: ""; display: block; width: 1em; height: 1em; }
Try it
Syntax
content: normal | none | [ <string> | <image> | <counter> | attr(<name>) | open-quote | close-quote | no-open-quote | no-close-quote ]+ [ / <alt-text> ]?;
| Initial value | normal |
| Applies to | ::before, ::after, ::marker, and elements (images only on the latter) |
| Inherited | No |
| Animatable | No (discrete) |
| Computed value | none for ::before and ::after when normal; otherwise as specified, with attr() and counters resolved at use time |
Values
| Value | Description |
|---|---|
normal |
On elements it has no effect. On ::before and ::after it computes to none, so no box is generated. |
none |
No generated content; the pseudo-element is not created. |
<string> |
Literal text, such as "โ ". An empty string "" still creates the pseudo-element, which is how decorative boxes are made. Use \A for a line break and escapes like "\2192" for Unicode characters. |
url(...) and other images |
Shows an image. On an ordinary element, an image replaces its content. |
attr(name) |
The value of the element's attribute, as a string. |
counter(name), counters(name, sep) |
A counter value; see counter() and counters(). |
open-quote, close-quote |
Quotation marks taken from the quotes property, and nested by level. |
no-open-quote, no-close-quote |
Moves the quote nesting level without printing a mark. |
/ <alt-text> |
An alternative text for assistive technology, such as content: "โ
" / "rated". Use / "" to mark the content decorative. Newer; check a compatibility table. |
Usage notes
- Always set
contenton::beforeand::after, even to"", or nothing shows. - Not selectable, not in the DOM. Generated text cannot be selected or found with Ctrl+F in most browsers, and scripts cannot read it from the DOM.
- Accessibility. Many screen readers read generated text. Do not put essential information in
content; use it for decoration and for labels that do not matter if missed. Use the/alt syntax to silence purely decorative symbols. - Display type. Pseudo-elements are inline by default. Set
display: blockorinline-blockto give them width and height. - Search engines generally ignore generated content, so keep real words in HTML.
- Escapes. In CSS strings, use a hexadecimal escape such as
\2605(a star) followed by a space or by a quote end;\"for a quote character. attr()returns text only. Using it for colors, lengths and numbers is a newer feature with limited support.
Browser support
Supported in all browsers. The / <alt-text> syntax and typed attr() are newer and not available everywhere, so check a compatibility table before relying on them.
Related
::beforeand::after::markerquotesattr()andcounter()/counters()counter-reset- Lessons: Pseudo-elements, CSS counters