Skip to content

Latest commit

 

History

History
155 lines (138 loc) · 3.9 KB

hiddencontent.md

File metadata and controls

155 lines (138 loc) · 3.9 KB
title layout description permalink page_title sidenav
Hidden Content
post
How and when to hide content
/accessibility/hidden-content/
Hidden Content
accessibility

Hiding content is very useful for accessibility. We can hide things visually and only display it to screen reader users, we can hide content from screen reader users and only show it visually, or we can hide content from both.

Techniques

{% capture css_display_none %}

  {
    display:none;
  }

{% endcapture %}

{% capture css_sr_only %}

  .sr-only {
    border: 0; 
    clip: rect(0 0 0 0); 
    height: 1px; 
    margin: -1px;
    overflow: hidden;
    padding: 0;
    position: absolute;
    width: 1px;
  }

{% endcapture %}

Technique Visually Hidden Screen reader hidden Additional Info
CSS:
{{ css_sr_only | markdownify }}
Yes No There are multiple ways to accomplish this with CSS. This is the current way we are reccomending it.
CSS:
{{ css_display_none | markdownify }}
Yes Yes
HTML5 attribute:
hidden
Yes Yes In supported browsers, this is the same as {{ css_display_none | markdownify }}
aria attribute:
aria-hidden='false'
No No This is overwritten by other techniques. i.e. Using {{ css_display_none | markdownify }} will cause the element not to be read or seen.
aria attribute:
aria-hidden='true'
No Yes

Aria Hidden

aria-hidden should be used in combination with these techniques. If we want to hide something from just the screen reader, you can mark it as aria-hidden='true'.

Items with aria-hidden='true' are always ignored by the screen reader. This is useful for:

  • Collapsing Menus
  • Repetitive information
  • Off screen content

If an element has multiple states, it's visibility should be tracked with aria-hidden true/false. An element with aria-hidden='false' is treated by the screen reader as if it didn't have the aria-hidden attribute and is read or not read based on other factors, such as CSS.

CSS

CSS can be used to hide content and allow it to still be used with the screen reader. For an example of that, see this code here,

.sr-only {
  border: 0;
  clip: rect(0 0 0 0);
  height: 1px;
  margin: -1px;
  overflow: hidden;
  padding: 0;
  position: absolute;
  width: 1px;
}

Using this we can:

  • Provide additional details about a form element
  • Hidden Labels
  • Error Details
  • Provide Context to page elements
  • Add aditional instructions for navigation

We can also use CSS to visually hide and Screen reader hide content.

{
  display: none;
}

This will hide completly and is the same as <div aria-hidden='true' hidden>content</div> Note that aria-hidden='true' should be used for additional compatibility.

display: none; and hidden can be used for:

  • Collapsing Menus
  • Repetitive information
  • Off screen content

Additional Resources

For more information on this topic, see these articles.