CSS text wrap is a fundamental aspect of web development, allowing you to control how text flows within an element. You can use the `white-space` property to specify how text wraps.
The `white-space` property has several values, including `normal`, `nowrap`, and `pre`. The `normal` value is the default, which means text wraps as expected.
Using the `wrap` value of the `word-wrap` property is another way to control text wrap. This property can also be set to `break-word`, which breaks the word if it doesn't fit within the element.
The `overflow-wrap` property is similar to `word-wrap`, but it's more specific to wrapping text within a line.
Text Wrapping Basics
Text wrapping occurs at soft wrap opportunities, which are points where the browser can break a line of text without causing overflow. A soft wrap opportunity is typically a space between words, but it can also occur at word boundaries in languages that don't use spaces to separate words.
The overflow-wrap CSS property is used to specify whether the browser can break a line of text at otherwise disallowed points to prevent overflow. This property is also known as the word-wrap property, which is a legacy name for overflow-wrap.
To enable text wrapping, you need to set the value of the white-space CSS property to nowrap. This will allow the browser to wrap text at soft wrap opportunities.
How Content Appears in Browsers
Browsers perform content wrapping at allowed breakpoints, referred to as soft wrap opportunities. This happens at word boundaries in the absence of hyphenation, where words are bound by spaces and punctuation.
Some languages, however, do not use spaces to separate words, so content wrapping depends on the language or writing system. The value of the lang attribute specified on the HTML element is used to determine the language system.
Soft wraps occur in space characters in English texts, but the situation might be different for non-English writing systems. This means that content wrapping can behave differently depending on the language being used.
Browsers will wrap content at a soft wrap opportunity if one exists to minimize content overflow. This is the default behavior in English and other similar writing systems.
Wrapping Text
Wrapping text is a fundamental aspect of text wrapping basics. You can use the overflow-wrap CSS property to wrap text or break a word overflowing the confines of its box.
The overflow-wrap property specifies whether the browser may break at otherwise disallowed points within a line to prevent overflow when an otherwise-unbreakable string is too long to fit within the line box.
To make the overflow-wrap property work, you need to set the white-space property to allow wrapping. You can use the following values for the overflow-wrap property: normal, anywhere, and none.
Here are the possible values of the overflow-wrap property:
The word-wrap CSS property is the legacy name for the overflow-wrap property. It was originally a non-prefixed Microsoft extension and was not part of the CSS standard.
You can also use the word-break CSS property to specify soft wrap opportunities between characters. This property controls what types of letters the browser can glom together to form unbreakable "words" – causing CJK characters to behave like non-CJK text or vice versa.
The word-break property has the following values: normal, break-all, break-word, and keep-all.
Word Wrap Properties
Word Wrap Properties are essential for preventing content overflow in your layout.
You can use the word-wrap, overflow-wrap, or word-break CSS properties to wrap or break words that would otherwise overflow their container.
The word-wrap property is a legacy name for the overflow-wrap CSS property, and most recent versions of popular web browsers have implemented the overflow-wrap property.
To use overflow-wrap, you need to set its value to allow wrapping, which is usually done by setting the white-space property to a value that enables wrapping.
The overflow-wrap property specifies whether the browser may break at otherwise disallowed points within a line to prevent overflow when an otherwise-unbreakable string is too long to fit within the line box.
You can use the global values inherit, initial, revert, and unset with overflow-wrap, but the most commonly used values are anywhere, break-word, and normal.
The anywhere value allows the browser to wrap the overflowing word to its line without overflowing, while the break-word value breaks the word at the point where the overflow would otherwise occur.
The normal value breaks words at the default rules, such as a blank space or a hyphen, and is similar to word-break: break-all.
The break-word value has decent coverage among desktop browsers but is unknown in some mobile browsers, making it safer to use the legacy word-wrap: break-word instead.
Word Break Properties
The word break properties in CSS are a lifesaver when it comes to managing text overflow. You can use the word-break property to specify soft wrap opportunities between characters.
The word-break property has several keyword values, including normal, break-all, keep-all, and break-word. The break-word value is a legacy property that was removed, but browsers still support it for compatibility reasons.
Word-break: normal applies the default word breaking rules. Word-break: break-all will ruthlessly break the overflowing word between two characters, even if placing it on its line would negate the need for word break.
The overflow-wrap property, on the other hand, determines whether the browser should break an otherwise unbreakable string to avoid it from overflowing its parent's width. It has the following possible keyword values: normal, anywhere, and break-word.
The break-word value in overflow-wrap behaves the same as anywhere, but it doesn't consider soft-wrap opportunities when calculating min-content intrinsic sizes.
Here's a comparison of the keyword values for both properties:
In some cases, overflow-wrap will wrap the entire overflowing word to its line if it can fit in a single line without overflowing its container. Word-break, however, will break the overflowing word between two characters even if placing it on its line would eliminate the need for word break.
Sources
- https://blog.logrocket.com/guide-css-word-wrap-overflow-wrap-word-break/
- https://unused-css.com/blog/text-wrapping/
- https://savvy.co.il/en/blog/css/css-text-wrap-pretty-balance/
- https://cloudinary.com/blog/guest_post/handle-text-wrap-around-images-using-html-and-css/
- https://blog.stephaniestimac.com/posts/2023/10/css-text-wrap/
Featured Images: pexels.com