Good afternoon!
I propose to add a new HTML/CSS Code style recommendation on our HTML and CSS codestyle page, in the WCAG section:
Properly hide elements:
- Hiding visually and semantically:
E.g. an element disappearing from the UI after a user interaction.
- CSS: Use the property and value:
display: none
.- HTML: For inputs only, use the attribute and value:
type="hidden"
.- Hiding visually and showing semantically:
E.g. a label for an interactive element that would just clutter the visual UI.
- HTML: Use the class:
sr-only
. (abbreviation of âscreen reader onlyâ)- Hiding semantically and showing visually:
{{warning}} This technique should only be used on an element that could be removed without changing the user experience in any other way than the presentation.
Example of proper usage: The avatar picture to display a âUserâ entry in a table. Itâs always paired with the name of the user, so itâs not an issue to hide it from assistive technologies. In this case itâs better, since the information is repeated in two elements.
Example of grey area use: A separator in a list. The separator can be seen as nothing but a blank space, but for the user it implies a âseparationâ between two groups of list elements. This separation should be reflected on a semantic meaning, hiding the separator is not the best solution.
Example of a clear misuse: Hiding a picture used to illustrate a <p>. Accessible content is content that conveys the same meaning no matter the technology used to read it. The picture has a purpose and meaning, and hiding this from assistive technologies prevents AT users from getting the full experience.
Use only on leave nodes of the DOM tree because this technique does not hide the content but only the semantics of the node. If you want to hide more than a leaf node, itâs very probable that you should consider hiding it visually too. If itâs still not possible, using thearia-hidden
attribute is the last resort.
{{/warning}}
- HTML: Use the attribute and value:
role="presentation"
.
Those are obviously not the only possible ways to hide elements, but I figured that if we could be consistent with our practice it would improve code quality. This doesnât cover all situations, but most of them. Some questions should be asked anyways when using other possible techniques.
I decided to prefer role=presentation
over aria-hidden
. For me, even if the presentation
role is a bit less powerful of a , it does not rely on the aria standards but html ones and therefore will have better support and compatibility.
What do you think of this proposal? Is the wording too heavy or imprecise? Is this an appropriate level of detail for a code style convention or should we make it shorter with details in a child page?
Thanks in advance for your feedback
Lucas C.
Sources: