7

Is there any way to have a dash ( - ) that wont break the word if it's ending a line? (Just like   prevents a line break between two words)

The problem that I currently have is a blog post (dynamic) that has the word X-Ray in it. Sadly, it falls on the end of a line so I get

Lorem ipsum dolor si amet X-
Ray

I would like to prevent that, and get a result like this

Lorem ipsum dolor si amet
X-Ray

I can't just <br /> everything, because depending on where you are on the website, it won't be stuck in that position everywhere.

Fredy31
  • 2,660
  • 6
  • 29
  • 54
  • possible duplicate of [No line-break after a hyphen](http://stackoverflow.com/questions/7691569/no-line-break-after-a-hyphen) – rds Feb 24 '15 at 03:25

4 Answers4

5

Try a non-breaking hyphen, &#8209;

Info at http://www.fileformat.info/info/unicode/char/2011/index.htm

5

You can use the Unicode Character 'NON-BREAKING HYPHEN' (U+2011).

HTML Entity (decimal)   &#8209;
HTML Entity (hex)   &#x2011;
How to type in Microsoft Windows    Alt +2011
UTF-8 (hex) 0xE2 0x80 0x91 (e28091)
UTF-8 (binary)  11100010:10000000:10010001
UTF-16 (hex)    0x2011 (2011)
UTF-16 (decimal)    8,209
UTF-32 (hex)    0x00002011 (2011)
UTF-32 (decimal)    8,209
C/C++/Java source code  "\u2011"
Python source code  u"\u2011"
kba
  • 19,333
  • 5
  • 62
  • 89
1

The way that works most widely is <nobr>X-ray</nobr>. Despite not being part of any HTML spec, the nobr tag works well.

Nowadays, using the non-breaking hyphen U+2011 (suggested in other answers; you can also enter it directly, if you know how to do that in your editor and you are using UTF-8) works almost as widely. There are risks, however. Support to U+2011 is not universal in fonts. This means that unless you take special measures to use a font that contains it, or to use suitable backup fonts, it is quite possible that the non-breaking hyphen looks different from the normal hyphen (which is mostly the “Ascii hyphen” on web pages).

This means that foo-bar and foo‑bar (with non-breaking hyphen) may look different, since they come from different fonts. It might take a trained eye or a typographer to see the difference, but in some font combinations, the difference is clearly observable.

Using nobr, you don’t have this problem, since the same character is used. The same applies to using <span style="white-space: nowrap">X-ray</span>, but it’s a bit clumsier and a little bit less reliable (does not work when CSS is disabled).

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
-1

This site Will provide you with more information than you'll ever need.

&#8211;

Is what you're after though.

Alexander Wigmore
  • 3,157
  • 4
  • 38
  • 60