CSS Properties
CSS overflow-wrap Property
Photo Credit to CodeToFun
🙋 Introduction
The overflow-wrap
property in CSS is used to specify how the browser should handle long words or unbreakable text that overflows the boundaries of its container.
This property helps improve the readability and layout of text content, especially in responsive design scenarios. It ensures that text does not break the layout or cause horizontal scrolling issues.
💡 Syntax
The overflow-wrap
property can be applied to any block-level or inline-level element. It has the following syntax:
element {
overflow-wrap: value;
}
Here, value can be one of the predefined keywords described below.
🎛️ Default Value
The default value of the overflow-wrap
property is normal. This means that the browser will use its default line-breaking rules, which may not always prevent long words from overflowing their container.
🏠 Property Values
Value | Description |
---|---|
normal | Allows words to overflow their container if they do not fit. This is the default behavior. |
break-word | Breaks long words and wraps them onto the next line to prevent overflow. This value ensures that even if a word is too long to fit, it will break to avoid horizontal scrolling. |
📄 Example
In this example, we’ll apply the overflow-wrap
property to a paragraph element to handle long words gracefully.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS overflow-wrap Example</title>
<style>
.text-container {
width: 200px;
border: 1px solid #ccc;
padding: 10px;
overflow-wrap: break-word;
}
</style>
</head>
<body>
<h1>Text Container with Overflow Wrap</h1>
<div class="text-container">
This is an example of a verylongwordthatwillbreakandwrapcorrectlyinsideitscontainerwithoutcausinganyoverflowissues.
</div>
</body>
</html>
🖥️ Browser Compatibility
The overflow-wrap
property is widely supported across modern browsers, including the latest versions of Chrome, Firefox, Safari, Edge, and Opera. It is a reliable solution for managing text overflow issues in most web environments.
🎉 Conclusion
The overflow-wrap
property is a valuable tool for controlling how text content behaves when it exceeds its container.
By using this property, you can improve the readability and user experience of your website by ensuring that text wraps correctly and avoids causing layout problems. Experiment with different values to see how they affect your text content and find the best fit for your design needs.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS overflow-wrap Property), please comment here. I will help you immediately.