Search code examples
htmlcssaccessibilitywai-aria

How to hide a text and make it accessible by screen reader?


I have a simple text that gets updated on an action and I want that to be announced by the screen reader. But I don't want that text to be visible on the web page. I tried display: none and visibility: hidden, but seems like they are not accessible by the screen reader softwares. I found a way to make this work - that is by absolute positioning the element all the way with negative 999999 value which will make it off screen and hidden from the webpage. I am not really a fan of this solution. Is there a more elegant way to achieve this?

<span class="aria-invisible" aria-live="polite">5 selections have been made.</span>

.aria-invisible {
   display: none; //either of these two attributes
   visibility: hidden;
}

Solution

  • I did encounter this problem in the past. Bootstrap has this sweet class sr-only that actually hides the content on the webpage but is accessible by the screen readers. You can check this link

    Moreover, if you are not using bootstrap, you can simply implement the class yourself in your code.

    .aria-invisible {
          border: 0; 
          clip: rect(0 0 0 0); 
          height: 1px;  
          margin: -1px;
          overflow: hidden;
          padding: 0;
          position: absolute;
          width: 1px;
        }
    <span class="aria-invisible">5 selections have been made. </span>

    I hope this helps.