I have a decorative element in the header that separates links "|". It is added as a CSS content element:
::after {
content: "|";
.....
}
In HTML it is presented inside of the span along with the header link:
<span>
<a ...>
Link
</a>
</span>
In some screen readers, "|" separator is being displayed and I want to make it invisible for the screen readers by adding "aria-hidden". Is there any way to add it to the CSS file? Or is there any other way to make a CSS content property invisible for screen readers?
I saw an example of adding ARIA property in square brackets like so:
::after[aria-hidden] {
content: "|";
.....
}
However, it would remove the visibility of the whole element completely, not only for screen readers.
No, it's impossible. This is one of the drawback of CSS generated content, and why it's quite discouraged from accessibility point of view. You can't add HTML attributes to an element using CSS. If you want to control ARIA attributes like aria-hidden, you have to add the content in the HTML code.
Just for your information, most screen readers render CSS generated contents as normal, but some don't at all with some browsers. So, CSS content isn't recommended either for conveying important information.
Note that your proposition,
::after[aria-hidden]don't add the attribute to the generated element, but tries to match a generated element having the attribute. As it isn't possible to add any attribute to CSS generated elements, it won't ever match anything. It doesn't make any sense, and is in fact invalid according to CSS specification.