CSS text-wrap Property

The CSS text-wrap property controls whether text wraps onto multiple lines and how the browser chooses those line breaks.

It is a shorthand for text-wrap-mode and text-wrap-style. It can prevent wrapping, balance heading lines, improve paragraph endings, or preserve stable line breaks while content is edited. Some advanced values have limited browser support.

text-wrap Syntax

The property accepts wrapping-mode keywords, wrapping-style keywords, and supported combinations of the two.

Basic Syntax

.example {
  text-wrap: balance;
}

Available Values

.example {
  text-wrap: wrap;
}
.example {
  text-wrap: nowrap;
}
.example {
  text-wrap: balance;
}
.example {
  text-wrap: pretty;
}
.example {
  text-wrap: stable;
}
.example {
  text-wrap: avoid-orphans;
}

Combined Values

.example {
  text-wrap: wrap balance;
}
.example {
  text-wrap: wrap pretty;
}
.example {
  text-wrap: wrap stable;
}
.example {
  text-wrap: wrap avoid-orphans;
}

Combined declarations specify both the wrapping mode and the wrapping style. The avoid-orphans value is a newer addition and may not be recognized by all browsers.

CSS-Wide Keywords

.example {
  text-wrap: initial;
}
.example {
  text-wrap: inherit;
}
.example {
  text-wrap: unset;
}
.example {
  text-wrap: revert;
}
.example {
  text-wrap: revert-layer;
}

Invalid Examples

.example {
  text-wrap: normal;
}
.example {
  text-wrap: none;
}
.example {
  text-wrap: 20px;
}
.example {
  text-wrap: balance pretty;
}
.example {
  text-wrap: wrap nowrap;
}

The property does not accept arbitrary lengths, normal, none, or conflicting values from the same longhand property.

Property Values

Value Description
wrap Allows text to wrap at permitted line-breaking opportunities.
nowrap Prevents ordinary soft wrapping within the text.
balance Attempts to balance the lengths of lines in a text block.
pretty Favors improved line-breaking quality, potentially avoiding undesirable short final lines.
stable Uses line-breaking behavior intended to reduce changes in earlier lines while text is edited.
avoid-orphans Requests line breaking that avoids leaving a very short final line. Browser support is limited.
initial Resets the shorthand to its initial longhand values.
inherit Inherits the parent's computed wrapping settings.
unset Inherits the wrapping settings because both longhand properties are inherited.
revert Reverts the wrapping settings according to the CSS cascade.
revert-layer Reverts the wrapping settings according to the previous cascade layer.

Property Characteristics

Characteristic Value
Property type Shorthand
Initial value wrap auto through its longhands
Inherited Yes, through both longhands
Applies to Text-containing elements
Longhand properties text-wrap-mode and text-wrap-style
Percentages Not accepted
Animation type Discrete
Specification CSS Text Module Level 4

Note: The initial wrapping style is auto, while the initial wrapping mode is wrap. The auto keyword belongs to text-wrap-style, although it is also valid within the shorthand.

How It Works

Browsers normally break text into lines according to the available width, whitespace rules, language, and permitted line-breaking opportunities. The text-wrap property changes whether those breaks are allowed and how they are selected.

Step 1: Create a Text Container

<p class="wrapping-text">CSS provides several ways to control how text wraps across multiple lines.</p>

Step 2: Set a Width

.wrapping-text {
  max-width: 300px;
}

The container width determines how much text can fit on each line.

Step 3: Choose a Wrapping Style

.wrapping-text {
  max-width: 300px;
  text-wrap: pretty;
}

The browser attempts to choose more visually pleasing line breaks while respecting the available width.

Step 4: Compare With Balanced Text

.wrapping-text {
  max-width: 300px;
  text-wrap: balance;
}

The browser attempts to distribute the text more evenly across lines.

Important Consideration

Wrapping styles do not guarantee a specific number of words per line. Results depend on font metrics, container width, browser implementation, and language.

The wrap Value

The wrap value allows text to break onto additional lines at permitted soft-wrap opportunities.

Basic Example

.example {
  text-wrap: wrap;
}

HTML Example

<p class="normal-wrap">This sentence wraps naturally when the available width becomes too small.</p>

CSS Example

.normal-wrap {
  max-width: 280px;
  text-wrap: wrap;
}
Browser Result

This sentence wraps naturally when the available width becomes too small.

Default Wrapping

Ordinary text typically wraps by default. The wrap keyword is useful when overriding a previously applied nowrap setting.

Restoring Wrapping

.navigation-label {
  text-wrap: nowrap;
}
.navigation-label.allow-wrap {
  text-wrap: wrap;
}

