Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "word-wrap:break-word" for a div without specifying width

Tags:

html

css

I have to wrap the text in a div which has the width property set to auto.

I have to take the input from users and have to display it. Sometimes users are inputting data without spaces. Normal strings are wrapping fine so far without any styles. But long strings without spaces are overflowing from the container. Here I want to use the "word-wrap:break-word;" to wrap the text. But it is working when specifying the specific width to the div. But when I specify width my layout is corrupting across the browsers.

Is there any solution to use word-wrap without specifying width property (It should work in all the browsers) ?

like image 295
Awesome Avatar asked Oct 29 '13 06:10

Awesome


People also ask

How do I force wrap text in a div?

You can try specifying a width for the div, whether it be in pixels, percentages or ems, and at that point the div will remain that width and the text will wrap automatically then within the div.

How do I split a word in a div?

Use word-wrap:break-word; It even works in IE6, which is a pleasant surprise. word-wrap: break-word has been replaced with overflow-wrap: break-word; which works in every modern browser.

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

Finally I did small change in my layout which given a standard result across all the browsers.

  1. Added a table inside my div (Here my table will have only one row & one column).
  2. Given the following styles to the table - "table-layout: fixed;width: 100%;word-wrap: break-word;"

Now user input will go into the table & it wraps the small words, breaks the long words if they are overflowing from the container.

like image 97
Awesome Avatar answered Sep 29 '22 08:09

Awesome


Here is another version of using table:

<div class="break-word-container">
    very_long_word_without_spaces
</div>

and here are the styles:

.break-word-container {
    display: table;
    table-layout: fixed;
    width: 100%;
    word-wrap: break-word;
}
like image 20
Tomasz Szymulewski Avatar answered Sep 29 '22 08:09

Tomasz Szymulewski