Properly hiding elements
Hiding an element should be done with respects to the semantics. A reflection about the role of this element and how it can be used by multiple types of users should be undertaken.
Thereafter are described three ways to hide elements, based on what users it's hidden from.
Visually refers to users that interact with the content based on the visual rendering supported browsers would make.
Semantically refers to users that interact with the content based on what assistive technologies provide them. Those assistive technologies base their representation on semantics in the page.
Hiding visually and semantically
Examples
An element disappearing from the UI after a user interaction; a label or description only for screen readers, referenced via aria-labelledby or aria-describedby (see this WAI-ARIA practice about naming elements).
Solution based on CSS
Use the property and value: display: none.
Solution based on HTML
- For inputs, use the attribute and value: type="hidden".
- For others, use the boolean attribute: hidden 1.
Hiding visually and showing semantically
Examples
A label for an interactive element that would just clutter the visual UI.
Solution based on HTML
Use the class: sr-only. (abbreviation of “screen reader only”)
Hiding semantically and showing visually
Solution based on HTML
Use the attribute and value: role="presentation".
- ^ This means avoiding using a dedicated class such as .hidden.