CSS content Property
The content property controls generated content, commonly inserting text, images, counters, or quotation marks through the ::before and ::after pseudo-elements.
It can also replace an element's rendered contents with a specified image and supply alternative text for generated content. Common uses include decorative symbols, labels, numbered headings, quotation marks, and visual indicators.
Generated content is primarily presentational. Important information and interactive controls should remain in the HTML document rather than relying entirely on CSS.
content Syntax
The content property accepts keywords, strings, images, counters, quotation keywords, and other supported generated-content values.
selector::before {
content: "Example";
}
Common Syntax
selector::before {
content: "Text";
}
selector::after {
content: "";
}
selector::before {
content: url("icon.svg");
}
selector::before {
content: counter(section);
}
selector::before {
content: open-quote;
}
Multiple Content Values
selector::before {
content: "Section " counter(section) ": ";
}
Alternative Text
selector::before {
content: url("status.svg") / "Status";
}
Keyword Values
selector::before {
content: normal;
}
selector::before {
content: none;
}
CSS-Wide Keywords
selector::before {
content: inherit;
}
selector::before {
content: initial;
}
selector::before {
content: unset;
}
selector::before {
content: revert;
}
selector::before {
content: revert-layer;
}
Text values must be enclosed in quotation marks. Multiple generated-content items are separated by spaces rather than commas.
Property Values
| Value | Description |
|---|---|
normal |
Uses the normal content behavior. For ::before and ::after, it behaves like none. |
none |
Prevents generated content from being created. |
"text" |
Inserts the specified text string. |
url() |
Inserts an image or other supported external resource. |
linear-gradient() |
Supplies a generated image. |
attr() |
Retrieves a value from an attribute on the originating element. |
counter() |
Inserts the current value of a CSS counter. |
counters() |
Inserts nested counter values separated by a specified string. |
open-quote |
Inserts an opening quotation mark according to the quotes property. |
close-quote |
Inserts a closing quotation mark according to the quotes property. |
no-open-quote |
Increases the quotation nesting level without displaying an opening quotation mark. |
no-close-quote |
Decreases the quotation nesting level without displaying a closing quotation mark. |
/ "text" |
Supplies an alternative text representation for generated content. |
leader() |
Defines a repeated leader pattern in the specification, with limited browser support. |
target-counter() |
References a counter at a target element, primarily for paged-media applications. |
target-counters() |
References nested counters at a target element. |
target-text() |
References text from a target element, with limited browser support. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | normal |
| Inherited | No |
| Applies to | All elements and supported pseudo-elements |
| Computed value | Computed content value, with relative URLs made absolute |
| Animation type | Discrete |
| Percentages | Not applicable |
Some values are defined by newer CSS specifications and are not supported consistently across browsers. Strings, images, counters, attributes, and quotation keywords are the most commonly used forms.
How It Works
The content property is commonly used to create generated content before or after an element's existing content.
Step 1: Create an HTML Element
<p class="notice">Important information</p>
Step 2: Add a Pseudo-Element
.notice::before {
content: "Note: ";
}
Step 3: Style the Generated Content
.notice::before {
content: "Note: ";
color: #b45309;
font-weight: bold;
}
Step 4: View the Result
The browser displays the generated label before the paragraph's existing text.
Generated Content and the DOM
The generated text appears in the rendered page but does not become a regular HTML text node in the document's DOM.
Important Distinction
The content property determines what is generated. Other CSS properties control its appearance, spacing, positioning, and layout.
Using ::before
The ::before pseudo-element creates generated content before the originating element's content.
Add a Label
.tip::before {
content: "Tip: ";
font-weight: bold;
color: #2563eb;
}
Add a Decorative Symbol
.feature::before {
content: "✓ ";
color: #15803d;
}
Add an Empty Decorative Element
.heading::before {
content: "";
display: inline-block;
width: 6px;
height: 20px;
margin-right: 10px;
background-color: #336699;
vertical-align: middle;
}
Insert an Image
.download::before {
content: url("download-icon.svg");
margin-right: 8px;
}
Important Consideration
For ordinary elements, ::before generally requires a content value other than none or normal to generate content.
Using ::after
The ::after pseudo-element creates generated content after the originating element's content.
Add a Decorative Arrow
.read-more::after {
content: " →";
color: #336699;
}
Add a Visual Label
.new-item::after {
content: " NEW";
color: #15803d;
font-size: 0.75rem;
font-weight: bold;
}
Add a Decorative Line
.section-heading::after {
content: "";
display: block;
width: 60px;
height: 3px;
margin-top: 8px;
background-color: #336699;
}
Add External Link Indicators
a.external::after {
content: " ↗";
font-size: 0.875em;
}
Important Consideration
Generated indicators should supplement meaningful link text rather than serve as the only explanation of a link's purpose.
String Values
String values insert literal text into generated content.
Double-Quoted String
.message::before {
content: "Notice: ";
}
Single-Quoted String
.message::before {
content: 'Notice: ';
}
Empty String
.decorative-box::before {
content: "";
display: block;
width: 30px;
height: 30px;
background-color: #336699;
}
Multiple Strings
.message::before {
content: "CSS " "Reference: ";
}
The strings are concatenated into the generated content.
Insert a Line Break
.message::before {
content: "First line\A Second line";
white-space: pre-line;
}
The \A escape represents a line feed. The white-space property controls whether that line break is displayed.
Insert a Quotation Mark
.message::before {
content: "\"Important\" ";
}
Practical Consideration
Strings are appropriate for short presentational labels and symbols. Use HTML for content that must remain available independently of CSS.
Generated Images
The content property can insert images into generated content.
External Image
.download-link::before {
content: url("download-icon.svg");
}
Generated Gradient Image
.decorative-heading::before {
content: linear-gradient(90deg, #336699, #60a5fa);
display: inline-block;
width: 20px;
height: 20px;
margin-right: 10px;
}
Decorative Image with Empty Alternative Text
.download-link::before {
content: url("download-icon.svg") / "";
}
Image with Alternative Text
.status::before {
content: url("status-icon.svg") / "Status";
}
Image Sizing
An image inserted with content: url() is rendered as a generated replaced object. Its sizing behavior differs from a pseudo-element styled with background-image.
Using a Background Instead
.download-link::before {
content: "";
display: inline-block;
width: 16px;
height: 16px;
margin-right: 8px;
background: url("download-icon.svg") center / contain no-repeat;
}
Using an empty pseudo-element with a background image provides direct control over the icon's width and height.
Attribute Values with attr()
The attr() function retrieves an attribute value from the originating HTML element for use in generated content.
HTML Example
<a href="https://example.com" data-label="External Link">Visit Website</a>
Display the Attribute
a::after {
content: " (" attr(data-label) ")";
}
Display a URL
a.show-url::after {
content: " (" attr(href) ")";
}
Display a Custom Label
<span class="status" data-status="Complete">Project</span>
.status::after {
content: " - " attr(data-status);
}
Combine Attribute Values and Strings
.product::after {
content: "Code: " attr(data-code);
}
Important Consideration
The traditional string form of attr() is widely supported in generated content. More advanced typed attribute values have different compatibility considerations.
Attribute-generated text should not be the only source of essential information, since its availability to assistive technologies can vary.
CSS Counters
The content property can display automatically generated numbering using CSS counters.
Create a Counter
.steps {
counter-reset: step;
}
Increment the Counter
.steps li {
counter-increment: step;
}
Display the Counter
.steps li::before {
content: counter(step) ". ";
font-weight: bold;
}
Complete Example
.steps {
counter-reset: step;
list-style: none;
}
.steps li {
counter-increment: step;
}
.steps li::before {
content: "Step " counter(step) ": ";
font-weight: bold;
color: #336699;
}
Counter Styles
.steps li::before {
content: counter(step, upper-roman) ". ";
}
Nested Counters
.outline {
counter-reset: section;
}
.outline li {
counter-increment: section;
}
.outline li::before {
content: counters(section, ".") " ";
}
The counters() function combines counter values across nested counter scopes.
Practical Consideration
Use semantic ordered lists when numbering represents meaningful document structure. CSS counters are useful for visual numbering and custom presentation.
Quotation Marks
The content property supports special keywords for generating quotation marks.
Opening Quotation Mark
blockquote::before {
content: open-quote;
}
Closing Quotation Mark
blockquote::after {
content: close-quote;
}
Complete Example
blockquote {
quotes: "“" "”" "‘" "’";
}
blockquote::before {
content: open-quote;
}
blockquote::after {
content: close-quote;
}
Suppress Visible Opening Quote
.nested-quote::before {
content: no-open-quote;
}
Suppress Visible Closing Quote
.nested-quote::after {
content: no-close-quote;
}
Quotation Nesting
The open-quote and close-quote keywords adjust quotation nesting. The no-open-quote and no-close-quote keywords adjust nesting without displaying quotation characters.
Practical Consideration
Use the quotes property to define quotation characters appropriate for the document's language and typography.
normal and none
The normal and none values control whether generated content appears.
normal Value
.message::before {
content: normal;
}
For ::before and ::after, normal behaves like none.
none Value
.message::before {
content: none;
}
The none value prevents the pseudo-element from generating content.
Remove Generated Content
.message::before {
content: "Note: ";
}
.message.plain::before {
content: none;
}
Empty String vs. none
.decorative::before {
content: "";
display: inline-block;
width: 10px;
height: 10px;
background-color: #336699;
}
An empty string can generate a pseudo-element that can be styled. The none value suppresses its generation.
Important Distinction
On ordinary elements, the default normal value preserves normal element content. It does not remove the element's existing HTML content.
Alternative Text
The content property supports alternative text after a forward slash in generated-content declarations.
Basic Syntax
.status::before {
content: "✓" / "Complete";
}
Image Alternative Text
.status::before {
content: url("complete.svg") / "Complete";
}
Decorative Content
.heading::before {
content: "◆" / "";
}
Combined Visual Content
.notice::before {
content: "⚠ " / "Warning";
}
Accessibility Consideration
Alternative text can help describe generated content, but support and exposure through accessibility APIs can vary.
For essential status messages, provide meaningful text in the HTML itself rather than depending exclusively on CSS-generated alternative text.
Replacing Element Content
The content property can also be applied directly to an element to replace its rendered contents with a supported image value.
HTML Example
<div class="replacement-image">Original content</div>
Replace the Rendered Content
.replacement-image {
content: url("replacement.svg");
}
Replacement with Alternative Text
.replacement-image {
content: url("replacement.svg") / "Replacement graphic";
}
Normal Element Content
.replacement-image {
content: normal;
}
The element returns to its normal content rendering behavior.
Important Distinction
Replacing an element's rendered contents does not rewrite its HTML or remove its original DOM children.
Practical Consideration
For meaningful document images, an HTML <img> element with an appropriate alt attribute is generally preferable. Use CSS replacement for presentation-specific cases where its behavior is appropriate.
List Markers
The content property can customize content generated by the ::marker pseudo-element.
Custom Marker Text
li::marker {
content: "→ ";
}
Custom Marker Symbol
li::marker {
content: "◆ ";
color: #336699;
}
Counter-Based Marker
ol li::marker {
content: counter(list-item) ". ";
}
Restore Normal Markers
li::marker {
content: normal;
}
Important Consideration
The ::marker pseudo-element supports a restricted set of CSS properties. Its styling capabilities differ from those of ::before and ::after.
Use actual list elements to preserve list semantics and accessibility.
Combining Content Values
Multiple generated-content values can be combined into a single content declaration.
Text and Counter
.section-heading::before {
content: "Section " counter(section) ": ";
}
Text and Attribute
.product::after {
content: " (" attr(data-code) ")";
}
Image and Text
.download::before {
content: url("download.svg") " Download";
}
Counter and Quotation Marks
.quote-item::before {
content: counter(item) ". " open-quote;
}
Alternative Text for Combined Content
.status::before {
content: "✓ " attr(data-status) / attr(data-status);
}
Invalid Comma Separation
.message::before {
content: "Hello", "World";
}
Commas do not separate ordinary generated-content items. Use space-separated values instead.
Practical Consideration
Keep combined generated content concise. Long strings and important document information are better maintained in HTML.
Styling Generated Content
Generated content can be styled using many of the same CSS properties applied to ordinary elements.
Text Color and Weight
.notice::before {
content: "Note: ";
color: #b45309;
font-weight: bold;
}
Spacing
.feature::before {
content: "✓";
margin-right: 8px;
color: #15803d;
}
Background and Padding
.badge::after {
content: "NEW";
display: inline-block;
margin-left: 8px;
padding: 3px 6px;
color: #ffffff;
background-color: #336699;
font-size: 0.75rem;
}
Block-Level Generated Content
.section-heading::after {
content: "";
display: block;
width: 80px;
height: 3px;
margin-top: 10px;
background-color: #336699;
}
Positioned Decorative Content
.highlight-box {
position: relative;
}
.highlight-box::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 4px;
height: 100%;
background-color: #336699;
}
Important Consideration
When using generated content for decoration, avoid positioning it over readable text or interactive controls.
Practical Uses
The content property is useful for adding small presentational details without additional HTML elements.
Decorative Checkmarks
.feature-item::before {
content: "✓ ";
color: #15803d;
font-weight: bold;
}
External Link Symbols
a.external::after {
content: " ↗";
font-size: 0.875em;
}
Required Field Indicators
.required-label::after {
content: " *";
color: #b91c1c;
}
The HTML form control should also include the required attribute when the field is required. A generated asterisk alone does not establish required-field semantics.
Section Numbering
.reference-sections {
counter-reset: section;
}
.reference-sections h2 {
counter-increment: section;
}
.reference-sections h2::before {
content: counter(section) ". ";
}
Decorative Heading Lines
.heading::after {
content: "";
display: block;
width: 50px;
height: 3px;
margin-top: 8px;
background-color: #336699;
}
Print-Friendly URLs
@media print {
a.print-url::after {
content: " (" attr(href) ")";
}
}
When Not to Use
Avoid using generated content for essential instructions, complete paragraphs, interactive controls, or information that must remain available when CSS is disabled.
content Example
This example demonstrates three common uses of the content property: adding a label before text, displaying an attribute value, and inserting decorative symbols.
The generated content is added with ::before and ::after without changing the original HTML text.
<div class="content-demo">
<div class="demo-item">
<h3>Generated Label</h3>
<p class="demo-notice">Review the CSS example.</p>
</div>
<div class="demo-item">
<h3>Attribute Value</h3>
<p class="demo-status" data-status="Complete">Reference Page</p>
</div>
<div class="demo-item">
<h3>Decorative Symbols</h3>
<p class="demo-feature">Copyable CSS examples</p>
<p class="demo-feature">Beginner-friendly explanations</p>
</div>
</div>
.content-demo {
display: grid;
gap: 15px;
}
.content-demo .demo-item {
padding: 15px;
border: 1px solid #cccccc;
background-color: #f8f9fa;
}
.content-demo h3 {
margin: 0 0 10px;
font-size: 1.125rem;
}
.content-demo p {
margin: 0 0 8px;
}
.content-demo p:last-child {
margin-bottom: 0;
}
.content-demo .demo-notice::before {
content: "Note: ";
color: #b45309;
font-weight: bold;
}
.content-demo .demo-status::after {
content: " - " attr(data-status);
color: #15803d;
font-weight: bold;
}
.content-demo .demo-feature::before {
content: "✓ ";
color: #15803d;
font-weight: bold;
}
Generated Label
Review the CSS example.
Attribute Value
Reference Page
Decorative Symbols
Copyable CSS examples
Beginner-friendly explanations
The first example inserts a bold Note: label before the paragraph text.
The second example uses attr(data-status) to display the element's custom attribute value after the text.
The third example adds green checkmarks before two feature descriptions. The symbols are generated by CSS rather than included in the HTML.
Accessibility
CSS-generated content can affect what users see and what assistive technologies announce. Its accessibility behavior depends on the type of content and browser support.
Keep Essential Information in HTML
Do not rely entirely on content to provide required instructions, form labels, status messages, or other essential information.
Decorative Content
Decorative symbols should not create unnecessary announcements for screen reader users. Where supported, empty alternative text can identify generated content as decorative.
Meaningful Images
Use semantic HTML images with appropriate alternative text when an image communicates important information.
Form Labels
Use actual HTML <label> elements for form controls. Generated labels do not replace accessible form labeling.
Interactive Controls
Do not create buttons, links, or other interactive elements solely through CSS-generated content.
Generated Numbering
When numbering conveys meaningful order, use semantic HTML structures such as ordered lists.
Test Assistive Technology
Screen readers and browsers may expose generated content differently. Test important interfaces with keyboard navigation and relevant assistive technologies.
Important Considerations
Keep the following points in mind when using the content property:
- Purpose: Controls generated content and supported content replacement.
- Initial value:
normal. - Inheritance: The property is not inherited.
- Pseudo-elements: Commonly used with
::beforeand::after. - Strings: Text values must be quoted.
- Empty strings:
content: ""can generate an empty pseudo-element for styling. - none: Prevents generated content from appearing.
- Images: Can insert images using
url()and supported CSS image functions. - Attributes:
attr()retrieves attribute values from the originating element. - Counters:
counter()andcounters()support generated numbering. - Quotation marks: Quote keywords work with the
quotesproperty. - Multiple values: Generated-content items are separated by spaces.
- Alternative text: The slash syntax supplies an alternative text representation.
- Element replacement: Image values can replace an element's rendered contents.
- List markers: The property can customize supported
::markercontent. - DOM: Generated content does not create ordinary HTML text nodes.
- Styling: Other CSS properties control generated content appearance and layout.
- Browser support: Advanced generated-content functions may have limited implementation.
- Accessibility: Essential information should remain available in semantic HTML.
Use content for decorative elements, short labels, numbering, and presentational enhancements. Keep meaningful document content and interactive functionality in HTML.
Browser Support
The CSS content property is widely supported in modern browsers, including Chrome, Edge, Firefox, and Safari.
Basic generated text, images, counters, quotation marks, and use with ::before and ::after have broad support. More advanced features, including some target-related functions and generated-content alternative text behavior, may vary.
Basic Example
.notice::before {
content: "Note: ";
font-weight: bold;
}
Feature Detection
@supports (content: "Example") {
.notice::before {
content: "Note: ";
}
}
Feature queries can test whether a declaration is recognized, but they do not guarantee support for all generated-content functions or accessibility behavior.
For current compatibility information, visit Can I Use? .
Specifications
The content property is defined in the CSS Generated Content specifications, with related behavior defined in CSS Pseudo-Elements and CSS Lists and Counters.
CSS Generated Content Module Level 3
The specification defines generated-content values, content replacement, alternative text, quotation handling, and additional generated-content functions.
