Skip to content

Commit f7f6f16

Browse files
Editorial review: Add information and example for counters in generated content alt text (#42645)
* Add information and example for counters in generated content alt text * Update files/en-us/web/css/reference/properties/content/index.md Co-authored-by: Estelle Weyl <estelle@openwebdocs.org> * Update files/en-us/web/css/reference/properties/content/index.md Co-authored-by: Estelle Weyl <estelle@openwebdocs.org> * Update files/en-us/web/css/reference/properties/content/index.md Co-authored-by: Estelle Weyl <estelle@openwebdocs.org> * Update files/en-us/web/css/reference/properties/content/index.md Co-authored-by: Estelle Weyl <estelle@openwebdocs.org> * Update example according to estelle feedback * Update files/en-us/web/css/reference/properties/content/index.md Co-authored-by: Estelle Weyl <estelle@openwebdocs.org> --------- Co-authored-by: Estelle Weyl <estelle@openwebdocs.org>
1 parent 0521262 commit f7f6f16

1 file changed

Lines changed: 54 additions & 3 deletions

File tree

  • files/en-us/web/css/reference/properties/content

files/en-us/web/css/reference/properties/content/index.md

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ content: image-set("image1x.png" 1x, "image2x.png" 2x);
5151

5252
/* speech output: alternative text after a "/" */
5353
content: url("../img/test.png") / "This is the alt text";
54+
content: counter(chapter) / "Chapter " counter(chapter);
5455

5556
/* <string> value */
5657
content: "unparsed text";
@@ -91,7 +92,7 @@ The value can be:
9192
- One of two keywords: `none` or `normal`. `normal` is the default property value.
9293
- `<content-replacement>` when replacing a DOM node. `<content-replacement>` is always an `<image>`.
9394
- A `<content-list>` when replacing pseudo-elements and margin boxes. A `<content-list>` is a list of one or more anonymous inline boxes appearing in the order specified. Each `<content-list>` item is of type [`<string>`](#string), [`<image>`](#image), [`<counter>`](#counter), [`<quote>`](#quote), [`<target>`](#target), or [`<leader()>`](#leader).
94-
- An optional alternative text value of a `<string>` or `<counter>`, preceded by a slash (`/`).
95+
- An optional alternative text value that can include `<string>`, `<counter>`, or [`attr()`](#attrx) function values, preceded by a slash (`/`).
9596

9697
The keywords and data types mentioned above are described in more detail below:
9798

@@ -131,8 +132,8 @@ The keywords and data types mentioned above are described in more detail below:
131132
- `attr(x)`
132133
- : The `attr(x)` CSS function retrieves the value of an attribute of the selected element, or the pseudo-element's originating element. The value of the element's attribute `x` is an unparsed string representing the attribute name. If there is no attribute `x`, an empty string is returned. The case sensitivity of the attribute name parameter depends on the document language.
133134

134-
- alternative text: `/ <string> | <counter>`
135-
- : 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. The {{cssxref("string", "/ &lt;string>")}} or {{cssxref("counter", "/ &lt;counter>")}} data types specify the "alt text" for the element.
135+
- alternative text: `/ <string> | <counter> | attr()`
136+
- : Alternative text may be specified for an image or any `<content-list>` items, by appending a forward slash and then a combination of strings, counters, and `attr()` functions. The alternative text is intended for speech output by screen-readers, but may also be displayed in some browsers.
136137

137138
## Formal definition
138139

@@ -342,6 +343,56 @@ a::before {
342343
343344
If using a screen reader, it should speak the word "MOZILLA" when it reaches the image. You can select the `::before` pseudo-element with your developer tools selection tool, and view the {{glossary("accessible name")}} in the accessibility panel.
344345

346+
### Including counters in alternative text
347+
348+
This example features a list of links to a set of book chapters, and shows how to use generated content to include a book icon and a counter before each one, with alternative text that includes the literal word "Chapter" in place of the icon. This results in the word "chapter" and the chapter number preceding the text in each link's {{glossary("accessible name")}}, which will be announced to screenreader users when the link receives focus.
349+
350+
#### HTML
351+
352+
We include a heading followed by an ordered list of chapter title links using {{htmlelement("ol")}}, {{htmlelement("li")}}, and {{htmlelement("a")}} elements.
353+
354+
```html live-sample___alt-counter
355+
<h2>Chapter list</h2>
356+
<ol>
357+
<li><a href="#">A stranger calls</a></li>
358+
<li><a href="#">Two owls</a></li>
359+
<li><a href="#">Dinner was bland</a></li>
360+
<li><a href="#">Three owls</a></li>
361+
<li><a href="#">No-one answered the door</a></li>
362+
<li><a href="#">The stranger leaves</a></li>
363+
<li><a href="#">Bedtime</a></li>
364+
</ol>
365+
```
366+
367+
#### CSS
368+
369+
The CSS includes a {{cssxref("counter-reset")}} for the `chapter` counter on the `<ol>` element. We also increment the `chapter` counter on each `<li>` element using {{cssxref("counter-increment")}}, and remove the list markers by setting a {{cssxref("list-style-type")}} value of `none`.
370+
371+
```css live-sample___alt-counter
372+
ol {
373+
counter-reset: chapter;
374+
}
375+
376+
li {
377+
counter-increment: chapter;
378+
list-style-type: none;
379+
}
380+
```
381+
382+
Next, we set the `<a>` elements' {{cssxref("::before")}} pseudo-elements to have generated `content` equal to a book emoji to represent a chapter, plus the current `chapter` counter value, and a space character so that the generated content is separated from the link text. Finally, we set the generated content's alt text to the current `chapter` counter value preceded by the word "Chapter".
383+
384+
```css live-sample___alt-counter
385+
a::before {
386+
content: "📖 " counter(chapter) " " / "Chapter " counter(chapter);
387+
}
388+
```
389+
390+
#### Result
391+
392+
{{EmbedLiveSample('alt-counter', '100%', 270)}}
393+
394+
When a screenreader navigates to a link within the list, supporting browsers will announce "Chapter" followed by the current counter number, followed by the link text, for example, "Chapter 1 A stranger calls" and "Chapter 2 Two owls".
395+
345396
### Element replacement with URL
346397

347398
This example replaces a regular element! The element's contents are replaced with an SVG using the {{cssxref("url_value", "&lt;url&gt;")}} type.

0 commit comments

Comments
 (0)