Explain Codes LogoExplain Codes Logo

How to prevent line break at hyphens in all browsers

html
responsive-design
best-practices
cross-browser-testing
Alex KataevbyAlex Kataev·Dec 16, 2024
TLDR

Prevent text from breaking at hyphens using white-space: nowrap; in CSS to make it appear on a single line:

/* Solid like a rock, unbreakable like an unbroken egg! */ .no-break { white-space: nowrap; }

Apply this style to your HTML element:

<!-- Like a long, unbroken train ride! --> <span class="no-break">non-breaking-hyphenated-word</span>

To dodgily dodge lengthy manual edits, dynamically replace ordinary hyphens with non-breaking variants in your content by employing some JavaScript or jQuery magic.

Cheat the system with Unicode

The non-breaking hyphen trick

Nothing screams "No breaks!" louder than a Unicode NON-BREAKING HYPHEN (U+2011). To make sure your hyphenated words soldier on without a midway hiccup across lines in any browser, replace standard hyphens with their Unicode non-breakable counterparts, &#x2011; or &#8209;.

The unbroken vow of CSS white-space property

Pledge unbreakability with white-space: nowrap;. This rule, daringly applied to individual elements or a class, ensures your words stick together. Just double-check browser compatibility, especially if you're dealing with the wild world of IE or Edge.

Jester JavaScript and jQuery

A large content body can be swiftly controlled with JavaScript or jQuery. These witty wizards can sneakily replace all instances of hyphens while you enjoy your cup of coffee.

/* The life hack you wish you knew before! */ document.body.innerHTML = document.body.innerHTML.replace(/-/g, '&#8209;');

The curious case of CKEditor & CMS

In the monstrous maze of CKEditor or any CMS, make certain that non-breaking hyphens aren’t traduced back to their ordinary counterparts. Investigate your editor's configuration and content sanitization rules.

Coding your way around exceptions

The word joiner character

The word joiner character (&#8288;) can be your lifesaver in complex situations involving editable content.

<!-- This code comment is a break-proof word joiner --> <span>word&#8288;-&#8288;word</span>

The mysterious <nobr> markup

Though it's a maverick in HTML and known to induce quirky browser behavior, <nobr> can protect your hyphenated text like a protective shell around an armoured egg.

<!-- You know when a code comment has no breaks? --> <nobr>hyphenated-word</nobr>

IE and its eccentricities

For the Internet Explorer pundits out there, buckle up! Your compatibility ride may need some extra effort, notably with older versions. Test your code judiciously across different IE versions.

Treating CMS like a teddy bear

Provide consistent formatting across your CMS by ensuring your content sanitization rules are airtight. This may involve some tweaks in your CMS backend or plugins.

Handling browser eccentricities

Cross-browser testing isn't just recommended, it's mandatory! Use platforms like BrowserStack or revert to good ol' manual testing on various devices.