Skip to main content
whitep4nth3r logo

How to hide text in CSS pseudo elements from screen readers

Learn how to hide decorative text generated by CSS from screen readers, so that your weird designs don't interrupt the flow of the text.

⚠️ This post is over two years old and may contain some outdated technical information. Please proceed with caution!

Unfortunately this doesn't yet work in Safari or Firefox, but you can keep up to date with support on caniuse.com.

TL:DR; Add empty "alternative text" to the pseudo element content to prevent screen readers announcing your decorative design elements, like so:

.someClass::before {
content: "M" / "";
}

I'm currently redesigning my website, and part of the design includes decorative elements provided by a font, specified as single letters in the content of CSS pseudo elements. Psst! If you don't know what a CSS pseudo element is, read this post first: What's the difference between : and :: in CSS?

Here's some code that adds a large stylised paint brush glyph as a pseudo element attached to the <main> HTML element of the page.

main::before {
font-family: "Atomic Marker Extras";
content: "M";
position: fixed;
z-index: -1;
/* ... */
}

When using VoiceOver for MacOS to read the page using that code, the letter M is announced. Not ideal! You can also confirm what will be announced using the Accessibility Tree view (enabled via an experimental flag in Chromium browsers), which gives you a visual display of all elements and their content that accessibility tools will have access to.

Browser accessibility tree view, showing a static text element with the content M as referenced in the code example above, that will be read out by screen readers.

Fortunately, the CSS content property comes with the ability to specify alternative text, which is especially useful if you're using images in your pseudo elements that actually mean something. And what's more, if you want to tell the browser that the content is purely decorative and doesn't need to be seen by assistive technology, you can provide an empty value for the alternative text, like so.

main::before {
font-family: "Atomic Marker Extras";
- content: "M";
+ content: "M" / "";
position: fixed;
z-index: -1;
/* ... */
}

And that's it! One handy lesser-known feature of CSS to help you make your weird and wonderful website designs more enjoyable to browse, for everyone, everywhere.

Like weird newsletters?

Join 497+ subscribers in the Weird Wide Web Hole to find no answers to questions you didn't know you had.

Subscribe

I stand on stage at what the stack as if it is 1995, which is indicated by the vintage 90s shirt I am wearing. I am holding the mic. I am looking into the distance. I am gesticulating. I am powerful. I command the stage. I am one with the presentation.

Salma Alam-Naylor

I'm a software engineer and developer educator, and I make stuff on the internet. I help developers build cool stuff by writing blog posts, making videos, coding live on the internet, and publishing open source projects. Head of Developer Education at nordcraft.com.

Did this post help you?

☕️ Buy me a coffee

(thank you!)

Related posts

31 Aug 2022

What's the difference between : and :: in CSS?

I spent years Googling this question before the information stayed in my brain. Sound familiar? Then this post is for you.

CSS 3 min read →

16 May 2021

How to make your font sizes accessible with CSS

Here's how to make sure your website respects font size preferences specified in browser settings using two important CSS concepts.

Tutorials 3 min read →