Long words can easily break your layout, especially in languages like German with lots of compound words. You donât even need a âDonauÂdampfschiffÂfahrtsgesellÂschaftsÂkapitänâ if the word is in a big headline on a small screen.
In this article I want to share what Iâve learned about this topic over the last years.
Automatic hyphenation in the frontend
It surely would be great to just add a few lines of code and consider it done. Unfortunately, itâs not that easy. But letâs have a look at the simplest solution first.
You donât need much to enable automatic hyphenation in the frontend. Just set your HTML lang attribute correctly and add the following CSS:
.text {
hyphens: auto;
}
Browser support is good nowadays, but hyphenation quality unfortunately isnât. Sometimes youâll see too many hyphenated lines in a row. Other times automatic hyphenation produces âleg-endaryâ breaks that can confuse readers. It might be worse in German but Iâve seen it in English as well.
Hereâs an example of how Google Chrome hyphenates a German text:
During my apprenticeship at a print magazine a long time ago, I learned about hyphenation the hard way. After accidentally hyphenating âKlosterfrauâ (roughly translated to ânunâ) as âKlo-sterfrauâ (where âKloâ means toilet) in a printed issue, they made me double-check every single hyphenation from then on. Some lessons you never forget đ˝
Taking more control on the server
For more control, you can handle hyphenation on the server using libraries like Syllable for PHP. They add soft hyphens based on syllables and language rules.
Among other settings, you can set minimum word lengths to prevent excessive hyphenation:
$syllable->setMinWordLength(10);
If youâre using Kirby, you might want to have a look at my plugin kirby-hyphenate.
In the example image above, the words âZeitplanâ, âarbeitetâ, âzusammenâ, and âbehebenâ would not be hyphenated with a minimum word length of 10.
Itâs still automatic, though. No matter how good the library is, it canât know the context of your text (at least I havenât seen a library that can).
Manual hyphenation
In my experience, manual hyphenation is the most reliable way to handle long words. Itâs also the most time-consuming, but itâs worth it. Letâs look at our options:
Adding soft hyphen HTML entities
The easiest way is to use the ­
HTML entity to add invisible soft hyphens that only wrap words when needed. Thatâs what the above mentioned library does, too. It can make text in the backend quite un­read­able
, though.
Making the HTML entity more readable
For a very long time, I simply converted (-)
into ­
. This makes the soft hyphens easier to type and the text stays relatively read(-)able
.
Using Kirby, itâs quite easy with a kirbytags hook:
Kirby::plugin('medienbaecker/shy', [
'hooks' => [
'kirbytags:before' => function ($text, $data, $options) {
return Str::replace($text, "(-)", "­");
}
]
]);
Using the unicode character directly
Depending on how excessively you hyphenate, even the (-)
might go on your nerves. It would be great to toggle visibility of the soft hyphens like in professional editorial software, right?
Just use the unicode character for soft hyphens U+00AD
directly!
You can copy it from sites like Unicode Explorer or setup a shortcut or Raycast snippet like I did.
Iâve recently found this great Kirby plugin called Typo & Paste that makes it easy to insert special characters. Itâs a great alternative for less tech savvy editors.
Then, depending on your text editor or content management system of choice, you can toggle the visibility of invisible characters on and off.
Because it can look very different depending on the tool, hereâs a simple visual representation:
This way you can also identify non-breaking-spaces and other invisible characters.
In Kirby you can use the markdown fieldâs invisibles button or the kirby-hidden-characters plugin for other fields. I love how there are so many typography-related plugins for Kirby!
The soft skills around soft hyphens
Often itâs the editor, not yourself who has to do the (manual) hyphenation. Therefore itâs important they know how and when to hyphenate.
In some projects in the past I used a simple plugin for Kirbyâs markdown field that visually highlights long words. This way editors could see at a glance where they might want to add soft hyphens:
In the case of the markdown field, itâs actually quite easy to add custom highlights.
You just need a tiny bit of PHP:
<?php
Kirby::plugin('medienbaecker/lengthy-word-locator', [
'fabianmichael.markdown-field.customHighlights' => [
[
'name' => 'lengthy-word',
'regex' => '\b[\wäÜßĂĂĂĂ]{20,}\b',
'class' => 'lengthy-word',
],
]
]);
Then you can style the highlights with CSS:
.lengthy-word {
background-color: rgba(255, 128, 0, 0.25);
}
Finding what works for you
In most projects right now, Iâm using completely manual hyphenation while trying to make it as easy as possible for editors to handle this themselves. I also make sure to talk to them about the importance of hyphenation and how to do it.
This article ended up being something of a chronological journey through my experiences with hyphenation â from automatic solutions to server-side processing, and finally to manual hyphenation with proper tooling. Not the most structured article perhaps, but an honest reflection of how I learned to deal with long words in web typography.
If you have your own experiences or tools to share, Iâd love to hear about them on Mastodon.