The HTML DOM style wordWrap property returns and modify the long word to be broken and wrap onto the next line in an HTML document.
Syntax
Following is the syntax −
Returning wordWrap
object.style.wordWrap
Modifying wordWrap
object.style.wordWrap = “value”
Values
Here, value can be −
Value | Explanation |
---|---|
initial | It set this property value to its default value. |
inherit | It inherits this property value from its parent element. |
normal | It breaks words only when necessary. |
break-word | It allows unbreakable words to be broken down when necessary. |
Example
Let us see an example of HTML DOM style wordWrap property −
<!DOCTYPE html> <html> <head> <style> body { color: #000; background: lightblue; height: 100vh; } p { border: 2px solid #fff; width: 100px; } .btn { background: #db133a; border: none; height: 2rem; border-radius: 2px; width: 40%; display: block; color: #fff; outline: none; cursor: pointer; margin: 1rem 0; } </style> </head> <body> <h1>DOM Style wordWrap Property Example</h1> <p>This is a paragraph element. This is a veryveryveryverylongword. This is another veryveryverylongword.</p> <button onclick="add()" class="btn">Set wordWrap</button> <script> function add() { document.querySelector('p').style.wordWrap = "break-word"; } </script> </body> </html>
Output
This will produce the following output −
Click on “Set wordWrap” button to allow long words to wrap −