CSS Generated Content
The content property is a unique CSS feature that allows you to insert generated content into your web pages. It is used almost exclusively with the ::before and ::after pseudo-elements.
What Can You Insert?
- Strings: Text content like
content: "Hello"; - Images: Using
content: url(icon.png); - Attributes: Using the
attr()function to grab HTML attributes. - Counters: Automatic numbering for lists or sections.
- Empty:
content: "";is used for clearing floats or creating CSS shapes.
Accessibility Note
Content added via CSS is generally treated as decorative. Some screen readers may read it, but behavior varies. Do not use the content property for critical information that users must read to understand the page. Use actual HTML text for that.
The attr() Function
The attr() function is powerful for print stylesheets or tooltips. For example, you can append the URL of a link in parentheses after the link text when printing the page: a::after { content: ' (' attr(href) ')'; }.
