No line-break after a hyphen
Stop the text break at a hyphen by applying white-space: nowrap
with CSS:
Assign the classy class to your HTML elements:
This CSS trick keeps your hyphenated content on a single line.
Equally important, ‑
is the non-breaking hyphen alternative for squeezing specific hyphens:
⁠
is your safeguard against unexpected line breaks, acting as a line break repellent around a hyphen or other characters. Run multiple browser tests to ensure none flees.
Alternative options with explanations
HTML entity: the non-breaking hyphen
The entity ‑
is your best friend when a specific hyphen needs to commit to not breaking:
Banishing breaks with a word joiner
To ensure all characters in a sequence follow hyphen’s suit, encase them with the word joiner, ⁠
:
Abandoning obsolete tags
Ditch the <nobr>
tag—a trend past its prime. Embrace CSS instead:
Correct character encoding and DOCTYPE
Start HTML documents equipped with the correct DOCTYPE and character encoding:
Embracing cross-browser harmony
Verify your solution's performance in the pageant of browsers:
Further exploration
For complex usage scenarios involving, say, dynamic or user-generated text, you might need additional scripting solutions or regular expressions.
Was this article helpful?