On this page
Binary code

Alternative text for CSS generated content

Discover how to add accessible alternative text to CSS generated content for better accessibility.

The majority of content on the web is conveyed using HTML. However, CSS may also be used to insert content into a web page.

But how do we actually make the CSS content property accessible for screen readers?

Creating alternative description for CSS content property

You can provide alternative description with accessible content fallback through by appending a forward slash and then a string of text or a counter. Example: content: "foo" / "alt-text";

Alternative text may be specified for an image or any <content-list> items, by appending a forward slash and then a string of text or a counter. The alternative text is intended for speech output by screen-readers, but may also be displayed in some browsers. Note that if the browser does not support alternative text, the content declaration will be considered invalid and will be ignored. The / <string> or / <counter> data types specify the “alt text” for the element.
The content syntax is also more expressive as it allows for cascading, and allows you to chain multiple strings and attr() as alternative text.

Note that content-list is a list of one or more anonymous inline boxes appearing in the order specified. Each <content-list> item is either contents or of type <string>, <image>, <counter>, <quote>, <target>, or <leader()>.

<content-list> items
/* <content-list>: a list of content values. 
Several values can be used simultaneously */
content: "prefix" url(http://www.example.com/example.png);
content: "prefix" url("/images/example.png") "suffix" / "Alt text";
content: open-quote counter(chapter_counter);
If a browser doesn’t support alternative text, the content declaration will be considered invalid and ignored.

Implementation and usage

For example, we may prefix specific links with the small ⓘ icon to alert users that they will find additional information. That sign might be interpreted by a screenreader as Circled Latin Small Letter I or Information source combining enclosing circle, neither of which effectively communicates the intended function.

A better option would be to just hear More detailed information:.

Example of CSS content and alternative description
.info::before {
  content: "\24D8" / "More detailed information:";
}

Additionally, you can define the alternative text using an element attribute or a custom CSS property:

content with an alternative text using an element attribute or a custom property
.info::before {
 /* Attribute content and element content is read out */
 content: "\24D8" / attr(data-content-alt);
  
 /* Custom property and element content is read out */
 content: "\24D8" / var(--content-alt);
}

In above example, the alternative text is retrieved from an attribute (data-content-alt) or a custom property (--content-alt).

Workable example

Related posts

Comments

Leave a Reply

Real-user monitoring for Accessibility, Performance, Security, SEO & Errors (SiteLint)