Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing long words from flowing out of Div without using CSS break-word

Tags:

css

word-wrap

I have a div and all the long words (without spaces) flow outside the div.

Please don't mark the question as a duplicate of How to prevent long text from flowing out of a container or Long words are flowing out of the box - How to prevent?, where the problem is solved by using word-wrap: break-word;.

The disadvantage of word-wrap: break-word; is that it also breaks short words which are at the edge of the div, in a way that disrupts the flow of the text. I want short words to remain the way they are and only break the long words. Is it possible to implement this? How do other websites handle it?

like image 660
Jigar Jain Avatar asked Apr 06 '13 11:04

Jigar Jain


People also ask

How do I stop Div text from overflowing?

To fix this problem, avoid using vw for your max-width and use a max-width of 100% instead. This will transfer the width of the parent container to the width of the child container.

How do you make words not break in CSS?

use white-space: nowrap; . If you have set width on the element on which you are setting this it should work. It's white-space: nowrap actually.


1 Answers

it also breaks short words which are at the edge of the div

That's not true...word-wrap: break-word; shouldn't do that.
Perhaps you're confusing this with the word-break: break-all; property (which doesn't work in all browsers).

See this jsfiddle for a comparison: http://jsfiddle.net/Snu8B/3/

For firefox you could try the hyphens property.

like image 171
mihai Avatar answered Oct 07 '22 03:10

mihai