5

Lets say I have a fixed-width container with a long string inside, no spaces:

div {
    background-color:yellow;
    width:100px;
    height:100px;
    position:relative;
}
<div>veryveryveryveryveryveryveryveryveryveryveryverylongstring</div>

How do I force the text to wrap via CSS? And what control do I have over that wrapping style (using a hyphen or not for example)?

I'm aware of the text-wrap property, but that doesn't seem to apply to a long string like this. Maybe I'm wrong. Could someone please explain this?

2 Answers 2

9

You're looking for the word-break property. You could use word-break: break-all:

div {
    background-color: yellow;
    width: 100px;
    height: 100px;
    position: relative;
    word-break: break-all;
}
<div>veryveryveryveryveryveryveryveryveryveryveryverylongstring</div>

Sign up to request clarification or add additional context in comments.

4 Comments

funny, doesn't work for me in the 'Run code snippet'
@deW1 That's odd... I didn't realize word-break: break-word wasn't supported in FF... break-all seems to.
I was pretty sure that it was at least some time ago.
@deW1 Yeah, me too.. I've been using it for a few years without any issues.. if anything I just remember IE8 being problematic..
1

This will also suffice

div {
  word-wrap: break-word;
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.