How to wrap text inside and outside box using CSS ?
In this article, we are going to cover how one can wrap the text inside and outside the box using the CSS properties.
Approach: We will be using the “overflow-wrap” property. This property comes into the picture when the length of the content exceeds the parent component length. The “overflow-wrap” property can have mainly five values.
The “normal” value will break lines according to the normal line breaking rules of the browser. The “inherit” will inherit the property of the parent element. But the value we are looking for is the “break-word“.
The “break-word” property will break any string or word which exceeds the boundary of the parent block or division and will try to fit the content into the block providing a new line.
We will give the height and width property to fix value and not to auto, as auto increases the width based on the content length.
HTML code: We have created one div with the class name “box” which is responsible for giving the fixed height, width, and overflow conditions. Inside this div box, we have our content <p> tag which is holding string values.
Class box without overflow condition: If the CSS used above is changed to the following for the class “box“.
In the above, we have not given any overflow conditions in the class box, so the normal behavior will be followed, and it will not break the lines until the break line is not specified which is white space. In this case, our content will overflow the box.
Class box with overflow condition: If the above CSS is modified with the following code.
After providing the overflow condition on the parent block box, we achieve the word wrapping on the content. Whenever the <p> tag content tries to exceed the boundary our overflow-wrap: break-word will break the word “chekolosvakialRegion” and tries to fit in the box.
We have successfully wrapped the content inside and outside the box using CSS property.