Tillitsdone
down Scroll to discover

CSS Overflow-wrap Control Text Wrapping Easily

Learn how to use CSS overflow-wrap to control text wrapping within elements.

Prevent overflow with options like normal, anywhere, and break-word.
thumbnail

Introduction

The overflow-wrap property in CSS helps control how text wraps within an element to prevent overflow. Originally known as word-wrap, it’s now widely supported across browsers. This property is especially useful for handling long words or URLs that might otherwise overflow their containers, making it a handy tool for web developers and designers.

Specification

The overflow-wrap property is defined in the CSS Text Module Level 3 specification. This specification outlines how browsers should handle text overflow within elements. The property is supported across modern browsers, ensuring consistent text display across different platforms and devices.

For detailed information, you can refer to the CSS Text Module Level 3 specification.

Description

The overflow-wrap property in CSS controls how text wraps within an element to prevent overflow. It’s particularly useful for handling long words or URLs that might extend beyond their container. By using overflow-wrap, you can ensure that text remains readable and contained within its designated space.

This property is beneficial in responsive web design, where container sizes can vary. By managing text wrapping effectively, you can create more flexible and adaptable layouts that maintain text integrity regardless of container dimensions.

Syntax

Here’s the syntax for the overflow-wrap property:

overflow-wrap: normal;
overflow-wrap: break-word;
overflow-wrap: anywhere;
/* Global values */
overflow-wrap: inherit;
overflow-wrap: initial;
overflow-wrap: revert;
overflow-wrap: revert-layer;
overflow-wrap: unset;

Values

  • normal: Text breaks only at standard word break points, like spaces between words.
  • anywhere: Text can break at any point if there are no acceptable break points in the line.
  • break-word: Similar to anywhere, but soft wrap opportunities are not considered when calculating intrinsic sizes.

Global values (inherit, initial, revert, revert-layer, unset) provide additional control over how the property is applied.

Formal Definition

The overflow-wrap property is formally defined in the CSS Text Module Level 3 specification. Here are its key attributes:

PropertyInitial ValueApplies ToInheritedComputed ValueAnimation Type
overflow-wrapnormaltext elementsYesas specifieddiscrete

Formal Syntax:

overflow-wrap = normal | break-word | anywhere

Examples

Let’s look at some examples to see how overflow-wrap works in practice.

HTML

<p>
They say the fishing is excellent at Lake
<em class="normal">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>, though
I've never been there myself. (<code>normal</code>)
</p>
<p>
They say the fishing is excellent at Lake
<em class="ow-anywhere">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
though I've never been there myself. (<code>overflow-wrap: anywhere</code>)
</p>
<p>
They say the fishing is excellent at Lake
<em class="ow-break-word">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
though I've never been there myself. (<code>overflow-wrap: break-word</code>)
</p>
<p>
They say the fishing is excellent at Lake
<em class="word-break">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
though I've never been there myself. (<code>word-break</code>)
</p>
<p>
They say the fishing is excellent at Lake
<em class="hyphens">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>, though
I've never been there myself. (<code>hyphens</code>, without
<code>lang</code> attribute)
</p>
<p lang="en">
They say the fishing is excellent at Lake
<em class="hyphens">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>, though
I've never been there myself. (<code>hyphens</code>, English rules)
</p>
<p class="hyphens" lang="de">
They say the fishing is excellent at Lake
<em class="hyphens">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>, though
I've never been there myself. (<code>hyphens</code>, German rules)
</p>

CSS

p {
width: 13em;
margin: 2px;
background: gold;
}
.ow-anywhere {
overflow-wrap: anywhere;
}
.ow-break-word {
overflow-wrap: break-word;
}
.word-break {
word-break: break-all;
}
.hyphens {
hyphens: auto;
}

Result

In this example, we compare the results of different text wrapping properties when dealing with a long word. Here’s what each class does:

  • .normal: Text wraps according to standard word break points.
  • .ow-anywhere: Text breaks at any point to prevent overflow.
  • .ow-break-word: Similar to .ow-anywhere, but soft wrap opportunities are not considered.
  • .word-break: Text breaks at any point to prevent overflow.
  • .hyphens: Text uses hyphens to break words at appropriate points, following language-specific rules.

Comparing overflow-wrap, word-break, and hyphens

Overflow-wrap

  • normal: Text breaks only at standard word break points.
  • anywhere: Text can break at any point if there are no acceptable break points.
  • break-word: Similar to anywhere, but soft wrap opportunities are not considered.

Word-break

  • normal: Uses the default line break rule.
  • break-all: Breaks words at any point to prevent overflow.
  • keep-all: Prevents breaks within a word.

Hyphens

  • none: No hyphenation.
  • manual: Hyphenation occurs only at explicitly defined hyphenation points within the text.
  • auto: Automatic hyphenation based on the language’s rules.

