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) ?
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.
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-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.
Finally I did small change in my layout which given a standard result across all the browsers.
Now user input will go into the table & it wraps the small words, breaks the long words if they are overflowing from the container.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With