How can I wrap or break long text/word in a fixed width span?

I want to create a span with a fixed width that when I type any thing in the span like <span>lgasdfjksdajgdsglkgsadfasdfadfasdfadsfasdfasddkgjk</span>, a long string of non-spaced text, the word(s) break or wrap to next line.

Any ideas?


Solution 1:

You can use the CSS property word-wrap:break-word;, which will break words if they are too long for your span width.

span { 
    display:block;
    width:150px;
    word-wrap:break-word;
}
<span>VeryLongLongLongLongLongLongLongLongLongLongLongLongExample</span>

Solution 2:

Try following css with addition of white-space:

span {
    display: block;
    word-wrap:break-word;
    width: 50px;
    white-space: normal;
}

Solution 3:

Like this

DEMO

  li span{
    display:block;
    width:50px;
    word-break:break-all;
}

Solution 4:

By default a span is an inline element... so that's not the default behavior.

You can make the span behave that way by adding display: block; to your CSS.

span {
    display: block;
    width: 100px;
}

Solution 5:

Try this

span {
    display: block;
    width: 150px;
}