DEV Community

Sandrina Pereira
Sandrina Pereira

Posted on • Edited on

9 2

A11Y study case: Breaking text on a specific word and Screen Readers implications

Asking to visually break a sentence in multiple lines on a strategic word is a common request from the design team, especially on landing pages. Eg “Hello /n human being”. However, that might affect a Screen Reader pitch flow in unexpected ways...

Scenario issue

For example, let's imagine we have the title "Hello human being" and we are asked to break it after the "hello" word.

Hello   
human being
Enter fullscreen mode Exit fullscreen mode

Here are some common (poor) approaches I've seen being used around:

  1. Add <br /> between each line: Don't do this! Despite being very common, because of its ease of use, it's semantically incorrect! <br> is meant to be used when the division of lines is significant. E.g. stress addresses, poems, etc...
  2. Add <span /> with display:block between each line. This creates the same effect as <br /> but without any semantics, which is good.
  3. Using flexbox: Wrapping each line into an <span> and adding flexbox to the sentence itself with flex-direction: column does the trick too.

However, all of these three approaches have the same unexpected result in the VoiceOver screen reader pitch flow:

  • Expected pitch: "Hello human being"
  • Actual pitch: "Hello" [pause] human being."

Another common practice is to set max-width, however, that solution is highly dependent on the text content. In this scenario that approach wouldn't work.

Shocase failed max-width attempt

Let's keep looking for a bulletproof solution instead.

Possible solution

Among other experiments, using write-space: pre-line; seem the best solution! We can create a literal newline or use &NewLine; control character to break the text.

<h2 style="white-space:pre-line;">Hello       
human being</h2>

<h2 style="white-space:pre-line;">Hello &NewLine;human being</h2>
Enter fullscreen mode Exit fullscreen mode

This is just an example of how HTML semantics and CSS usage can affect the way text is read out loud in ways we might not expect.

Here's a Codepen where you can try out the approaches described and other solutions attempts:

Image of Datadog

The Essential Toolkit for Front-end Developers

Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

Get The Kit

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay