Automatic hyphenation depends on the defined document language (#tilPost)

The lang HTML attribute (e.g. lang="en") is essential for creating accessible websites. Without it, browsers and assistive technology can only guess the website’s language leading to a poor user experience.
And while I was…

The lang HTML attribute (e.g. lang="en") is essential for creating accessible websites. Without it, browsers and assistive technology can only guess the website’s language leading to a poor user experience.

And while I was aware that the attribute is important, I didn’t know that it also affects how browsers deal with long words and hyphenation.



hyphens: auto and the lang attribute

If you’re displaying text with long words in narrow containers, you might run into overflow situations. Long words will break out of their containers.

If you look at the overflowing words and the container width, the only solution is to break the words into pieces and add hyphens.

The hyphens CSS property can help here.



Define where to break long words manually

The default value of the hyphens property is manual. With manual hyphenation, you’re in charge of defining when and how a word should be divided. Use a visible hyphen (U+2010 / ‐ (HYPHEN)) or an invisible "soft" hyphen (U+00AD / ­ (SHY)) character to define the hyphenation breakpoints.

Both character cues are used to break words apart. Unfortunately, the manual way is not scalable.

Think of a site that includes hundreds of pages maintained by various people. Hyphens then need to be rendered in different word locations depending on the responsive layout. A long word on a mobile device might need hyphenation, whereas the same word might be acceptable in a large-screen layout. An all-time-visible hyphen won’t do it!

And adding an invisible character to break long words properly… well… you can’t expect writers and editors to fiddle around with invisible HTML-encoded characters. That’s not going to work either.

<!-- That's too complicated ? -->
<div>un&shy;imaginative&shy;ly</div>


Let browsers automatically hyphenate long words

Another approach is to use hyphens: auto. With this CSS declaration, you’re throwing the burden of hyphenating words to the browser side. MDN documents the auto value as follows:

The browser is free to automatically break words at appropriate hyphenation points, following whatever rules it chooses.

After playing around with text containing long words, I learned that the document language plays a role in how browsers hyphenate words.

Have a look at the demo below to see how the lang attribute affects automatic hyphenation.

I’m not 100% sure how to correctly hyphenate English words, but I guess that the hyphenation is better with the correct language.

And there you have, the lang attribute does not only make sites more accessible but also affects how long words are divided. We should better make sure it’s defined. ?

If you want to read more about hyphenation and the lang attribute, have a look at these two excellent articles:



Reply to Stefan


Print Share Comment Cite Upload Translate
APA
Stefan Judis | Sciencx (2024-04-26T04:58:12+00:00) » Automatic hyphenation depends on the defined document language (#tilPost). Retrieved from https://www.scien.cx/2021/07/25/automatic-hyphenation-depends-on-the-defined-document-language-tilpost/.
MLA
" » Automatic hyphenation depends on the defined document language (#tilPost)." Stefan Judis | Sciencx - Sunday July 25, 2021, https://www.scien.cx/2021/07/25/automatic-hyphenation-depends-on-the-defined-document-language-tilpost/
HARVARD
Stefan Judis | Sciencx Sunday July 25, 2021 » Automatic hyphenation depends on the defined document language (#tilPost)., viewed 2024-04-26T04:58:12+00:00,<https://www.scien.cx/2021/07/25/automatic-hyphenation-depends-on-the-defined-document-language-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » Automatic hyphenation depends on the defined document language (#tilPost). [Internet]. [Accessed 2024-04-26T04:58:12+00:00]. Available from: https://www.scien.cx/2021/07/25/automatic-hyphenation-depends-on-the-defined-document-language-tilpost/
CHICAGO
" » Automatic hyphenation depends on the defined document language (#tilPost)." Stefan Judis | Sciencx - Accessed 2024-04-26T04:58:12+00:00. https://www.scien.cx/2021/07/25/automatic-hyphenation-depends-on-the-defined-document-language-tilpost/
IEEE
" » Automatic hyphenation depends on the defined document language (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2021/07/25/automatic-hyphenation-depends-on-the-defined-document-language-tilpost/. [Accessed: 2024-04-26T04:58:12+00:00]
rf:citation
» Automatic hyphenation depends on the defined document language (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2021/07/25/automatic-hyphenation-depends-on-the-defined-document-language-tilpost/ | 2024-04-26T04:58:12+00:00
https://github.com/addpipe/simple-recorderjs-demo