0

I have a text inside a div with a hyphen in it. When the div gets too small, the text overflows instead of breaking at the hyphen.

I tried a bit with word-break and overflow, but couldn't figure out how to make the text break only at the hyphen. Here is an example of what is happening, and what I am trying to achieve.

div{
  width:70px;
  border:1px solid;
}
<b>How it is now </b>
<div> bla bla-blablabla</div>
<br>

<b> What should happen: </b>
<div> bla bla-<br>blablabla</div>

Edit for the duplicate question: The answers given there are wrong.

  1. The best (by votes) answer suggests replacing hyphens by &shy;. This is incorrect, since those hyphens will NOT show up when there is no line break.
  2. The second answer suggests <wbr>, but this is not supported in all browsers.
  3. The third best answer suggests CSS word-wrapping, which does not make it break at hyphens.
BSMP
  • 4,596
  • 8
  • 33
  • 44
user4493177
  • 750
  • 11
  • 32
  • 1
    its already breaking in your snippet – Gaurav Aggarwal Jun 09 '16 at 10:12
  • 1
    Possible duplicate of [In HTML, how to word-break on a dash?](http://stackoverflow.com/questions/904/in-html-how-to-word-break-on-a-dash) –  Jun 09 '16 at 10:17
  • @GauravAggarwal then it probably doesn't break for every browser, dont you think? – user4493177 Jun 09 '16 at 10:17
  • @blonfu Thank you for your suggestion. I checked that page, but the above two answers are not correct. The first one uses shy hyphens, which will not show up when there is no line break, and the second uses word break all, which also gives another result. The other answers also don't give a full solution for all browsers. – user4493177 Jun 09 '16 at 10:20
  • is possible use javascript? –  Jun 09 '16 at 10:30

2 Answers2

1

use word-wrap: break-word and will break when there is no more space. not when "we" want.

div {
  width: 70px;
  border: 1px solid;
}
div:first-of-type {
  word-wrap: break-word
}
<b>How it is now </b>
<div>bla bla-blablabla</div>
<br>

<b> What should happen: </b>
<div>bla bla-
  <br>blablabla</div>

UPDATE due to you really want to break only in hyphen you can use either &shy; or wbr

div {
  width: 70px;
  border: 1px solid;
}
<b>How it is now </b>
<div>bla bla-blablabla</div>
<br>

<b> What should happen: </b>
<div>bla bla-
  <br>blablabla</div>

<hr />
<b>How it is now using &amp;shy;</b>
<div>bla bla&shy;blablabla</div>
<br>

<b>How it is now using wbr</b>
<div>bla bla-<wbr>blablabla</div>
<br>
dippas
  • 58,591
  • 15
  • 114
  • 126
  • This doesn't work. In firefox, it breaks during the second word, but not on the hyphen. – user4493177 Jun 09 '16 at 10:21
  • @user4493177 as I said, it will break where is no more space to write. if you change the words, you will see what I'm talking about – dippas Jun 09 '16 at 11:07
  • Right, but the question was about breaking on hyphen. I know what word break does, but it is no solution for this question. Same goes for shy, it is simply no answer to this question. Only wbr provides the desired solution, but lacks support in internet explorer (before IE 12). – user4493177 Jun 09 '16 at 11:18
  • @user4493177 why `­` it is no solution for you? it is replacing the hyphen, and as you already said you want to **break only** when there is an **hyphen** – dippas Jun 09 '16 at 11:23
  • With shy, the hyphen is not visible when there is no line break. – user4493177 Jun 09 '16 at 11:32
  • You won't find a bullet proof solution using only CSS for cross browser compatible – dippas Jun 09 '16 at 11:40
  • Is curious, in IE `` works only with a `-` before, but not with others characters. –  Jun 09 '16 at 12:41
0

With jquery if you can use:

$(function() {
$('div').each(function() {
  $(this).html($(this).text().replace(/-/, '-<br>'));
});
});
div{
  width:80px;
  border:solid 1px red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div> bla bla-blablabla blablabla-blabla</div>

UPDATE

$('div').each(function() {
  $(this).html($(this).text().replace(/-/g, '<span></span>'));
});
div{
  width:80px;
  border:solid 1px red;
}

span:after{
  content:"-";
  display:inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div> bla bla-blablabla babababs sbba-blabla</div>
  • doesn't this simple replace all - by -
    , therefore also inserting newlines when the div is wide enough to show everything on one line?
    – user4493177 Jun 09 '16 at 10:48
  • I edit my answer. Maybe is better put the hypen in the `span` and remove from `content`, because in `content` isn`t selectable. –  Jun 09 '16 at 11:00