- Services
- Case Studies
- Technologies
- NextJs development
- Flutter development
- NodeJs development
- ReactJs development
- About
- Contact
- Tools
- Blogs
- FAQ
CSS Overflow-wrap Control Text Wrapping Easily
Prevent overflow with options like normal, anywhere, and break-word.
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 toanywhere, 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:
| Property | Initial Value | Applies To | Inherited | Computed Value | Animation Type |
|---|---|---|---|---|---|
overflow-wrap | normal | text elements | Yes | as specified | discrete |
Formal Syntax:
overflow-wrap = normal | break-word | anywhereExamples
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 toanywhere, 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 thelangattribute.
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
| Browser | Version | Support |
|---|---|---|
| Google Chrome | 23 | Full Support |
| Mozilla Firefox | 49 | Full Support |
| Microsoft Edge | 18 | Full Support |
| Apple Safari | 7 | Full Support |
| Opera | 12.1 | Full 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!
สร้างเว็บไซต์ 1 เว็บ ต้องใช้งบเท่าไหร่? เจาะลึกทุกองค์ประกอบ website development cost อยากสร้างเว็บไซต์แต่ไม่มั่นใจในเรื่องของงบประมาณ อ่านสรุปเจาะลึกตั้งแต่ดีไซน์, ฟังก์ชัน และการดูแล พร้อมตัวอย่างงบจริงจาก Till it’s done ที่แผนชัด งบไม่บานปลายแน่นอน
Next.js สอน 14 ขั้นตอนเบื้องต้น: สร้างโปรเจกต์แรกใน 30 นาที เริ่มต้นกับ Next.js ใน 14 ขั้นตอนเพียงแค่ 30 นาที พร้อม SSR/SSG และ API Routes ด้วยตัวอย่างโค้ดง่าย ๆ อ่านต่อเพื่อสร้างโปรเจ็กต์แรกได้ทันทีที่นี่
วิธีสมัคร Apple Developer Account เพื่อนำแอปขึ้น App Store ทีละขั้นตอน อยากปล่อยแอปบน App Store ระดับโลก มาอ่านคู่มือสมัคร Apple Developer Account พร้อมเคล็ดลับ TestFlight และวิธีอัปโหลดที่ง่ายในบทความเดียวนี้ได้เลย
TypeScript Interface คืออะไร? อธิบายพร้อมวิธีใช้และข้อแตกต่างจาก Type เรียนรู้วิธีใช้ TypeScript Interface เพื่อสร้างโครงสร้างข้อมูลที่ปลอดภัยและเข้าใจง่าย พร้อมเปรียบเทียบข้อดีข้อแตกต่างกับ Type ที่คุณต้องรู้ ถูกรวมเอาไว้ในบทความนี้แล้ว
Material-UI (MUI) คืออะไร อยากสร้าง UI สวยงามและเป็นมืออาชีพในเวลาอันรวดเร็วใช่ไหม มาทำความรู้จักกับ Material-UI (MUI) ที่ช่วยให้คุณพัฒนาแอปพลิเคชันบน React ได้ง่ายและดูดีในทุกอุปกรณ์
เปรียบเทียบ 3 วิธีติดตั้ง install node js บน Ubuntu: NVM vs NodeSource vs Official Repo แบบไหนดีที่สุด? เรียนรู้วิธีติดตั้ง Node.js บน Ubuntu ด้วย NVM, NodeSource หรือ Official Repo เลือกวิธีที่เหมาะกับความต้องการของคุณ พร้อมเปรียบเทียบ เพื่อการพัฒนาที่มีประสิทธิภาพ! Talk with CEO
We'll be right here with you every step of the way.
We'll be here, prepared to commence this promising collaboration.
Whether you're curious about features, warranties, or shopping policies, we provide comprehensive answers to assist you.