0
votes

http://codepen.io/rick-li/pen/oshCb

<div class="wrapper">
  <div class="left"></div>
  <span>lfjaslkjasldjfaljdfaldflasjflasjdfldasfsdafafasdfsdafadsfazxcvzvzxv</span>
</div>
.wrapper{
  width: 400px;
  height: 300px;
  border: solid 1px;
}

.wrapper .left{
  float: left;
  width: 200px;
  height: 200px;
  border: solid 1px;
  background-color: #111111
}

.wrapper span{
  word-wrap: break-word;
}

you will notice the text dropped to the bottom,

if there're spaces exist in the text, it will wrap correct and lies on the wright, so I wonder why the word-wrap: break-word or word-wrap: break-all doesn't work?

4
Erm, I just tried this in Chrome, and it does work: codepen.io/anon/pen/FqCzA using break-word.Ming
@setek, you didn't get it, the content if the whole span.right dropped. if you add several spaces, it will wrap correctly and lies on the right.Rick Li
Oh I get it now, okay :) answering ..Ming

4 Answers

0
votes

.right should not be inline. Also, assign some width to it.

.wrapper .right{
  display: block;
  width: 200px;
}
.wrapper .right{
  word-break: break-all;
}

DEMO here.

0
votes

You could also use, word-wrap: break-word;

.wrapper .right{
  display: block;
  width:200px;
}
.wrapper .right{
  word-wrap:break-word;
}
0
votes

NoobEditor in reference to your answer did you mean to say "word-wrap only works when there are no spaces rather than when there are spaces in the word? The reason why word-wrap is not working in the code above is due to wrong placement of closing div.

<div class="wrapper">
    <div class="left"></div> <!-- here is your problem -->
    <span>lfjaslkjasldjfaljdfaldflasjflasjdfldasfsdafafasdfsdafadsfazxcvzvzxv</span>
</div>

If you close div tag in the right place, word-wrap will work:

<div class="wrapper"> 
    <div class="left">
    <span>lfjaslkjasldjfaljdfaldflasjflasjdfldasfsdafafasdfsdafadsfazxcvzvzxv</span>
    </div> <!-- close statement here -->
</div> <!-- close statement here -->

See it here

0
votes

You can do this a couple of ways, you can change the display from inline so that you can set a max-width, allowing it to know when it should break the word:

http://codepen.io/anon/pen/ibvKq

.wrapper .right{
  display: inline-block;
  max-width: 290px;
  word-wrap: break-word;
}

Alternatively, if you want to keep the inline, you need to couple the word-wrap: break-word; with white-space: pre; but this does mean it will preserve line-breaks and spaces. Personally, I'd use the first one over this one.

http://codepen.io/anon/pen/cfhwI

.wrapper .right{
  display: inline;
  white-space: pre;
  word-wrap: break-word;
}