Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break word after special character like Hyphens (-)

Tags:

html

text

css

Given a relatively simple CSS:

div {    width: 150px;  }
<div>    12333-2333-233-23339392-332332323  </div>

How do I make it so that the string stays constrained to the width of 150, and wraps to a new line on the hyphen?

like image 989
Karl Seguin Avatar asked Aug 04 '08 00:08

Karl Seguin


People also ask

How do you break words with a hyphen in CSS?

hyphens: manual Words are only broken at line breaks where there are characters inside the word that suggest line break opportunities. There are two characters that suggest line break opportunity: U+2010 (HYPHEN): the “hard” hyphen character indicates a visible line break opportunity.

Which property will break the text at hyphens only?

The word-break property in CSS can be used to change when line breaks ought to occur. Normally, line breaks in text can only occur in certain spaces, like when there is a space or a hyphen.


2 Answers

Replace your hyphens with this:

&shy; 

It's called a "soft" hyphen.

div {    width: 150px;  }
<div>    12333&shy;2333&shy;233&shy;23339392&shy;332332323  </div>
like image 90
Ryan Fox Avatar answered Sep 20 '22 20:09

Ryan Fox


In all modern browsers* (and in some older browsers, too), the <wbr> element is the perfect tool for providing the opportunity to break long words at specific points.

To quote from that link:

The Word Break Opportunity (<wbr>) HTML element represents a position within text where the browser may optionally break a line, though its line-breaking rules would not otherwise create a break at that location.

Here's how it could be used to in the OP's example (or see it in action at JSFiddle):

<div style="width: 150px;">   12333-<wbr>2333-<wbr>233-<wbr>23339392-<wbr>332332323 </div> 

*I've tested it in IE9, IE10, and the latest versions of Chrome, Firefox, and Opera, and Safari.

div {    width: 150px;  }
<div>    12333-<wbr>2333-<wbr>233-<wbr>23339392-<wbr>332332323  </div>
like image 27
aeskr Avatar answered Sep 21 '22 20:09

aeskr