html

How to Force a Line Break in a Long Word in a DIV

There are tables and boxes created through div in HTML documents that contain text information inside. The problem arises when there is a large word with a huge number of characters, and because of this, the text flows out of the container neglecting the boundary of the container.

CSS word-wrap property that formats the written text automatically by breaking the long word into parts when needed. The “word-wrap” property moves the parts of the word to the next line according to the size of the container.

Forcing a Line Break in a Long Word in a Div

Simply add the word-wrap property with the break word value/attribute in the CSS style element that refers to the div.

Syntax
The exact syntax to add the word-wrap property is as follows:

word-wrap: break-word;

Problem: Content Overflows the div

Let’s discuss this with the help of a simple example of a div that has the text inside with a long word:

<h2>ithout word-wrap: break-word Element</h2>
<div class="class1"> The line break element in html formats the written text automatically by breaking the long word with a lot of characters into parts when needed. For instance, if there is a veeeeeeeeeeeeeeeeerrrrrrrrrrrrrrrryyyyyyyyyyyyy long word</div>

This will display the following result in the output. This looks very problematic as the text is overflowing out of the div:

Solution: Adding “word-wrap” Property

Now, if we take the same div container with the same text inside as added above in this post:

<h2>With word-wrap: break-word Element</h2>
    <div class="class2">The line break element in html formats the written text automatically by breaking the long word with a lot of characters into parts when needed. For instance, if there is a veeeeeeeeeeeeeeeeerrrrrrrrrrrrrrrryyyyyyyyyyyyy long word</div>

Now, in the CSS style element, it is just required to refer to the div class, id, or attribute in which the long problematic word has been written and then simply add the word-wrap property:

.class2 {
  word-wrap: break-word;
}

This will be the output generated through the above code snippet. Now, this looks presentable because the word-wrap property did divide the text characters into multiple lines rather than overflowing out of the container:

This is how we can force a line break in a word that has a lot of characters.

Conclusion

To force a line break in a long word in a div in such a way that it moves the parts of the words to the next lines according to the size of the container, there is a CSS word-wrap property with the value break-word. In the CSS style element, it is just required to add a selector for referring to the div container in which the word is created and then write the word-wrap property.

About the author

Hadia Atiq

A Software Engineer and Technical Writer passionate about learning and spreading knowledge of the latest technology. I utilize my writing skills to help readers understand the importance and usage of modern technology.