Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word wrap a link so it doesn't overflow its parent div width [duplicate]

I have this piece of code:

div#permalink_section {    width: 960px  }
<div id='permalink_section'>    <a href="here goes a very long link">here goes a very very long link</a>  </div>

The link text can be very long and it overflows the div when it's length does exceed the div width. Is there a way to force the link to break and go on the next line when its width exceeds the div width?

like image 919
Florent2 Avatar asked Mar 09 '11 04:03

Florent2


People also ask

How do I keep my DIV from expanding?

You should use display: table; It will shrink to the size of it's contents and can also be centered and positioning without having to assign a given width.

What is the difference between word-wrap and word break?

What is the difference between “word-break: break-all” versus “word-wrap: break-word” in CSS ? The word-break property in CSS is used to specify how a word should be broken or split when reaching the end of a line. The word-wrap property is used to split/break long words and wrap them into the next line.


2 Answers

The following is a cross browser compatible solution:

#permalink_section {     white-space: pre-wrap; /* CSS3 */         white-space: -moz-pre-wrap; /* Mozilla, since 1999 */     white-space: -pre-wrap; /* Opera 4-6 */         white-space: -o-pre-wrap; /* Opera 7 */         word-wrap: break-word; /* Internet Explorer 5.5+ */ } 

From How do I wrap text with no whitespace inside a <td>?

Check working example here.

like image 170
Hussein Avatar answered Sep 26 '22 02:09

Hussein


If you're okay with CSS3, there's a property for that:

word-wrap:break-word; 
like image 39
corroded Avatar answered Sep 25 '22 02:09

corroded