Reputation: 1477
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?
Upvotes: 3
Views: 13295
Reputation: 4578
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;
}
Upvotes: 1
Reputation: 301
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
Upvotes: 1
Reputation: 8346
You could also use, word-wrap: break-word
;
.wrapper .right{
display: block;
width:200px;
}
.wrapper .right{
word-wrap:break-word;
}
Upvotes: 2
Reputation: 15699
.right
should not be inline
. Also, assign some width to it.
.wrapper .right{
display: block;
width: 200px;
}
.wrapper .right{
word-break: break-all;
}
Upvotes: 1