Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a soft hyphen (­) in HTML / CSS

I am trying to word-wrap an email address within a div, where otherwise it would overflow because it's too long for the div's width.

I know this issue has been covered here before (e.g. this question), but read-on, because I cover all the possible solutions mentioned there and elsewhere.

None of the below solutions do exactly what I want:

  1. CSS

    "word-wrap: break-word".
    

    Depending on the div's width, this breaks the email address at awkward places. e.g.

    [email protected]

    k

  2. Use a Soft Hyphen within the HTML:

    ­
    

    This is really well supported, but renders a visible hyphen on the page, leading user to believe the email address has a hyphen in there:

    info@long-

    emailaddress.co.uk

  3. Use a thinspace or zero-width space within the email address:

      (thinspace)
    ​ (zero-width space)
    

    Both of these insert extra characters (bummer if user copy-pastes)

  4. Linebreaks...

    <br />
    

    ... are out because, most of the time, the div is large enough to contain the email address on one line.

I guess I'm hoping for some ingenious way of doing this in HTML/CSS, perhaps making use of pseudo elements (e.g. :before / :after), or by using a soft hyphen but somehow hiding it with CSS in some clever way.

My site uses jquery, so I'll resort to that if I must, although I'd rather not include a whole hyphenation library just for this one little problem!

Answers on a postcard please. (Or, ideally here...)

like image 588
hazymat Avatar asked Aug 21 '12 13:08

hazymat


People also ask

How do I get rid of a hyphen?

Select the text. On the Home tab, expand the Paragraph group. In the Paragraph dialog box, select the Line and Page Breaks tab. Under Formatting exceptions, select the Don't hyphenate check box.

How do you make a hard hyphen in word?

Click where you want to insert a nonbreaking hyphen. Press CTRL+SHIFT+HYPHEN.

What is soft hyphen in word?

A soft hyphen is an option for hyphenation. That is, it will remain invisible and will have no effect on the text, unless it is at the end of the line that must be hyphenated, when it will be used to determine how to hyphenate the word.


3 Answers

You can clip the text and use a tooltip

   width: 100px;
   overflow: hidden;
   text-overflow: ellipsis;

What I do is on hover show the full text as tooltip or use the title attribute for tooltip.

<p class="email" title="[email protected]">...</p>
like image 196
Robin Maben Avatar answered Oct 12 '22 01:10

Robin Maben


Maybe you can try <wbr> instead of <br>
Unfortunately, this won't work in IE.

like image 25
Simon Arnold Avatar answered Oct 12 '22 02:10

Simon Arnold


word-wrap:break-word is based on the width of the container you want to break the word in. It's not going to break it where you decide to. You are unfortunately out of luck unless you want to reduce the width of the div so that it breaks where you want.

The other solutions, as you've discovered, are inadequate for your needs. Additionally, using a "jQuery hyphenation library" probably won't fix your issue either because it'll just be injecting the characters, line breaks, or so on just as you tried. You end up with the same problem.

I don't mean any offense by this, but maybe it would be good to rethink the design, rather than work around the design? Robin's answer is a decent alternative, though you won't be able to select the email address without javascript.

like image 45
Brendan Avatar answered Oct 12 '22 02:10

Brendan