When to Use wrap

Use wrap for body text, descriptions, and other content that should adjust naturally to the available width.

The nowrap Value

The nowrap value prevents ordinary soft line wrapping, keeping text on a single line unless an explicit or otherwise forced break occurs.

Basic Example

.example {
  text-wrap: nowrap;
}

HTML Example

<p class="no-wrap">This text remains on one line even when the container is narrow.</p>

CSS Example

.no-wrap {
  width: 280px;
  text-wrap: nowrap;
}
Browser Result

This text remains on one line even when the container is narrow.

Navigation Labels

.navigation-link {
  text-wrap: nowrap;
}

Preventing wrapping can keep short navigation labels together.

Button Labels

.action-button {
  text-wrap: nowrap;
}

Overflow Considerations

Long text can overflow a narrow container when wrapping is disabled.

When to Avoid nowrap

Avoid using nowrap for long paragraphs or other content that must adapt to small screens.

The balance Value

The balance value attempts to make the lines of a text block more equal in length.

It is particularly useful for short headings, titles, and introductory text. Browsers may limit the number of lines considered for balancing, so it is not intended for lengthy paragraphs.

Basic Example

.example {
  text-wrap: balance;
}

Heading Example

<h2 class="balanced-heading">Create Better Looking Headings With CSS</h2>

CSS Example

.balanced-heading {
  max-width: 320px;
  text-wrap: balance;
}

Comparison

Browser Result

Normal Wrapping

Create Better Looking Headings With CSS

Balanced Wrapping

Create Better Looking Headings With CSS

How Balancing Works

The browser evaluates possible line breaks and attempts to reduce uneven line lengths. It may change where words wrap without changing the text or its font size.

Performance Considerations

Balancing requires additional line-breaking calculations. Browsers may impose limits to avoid excessive processing.

When to Use balance

Use balance for headings, short quotations, card titles, and other compact blocks of text.

The pretty Value

The pretty value asks the browser to favor higher-quality line breaks, potentially reducing undesirable short final lines and other awkward wrapping.

Basic Example

.example {
  text-wrap: pretty;
}

Paragraph Example

<p class="pretty-paragraph">Thoughtful line wrapping can make longer passages easier to read and improve the overall appearance of a page.</p>

CSS Example

.pretty-paragraph {
  max-width: 420px;
  text-wrap: pretty;
}

Short Final Lines

A very short final line is sometimes called a typographic orphan. The pretty value can help browsers avoid such endings when suitable line breaks are available.

Comparison With balance

Value Primary Goal
balance Make lines more similar in length.
pretty Improve overall line-breaking quality.

Browser Differences

Browsers may use different algorithms for pretty wrapping. The property does not guarantee identical line breaks across browsers.

When to Use pretty

Use pretty for paragraphs, article introductions, and other text where polished line endings are desirable.

The stable Value

The stable value is intended to keep earlier line breaks relatively stable while text is being edited.

Basic Example

.example {
  text-wrap: stable;
}

Editable Text Example

<div class="editable-text" contenteditable="true">Edit this text and observe how the lines wrap while typing.</div>

CSS Example

.editable-text {
  max-width: 360px;
  padding: 12px;
  border: 1px solid #cbd5e1;
  text-wrap: stable;
}

How Stable Wrapping Works

Stable wrapping is designed to reduce unnecessary changes to earlier line breaks when text is modified. This can be useful in editing interfaces.

When to Use stable

Use stable in editable content, text composition areas, and applications where predictable line wrapping improves the editing experience.

Important Consideration

The value does not prevent all line changes. Text edits, font changes, and container resizing can still affect wrapping.

The avoid-orphans Value

The avoid-orphans value requests line breaking that avoids leaving a very short final line.

It is a newer wrapping-style value, and browser support is limited. It should be treated as a progressive enhancement rather than a requirement for readable content.

Basic Example

.example {
  text-wrap: avoid-orphans;
}

Paragraph Example

.article-introduction {
  max-width: 500px;
  text-wrap: avoid-orphans;
}

Fallback Example

.article-introduction {
  text-wrap: pretty;
}
@supports (text-wrap: avoid-orphans) {
  .article-introduction {
    text-wrap: avoid-orphans;
  }
}

Comparison With pretty

The pretty value requests general improvements to line-breaking quality. The avoid-orphans value focuses more specifically on undesirable short final lines.

When to Use avoid-orphans

Consider it for editorial layouts where avoiding isolated words or very short final lines is important, provided the target browsers support the value.

Browser Support

