Open any well‑set book and you will notice two things that never happen: a line never ends with a dangling ‘to’, ‘of’ or ‘in’, and a compound word never breaks right after its hyphen. Professional typesetting has enforced both rules for more than a century. The web somehow forgot them — and most sites break both every day, in their headlines, which are the last place you want to look careless.
We found both tells on our own site: a Russian‑language headline carried ‘SEO‑оптимизированные’, and at common viewport widths the browser broke the line right after the dash. A company that sells search optimization cannot afford a torn ‘SEO-’ in a headline. So we fixed the problem properly — not by hand‑editing one headline, but with a pass that removes both tells from every page we render. The answer is less obvious than it looks.
In short
- Browsers break lines after a regular hyphen. The hyphen‑minus is a legal break point, so compounds like
SEO-optimizedget torn in half at the end of a line. - Short words dangle at line ends. ‘of’, ‘in’, ‘the’ — glued to the following word with a non‑breaking space, they stop wandering off alone.
- CSS has no fix for the hyphen. No property stops the break after a literal hyphen; the canonical fix is the character itself — U+2011, the non‑breaking hyphen.
- Best‑in‑class libraries don’t cover it either. typograf v7.8, the strongest Russian typography engine, ships dash and quote rules but no non‑breaking‑hyphen rule.
- Apply the fix in one pass, at two layers. A remark plugin transforms markdown at build; an Astro middleware transforms server‑rendered HTML at runtime — text nodes only, code never touched.
- Presentation is trust. No dashboard metric here, just craft: a page that doesn’t tear words apart reads like it was made by people who care about details.
The two tells
When a browser lays out text, the ordinary hyphen‑minus is one of its legal break opportunities — reasonable for long hyphenated names, except that the break is allowed right after the hyphen, exactly where you don’t want it. SEO-optimized becomes SEO- at the end of one line and optimized at the start of the next. In English it bites more rarely, because fewer terms are built this way — but it bites: state-of-the-art, end-to-end. In Russian and Serbian, hyphenated compounds are the standard way to build terms, so the tears multiply. Each one is a small tear in the text, and in headlines — large type, narrow containers — they show up constantly.
The second tell is older and better known: a preposition or another short function word left at the end of a line — ‘The state of’ / ‘the art’. Typographers solved this long ago by gluing the short word to the word that follows it with a non‑breaking space, so the pair wraps as a unit. The web has the same character — the no‑break space, U+00A0 — yet almost nobody applies it systematically, because doing it by hand across a site is a losing game.
Both tells share one nasty property: every individual line looks fine. Nothing is broken in the CSS, no validator complains, no test fails. The page is wrong the way a book is wrong when its margins wander — you need an editor’s eye to see it, and your visitors have that eye even when they can’t name what bothers them.
Why CSS cannot fix it
The natural first reaction is to look for a CSS property. There isn’t one. The hyphens property governs automatic hyphenation of long words, word-break and overflow-wrap handle emergency breaking of unbreakable strings — none of them can look at a hyphen that already exists in the source text and decide not to break after it. Browsers treat the hyphen‑minus as breakable by specification, and no stylesheet overrides that.
For spaces, the escape hatch has existed for decades: a no‑break space makes two words inseparable. For hyphens the same trick exists in Unicode: U+2011, NON‑BREAKING HYPHEN. Same width, same shape, one behavioral difference — no line break after it. This is the canonical answer to ‘how do I prevent a line break at a hyphen’, and it works in all browsers, because the spot simply stops being a break opportunity. The fix is not a layout rule; it is a change to the text itself — so the place to apply it is the pipeline that produces the text, not the stylesheet.
Why we didn’t reach for a library
The obvious next thought is ‘typography is a solved problem — plug in a library’. We checked. The strongest engine for Russian typography is typograf, and it is genuinely excellent: dashes, quotes, spacing, ellipses, the whole classic set. But as of v7.8 it has no rule that turns a breakable hyphen into a non‑breaking one — its dash rule converts hyphens into em dashes, a different concern entirely. And wiring it in wholesale would rewrite dashes and quotes across copy that had already been proofread and approved, changing the voice of the text on every page at once.
We needed exactly two rules — the hyphen and the short words — applied surgically, leaving everything else alone. That is a small, auditable piece of code, not a dependency.
Two rules, one pass
The engine is a pair of regular expressions. The first: any hyphen between a letter or a digit becomes U+2011. That catches state-of-the-art, E-E-A-T, end-to-end — and never touches a hyphen surrounded by spaces, which is a dash in running text, or a hyphen in a URL.
The second glues short words to what follows. The little words are language‑specific: English glues ‘of’, ‘in’, ‘to’, ‘the’ and their neighbours; Serbian shares the same list and brings its own; Cyrillic text switches to a Russian list. The choice is heuristic — Cyrillic gets the Russian list, everything else the shared one — but a false positive costs nothing: a non‑breaking space renders exactly like a regular space, so the worst case is a word pair staying together a line longer than strictly necessary.
Both rules skip code: inline code and fenced blocks are excluded at the markdown level. Nobody wants a non‑breaking hyphen inside a command they are about to copy.
The gap that middleware closed
That engine ran as a remark plugin and covered every markdown article at build time. It did not cover the page where we first saw the problem — that page is not built from markdown. Its headlines come from a UI data module and are rendered on the server, and no remark plugin ever sees that text. This gap bites every statically‑minded fix: the site is server‑rendered on Cloudflare, so a build‑time transform is structurally blind to part of the HTML.
The answer was an Astro middleware — a function that sees every response before it goes out. It masks everything that must never be touched — script and style contents, code, textarea, kbd, samp elements, HTML comments — with placeholder characters, applies the two rules only to text between tags, and restores the placeholders. The pass is idempotent — running it twice changes nothing — which makes it safe where the same text may be transformed at build and again at render.
Since the middleware landed, the two rules apply to every HTML page we serve: at build for prerendered pages, per‑request for server‑rendered ones. The SEO-оптимизированные headline now wraps as a whole word at every viewport width, and no line ends with a dangling preposition.
Summary
Bad line breaks are not a design problem — no mockup can show them, because they depend on the width of the viewer’s window. They are an engineering problem, and the fix belongs in the pipeline, not in anyone’s daily routine. Two Unicode characters, two regexes, one pass at build and one at render, and the oldest tells of careless typesetting are gone from the site for good.
If you want a site built with this level of attention to detail, look at our web development work — the same engineering culture applies to everything we ship. And if the details are what interest you, the approach hub is where we write about them.
Yuri Osipov
Founder, SEO & analytics
Runs Take the Space. Specialises in technical SEO, measurable analytics setups and AI search visibility.