By understanding and using these properties effectively, you can ensure that text is always neatly displayed within its container, enhancing the overall user experience.

Understanding the hyphens Property

The hyphens property in CSS controls how words are broken and hyphenated for better readability. Here’s a quick rundown of the values:

  • none: No hyphens, words aren’t broken.
  • manual: Hyphens only where you place a soft hyphen ­.
  • auto: Automatic hyphenation based on language rules defined by the lang attribute.

Example Comparison

Let’s see how these properties affect text wrapping with an example.

HTML

<p>
They say the fishing is excellent at Lake
<em class="normal">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>, though
I've never been there myself. (<code>normal</code>)
</p>
<p>
They say the fishing is excellent at Lake
<em class="ow-anywhere">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
though I've never been there myself. (<code>overflow-wrap: anywhere</code>)
</p>
<p>
They say the fishing is excellent at Lake
<em class="ow-break-word">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
though I've never been there myself. (<code>overflow-wrap: break-word</code>)
</p>
<p>
They say the fishing is excellent at Lake
<em class="word-break">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>,
though I've never been there myself. (<code>word-break</code>)
</p>
<p>
They say the fishing is excellent at Lake
<em class="hyphens">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>, though
I've never been there myself. (<code>hyphens</code>, without
<code>lang</code> attribute)
</p>
<p lang="en">
They say the fishing is excellent at Lake
<em class="hyphens">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>, though
I've never been there myself. (<code>hyphens</code>, English rules)
</p>
<p class="hyphens" lang="de">
They say the fishing is excellent at Lake
<em class="hyphens">Chargoggagoggmanchauggagoggchaubunagungamaugg</em>, though
I've never been there myself. (<code>hyphens</code>, German rules)
</p>

CSS

p {
width: 13em;
margin: 2px;
background: gold;
}
.ow-anywhere {
overflow-wrap: anywhere;
}
.ow-break-word {
overflow-wrap: break-word;
}
.word-break {
word-break: break-all;
}
.hyphens {
hyphens: auto;
}

Results

  • normal: Text wraps at standard word break points like spaces. Long words may overflow.
  • overflow-wrap: anywhere: Text breaks anywhere to prevent overflow, but breaks might look unnatural.
  • overflow-wrap: break-word: Breaks text at any point to prevent overflow, useful for long words or URLs.
  • word-break: break-all: Text breaks at any point, useful for languages without spaces between words.
  • hyphens: auto: Text breaks at appropriate points with hyphens, following language-specific rules.

Conclusion

Understanding the differences between overflow-wrap, word-break, and hyphens is key for managing text effectively. Choose the right property for your needs to ensure text stays readable and within its container. Experimenting with these properties will help you create better web designs.

Specifications and Browser Compatibility

The overflow-wrap property is defined in the CSS Text Module Level 3 specification. It’s widely supported in modern browsers like Chrome, Firefox, Edge, Safari, and Opera. For older browsers, use word-wrap as a fallback.

Browser Compatibility Table

BrowserVersionSupport
Google Chrome23Full Support
Mozilla Firefox49Full Support
Microsoft Edge18Full Support
Apple Safari7Full Support
Opera12.1Full Support

See Also

For more information, check out these resources:

These resources will help you create more sophisticated and visually appealing text layouts. Happy coding!

icons/css-4.svg CSS Blogs
CSS3 is the latest version of Cascading Style Sheets, offering advanced styling features like animations, transitions, shadows, gradients, and responsive design.
icons/logo-tid.svg

Talk with CEO

Ready to bring your web/app to life or boost your team with expert Thai developers?
Contact us today to discuss your needs, and let’s create tailored solutions to achieve your goals. We’re here to help at every step!
🖐️ Contact us
Let's keep in Touch
Thank you for your interest in Tillitsdone! Whether you have a question about our services, want to discuss a potential project, or simply want to say hello, we're here and ready to assist you.
We'll be right here with you every step of the way.
Contact Information
rick@tillitsdone.com+66824564755
Find All the Ways to Get in Touch with Tillitsdone - We're Just a Click, Call, or Message Away. We'll Be Right Here, Ready to Respond and Start a Conversation About Your Needs.
Address
9 Phahonyothin Rd, Khlong Nueng, Khlong Luang District, Pathum Thani, Bangkok Thailand
Visit Tillitsdone at Our Physical Location - We'd Love to Welcome You to Our Creative Space. We'll Be Right Here, Ready to Show You Around and Discuss Your Ideas in Person.
Social media
Connect with Tillitsdone on Various Social Platforms - Stay Updated and Engage with Our Latest Projects and Insights. We'll Be Right Here, Sharing Our Journey and Ready to Interact with You.
We anticipate your communication and look forward to discussing how we can contribute to your business's success.
We'll be here, prepared to commence this promising collaboration.
Frequently Asked Questions
Explore frequently asked questions about our products and services.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.