Because implementation is still evolving, check current browser compatibility before relying on this value.

Shorthand and Longhand Properties

The text-wrap shorthand controls two longhand properties: text-wrap-mode and text-wrap-style.

text-wrap-mode

.example {
  text-wrap-mode: wrap;
}

Controls whether ordinary soft wrapping is allowed.

text-wrap-style

.example {
  text-wrap-style: balance;
}

Controls how the browser chooses line breaks when wrapping is allowed.

Equivalent Declarations

.example {
  text-wrap: wrap balance;
}

This is equivalent to:

.example {
  text-wrap-mode: wrap;
  text-wrap-style: balance;
}

Using a Single Keyword

.example {
  text-wrap: balance;
}

When only a wrapping-style keyword is supplied, the shorthand sets text-wrap-mode to its initial value of wrap.

Using nowrap

.example {
  text-wrap: nowrap;
}

This sets the wrapping mode to nowrap and resets the wrapping style to its initial value of auto.

Shorthand Reset Behavior

Like other CSS shorthands, text-wrap resets any omitted longhand components to their initial values.

Important Consideration

Declaration order matters when combining the shorthand with its longhand properties.

.example {
  text-wrap-style: pretty;
  text-wrap: nowrap;
}

The final shorthand resets the wrapping style to auto.

Relationship With white-space

The white-space property controls whitespace collapsing and wrapping behavior. Modern CSS separates these concerns into more specific properties.

Traditional white-space Declaration

.example {
  white-space: nowrap;
}

Using text-wrap

.example {
  text-wrap: nowrap;
}

Both can prevent ordinary soft wrapping, although white-space also participates in whitespace handling.

Preserving Whitespace

.example {
  white-space-collapse: preserve;
  text-wrap-mode: wrap;
}

This combination preserves whitespace while allowing wrapping according to the applicable text-breaking rules.

Comparison

Property Purpose
white-space Shorthand for whitespace collapsing and wrapping behavior.
white-space-collapse Controls how whitespace is collapsed or preserved.
text-wrap Controls wrapping mode and line-breaking style.

Important Consideration

When white-space and text-wrap both affect wrapping, the cascade and declaration order determine which settings apply.

Several CSS properties work alongside text-wrap to control text layout and line breaking.

overflow-wrap

.example {
  overflow-wrap: anywhere;
}

Allows long words or unbroken strings to break when necessary to avoid overflow.

word-break

.example {
  word-break: break-all;
}

Controls additional opportunities for breaking words.

hyphens

.example {
  hyphens: auto;
}

Allows automatic hyphenation when supported and appropriate language information is available.

white-space

.example {
  white-space: pre-wrap;
}

Controls whitespace handling and wrapping behavior.

line-height

.example {
  line-height: 1.6;
}

Controls the height of lines rather than where text wraps.

max-width

.example {
  max-width: 65ch;
}

Limits the text container width, influencing the available space for each line.

Combining Properties

.article-text {
  max-width: 65ch;
  line-height: 1.6;
  overflow-wrap: break-word;
  text-wrap: pretty;
}

This combination provides a readable line length, comfortable line spacing, protection against long unbroken content, and improved line-breaking behavior where supported.

Inheritance

The text-wrap shorthand controls two inherited longhand properties. Descendant elements generally inherit their parent's wrapping settings unless another declaration overrides them.

Parent Element Example

.content {
  text-wrap: pretty;
}

Child Element Override

.content {
  text-wrap: pretty;
}
.content h2 {
  text-wrap: balance;
}

Paragraphs can use pretty wrapping while headings use balanced wrapping.

Explicit Inheritance

.child {
  text-wrap: inherit;
}

Restore Initial Values

.child {
  text-wrap: initial;
}

This restores the initial longhand settings, wrap and auto.

Using unset

.child {
  text-wrap: unset;
}

Because both longhand properties are inherited, unset inherits their values from the parent.

Important Consideration

Inherited wrapping settings do not guarantee identical line breaks. Different widths, fonts, and font sizes can produce different results in descendant elements.

Practical Examples

Balanced Page Heading

.page-heading {
  max-width: 700px;
  text-wrap: balance;
}

Balanced Card Title

.card-title {
  max-width: 28ch;
  text-wrap: balance;
}

Pretty Paragraph Wrapping

.article-paragraph {
  max-width: 65ch;
  text-wrap: pretty;
}

Article Introduction

.article-introduction {
  font-size: 1.2rem;
  line-height: 1.6;
  text-wrap: pretty;
}

Prevent Button Text Wrapping

.action-button {
  text-wrap: nowrap;
}

Navigation Labels

