Optional line-breaking HTML entity that is always invisible

HtmlLine BreaksHtml Entities

Html Problem Overview


I want an optional line-breaking character that is always invisible that works with the word-wrap: break-word; CSS style.

Here are some specifics. My goal is to break apart long links in reasonable places. These characters are a good place to start: -, ., _, /, \. This is not a Rails-specific question, but I wanted to share some code I'm using now:

module ApplicationHelper
  def with_optional_line_breaks(text)
    text.gsub(%r{([-._/\\])}, '\1­')
  end
end

Here's the problem with the code above: when ­ takes effect (in a table with: word-wrap: break-word;), ­ gets displayed as -. I don't want to see the -; I want a line break without any character shown.


Html Solutions


Solution 1 - Html

​ is the HTML entity for a unicode character called the zero-width space (ZWSP).

> "In HTML pages, this space can be used as a potential line-break in long words as an alternative to the <wbr> tag."- Zero-width space - Wikipedia

The <wbr> tag also works, as mentioned by Aaron's answer. I think I prefer the HTML entity over the tag because the entity seems simpler: unicode handles it, not the web browser.

Solution 2 - Html

<wbr> looks like it does what you want, but it looks like the support for it, and &shy; for that matter, is very inconsistent. So unfortunately, there may not be a particularly good way to do what you want.

Solution 3 - Html

I'll post this as an answer, in 2019, although it draws its substance entirely from other contributions on this page: use <wbr>. It works well in allowing the wrap of long URLs and so not having them break out of content boxes. Users being able to paste the link you show into a web browser does matter and support for <wbr> is good in modern browsers, according to caniuse.com and my own quick tests in Chrome and Firefox for Android. I replaced forward slashes with forward slashes and a WBR, URLs now wrapping nicely.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid J.View Question on Stackoverflow
Solution 1 - HtmlDavid J.View Answer on Stackoverflow
Solution 2 - HtmlAaron MaenpaaView Answer on Stackoverflow
Solution 3 - HtmlGeoff KendallView Answer on Stackoverflow