Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you wrap long words on newline, and avoid horizontal scroll using CSS?

I have the folowing html:

<div class="box">
    long text here
</div>

and css:

.box {
    width: 400px;
    height: 100px;
    overflow: auto;
    border: 1px gold solid; 
}

I want only a vertical scroll. But when a word is too long, a horizontal scroll is displayed. How do I make the long words wrap ?

If needed, I can use a trick with jQuery or PHP, but I would like to solve it using CSS, because it's CSS job.

You can fiddle here: http://jsfiddle.net/879bc/1/

like image 520
Benjamin Crouzier Avatar asked Oct 13 '11 21:10

Benjamin Crouzier


People also ask

How do I stop my CSS from scrolling horizontally?

To hide the horizontal scrollbar and prevent horizontal scrolling, use overflow-x: hidden: HTML. CSS.

How do you wrap a new line in CSS?

break-word: This is the actual CSS syntax that tells the browser to wrap a long text over to a new line. normal: It breaks each word at the normal points of separation within a DOM. It doesn't have effect on long strings. initial: It's the default browser's way of handling strings.

How do you break a long text in CSS?

Breaking long words The word-wrap property is now treated by browsers as an alias of the standard property. An alternative property to try is word-break . This property will break the word at the point it overflows.


2 Answers

word-wrap: break-word

https://developer.mozilla.org/en/CSS/word-wrap

like image 52
DA. Avatar answered Oct 12 '22 20:10

DA.


For custom word breaking, there is an html special character that is not so often used- &shy; (soft hyphen) - that will split words on 2 lines and insert a dash after the first part of the word if the word approaches the edge of its container. Trouble is, you'll have to place them everywhere you want them. As you say, though, you can also set up a js or php function, and insert them into the appropriate places.

like image 32
uɥƃnɐʌuop Avatar answered Oct 12 '22 21:10

uɥƃnɐʌuop