.navigation-link {
  text-wrap: nowrap;
}

Allow Responsive Wrapping

.responsive-label {
  text-wrap: wrap;
}

Editable Content

.editable-content {
  text-wrap: stable;
}

Long URLs and Text

.long-content {
  text-wrap: wrap;
  overflow-wrap: anywhere;
}

Responsive Heading

.responsive-heading {
  max-width: 32ch;
  text-wrap: balance;
}
@media (max-width: 600px) {
  .responsive-heading {
    max-width: 100%;
  }
}

Progressive Enhancement

.article-text {
  text-wrap: wrap;
}
@supports (text-wrap: pretty) {
  .article-text {
    text-wrap: pretty;
  }
}

Avoiding Short Final Lines

.editorial-text {
  text-wrap: pretty;
}
@supports (text-wrap: avoid-orphans) {
  .editorial-text {
    text-wrap: avoid-orphans;
  }
}

text-wrap Example

This example compares normal wrapping, balanced wrapping, pretty wrapping, and no wrapping. The samples use the same container width so differences in line-breaking behavior are easier to observe.

HTML Example

<div class="text-wrap-demo">
  <div class="wrap-sample">
    <strong>wrap</strong>
    <p class="wrap-normal">Create Better Looking Text Layouts With Modern CSS Wrapping</p>
  </div>
  <div class="wrap-sample">
    <strong>balance</strong>
    <p class="wrap-balance">Create Better Looking Text Layouts With Modern CSS Wrapping</p>
  </div>
  <div class="wrap-sample">
    <strong>pretty</strong>
    <p class="wrap-pretty">Create Better Looking Text Layouts With Modern CSS Wrapping</p>
  </div>
  <div class="wrap-sample">
    <strong>nowrap</strong>
    <p class="wrap-nowrap">Create Better Looking Text Layouts With Modern CSS Wrapping</p>
  </div>
</div>

CSS Example

.text-wrap-demo {
  display: grid;
  gap: 12px;
}
.text-wrap-demo .wrap-sample {
  padding: 16px;
  border: 1px solid #cbd5e1;
  background: #f8fafc;
  overflow-x: auto;
}
.text-wrap-demo strong {
  display: block;
  margin-bottom: 10px;
  color: #334155;
  font-size: 14px;
}
.text-wrap-demo p {
  max-width: 320px;
  margin: 0;
  color: #1f2937;
  font-size: 21px;
  line-height: 1.4;
}
.text-wrap-demo .wrap-normal {
  text-wrap: wrap;
}
.text-wrap-demo .wrap-balance {
  text-wrap: balance;
}
.text-wrap-demo .wrap-pretty {
  text-wrap: pretty;
}
.text-wrap-demo .wrap-nowrap {
  text-wrap: nowrap;
}
Browser Result
wrap

Create Better Looking Text Layouts With Modern CSS Wrapping

balance

Create Better Looking Text Layouts With Modern CSS Wrapping

pretty

Create Better Looking Text Layouts With Modern CSS Wrapping

nowrap

Create Better Looking Text Layouts With Modern CSS Wrapping

Understanding the Results

The wrap sample uses ordinary line wrapping. The balance sample attempts to make the lines more even, while pretty favors improved line breaks. The nowrap sample stays on one line and may require horizontal scrolling.

Browser Differences

The pretty and balance samples may look similar in some browsers or at certain widths. Their algorithms and support differ.

Experiment With Container Width

.text-wrap-demo p {
  max-width: 240px;
}

Reducing the width creates additional line-breaking opportunities and may make the differences between wrapping styles more noticeable.

Accessibility Considerations

Text wrapping plays an important role in responsive design, readability, and accessibility.

Allow Text to Reflow

Text should generally wrap when the available width becomes smaller, including when users enlarge text or view a page on a narrow screen.

Avoid Unnecessary nowrap

Applying nowrap to long text can create horizontal overflow and make content difficult to read.

Use Readable Line Lengths

Combine appropriate wrapping with a reasonable maximum line width.

.article-content {
  max-width: 65ch;
  line-height: 1.6;
  text-wrap: pretty;
}

Support Text Enlargement

Test layouts at increased text sizes and browser zoom levels to ensure that content remains readable.

Preserve Meaningful Line Breaks

Do not rely on visual wrapping to communicate essential structure or meaning. Use appropriate HTML elements for paragraphs, headings, and lists.

Avoid Excessively Narrow Containers

Very narrow containers can produce awkward wrapping even when balancing or pretty wrapping is enabled.

Test Different Languages

Line-breaking rules vary by language and writing system. Test multilingual content with appropriate language markup.

