Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional line-breaking HTML entity that is always invisible

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.


like image 520
David J. Avatar asked Apr 05 '13 18:04

David J.


People also ask

Why we use& nbsp in HTML?

A commonly used entity in HTML is the non-breaking space:   A non-breaking space is a space that will not break into a new line. Two words separated by a non-breaking space will stick together (not break into a new line). This is handy when breaking the words might be disruptive.

Which HTML tag allows you to break the line?

The <br> HTML element produces a line break in text (carriage-return). It is useful for writing a poem or an address, where the division of lines is significant.


2 Answers

&#8203; 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.

like image 161
David J. Avatar answered Sep 22 '22 03:09

David J.


<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.

like image 22
Aaron Maenpaa Avatar answered Sep 19 '22 03:09

Aaron Maenpaa