Maintain Readable Spacing

Use suitable line height and spacing so that wrapped lines remain easy to follow.

Provide Fallbacks

Advanced wrapping styles should enhance the presentation without being required for the content to remain usable.

Common Mistakes

Using normal Instead of wrap

.example {
  text-wrap: normal;
}

The normal keyword is not valid for this property. Use wrap.

Using none Instead of nowrap

.example {
  text-wrap: none;
}

Use nowrap to prevent ordinary soft wrapping.

Using Length Values

.example {
  text-wrap: 300px;
}

The property does not control container width. Use width or max-width.

Combining Conflicting Styles

.example {
  text-wrap: balance pretty;
}

Both values belong to text-wrap-style and cannot be combined.

Combining Conflicting Modes

.example {
  text-wrap: wrap nowrap;
}

These are conflicting values for text-wrap-mode.

Expecting balance to Work on Long Paragraphs

Browsers may limit balancing to relatively short text blocks.

Expecting Identical Line Breaks

Font metrics, container width, and browser algorithms affect wrapping results.

Using nowrap on Responsive Paragraphs

Long unwrapped text can overflow narrow screens.

Assuming pretty Is Universally Supported

Support and implementation details vary across browsers.

Assuming avoid-orphans Is Widely Available

The value is newer and requires compatibility testing.

Confusing text-wrap With overflow-wrap

The text-wrap property controls wrapping mode and style. The overflow-wrap property provides additional break opportunities for otherwise unbreakable content.

Forgetting Shorthand Reset Behavior

Declaring text-wrap can reset both longhand properties, including any longhand value previously specified.

Ignoring Inheritance

Wrapping settings can be inherited by nested elements and may need to be overridden for headings, buttons, or navigation labels.

Important Considerations

Keep the following points in mind when using text-wrap:

  • The property controls whether text wraps and how line breaks are selected.
  • It is a shorthand for text-wrap-mode and text-wrap-style.
  • The initial longhand values are wrap and auto.
  • Both longhand properties are inherited.
  • The wrap value allows ordinary soft wrapping.
  • The nowrap value prevents ordinary soft wrapping.
  • The balance value attempts to balance line lengths.
  • The pretty value favors improved line-breaking quality.
  • The stable value is intended to reduce unnecessary line changes during editing.
  • The avoid-orphans value targets short final lines and has limited support.
  • Combined values can specify both wrapping mode and style.
  • Conflicting values from the same longhand cannot be combined.
  • Shorthand declarations reset omitted longhand components.
  • Balancing is most useful for short text blocks.
  • Pretty wrapping is often useful for paragraphs.
  • Stable wrapping can be useful for editable content.
  • Disabling wrapping may cause horizontal overflow.
  • The property does not control container width.
  • Font metrics affect where lines break.
  • Language-specific line-breaking rules still apply.
  • Advanced wrapping styles may produce different results across browsers.
  • Use overflow-wrap for long unbroken strings when necessary.
  • Use hyphens when automatic hyphenation is appropriate.
  • Test wrapping at different screen widths and zoom levels.
  • Use advanced values as progressive enhancements when compatibility is uncertain.

Browser Support

The CSS text-wrap property has varying browser support depending on the value used. Basic wrapping modes are broadly supported in modern browsers, while advanced line-breaking styles have different implementation histories.

The balance and pretty values are available in many modern browser versions. Support for stable, avoid-orphans, and specific combinations should be verified for the target browsers.

For current compatibility information, visit Can I Use? and search for text-wrap.

Basic Declaration

.example {
  text-wrap: balance;
}

Feature Detection

@supports (text-wrap: balance) {
  .heading {
    text-wrap: balance;
  }
}

Checking pretty Support

@supports (text-wrap: pretty) {
  .article-text {
    text-wrap: pretty;
  }
}

Checking avoid-orphans Support

@supports (text-wrap: avoid-orphans) {
  .article-text {
    text-wrap: avoid-orphans;
  }
}

Fallback Behavior

Browsers that do not recognize a value ignore that declaration and retain the previously applicable wrapping settings.

Testing Recommendations

Test advanced wrapping styles with the actual fonts, text content, and container widths used on the website. Feature queries confirm syntax recognition but do not guarantee identical line-breaking results.

Specifications

The text-wrap property is defined in the CSS Text Module Level 4 specification.

CSS Text Module Level 4 - text-wrap Property

The specification defines the wrapping shorthand, its longhand properties, and the available wrapping modes and styles.

For additional documentation and examples, consult MDN Web Docs.

MDN Web Docs: text-wrap