🧡 CSS Tutorial: text-wrap

πŸ“Œ Introduction

The text-wrap property in CSS controls how text should wrap inside an element. This is essential for responsive design, preventing long text from breaking layout, and ensuring paragraphs remain readable across different devices. Introduced as part of the **CSS Text Module Level 4**, text-wrap is a modern and improved way to manage text wrapping. ✨

🎯 Why Use text-wrap?

  • To prevent awkward text breaks in responsive layouts πŸ“±
  • To ensure long words wrap correctly instead of overflowing πŸ”€
  • To maintain readability in paragraphs and UI labels πŸ“–
  • To give designers better control than the older white-space hacks 🎨

✨ Syntax

Basic Syntax

selector {
  text-wrap: wrap | nowrap | balance | pretty;
}

Note

text-wrap is newer and more flexible than older properties likewhite-space and word-wrap.

🧩 Available Values

1. wrap (default)

Allows text to wrap normally at word boundaries. This is the default for most block elements like <p>.

2. nowrap 🚫

Prevents text from wrapping at all β€” the text will stay in one line and may overflow.

nowrap Example

.nowrap {
  text-wrap: nowrap;
}

3. balance βš–οΈ

Tries to balance text across lines for aesthetic layout, especially for headings. Great for hero sections or multi-line titles.

balance Example

h1 {
  text-wrap: balance;
}

4. pretty πŸ’…

Tries to avoid β€œugly” breaks by giving lines a more natural and smooth wrap. Ideal for long paragraphs and blog content.

pretty Example

p {
  text-wrap: pretty;
}

πŸ”₯ Practical Examples

1. Prevent Wrapping for Labels

Label Example

.label {
  text-wrap: nowrap;
  background: #eee;
  padding: 5px 10px;
}

2. Balanced Headings

Balanced Heading

h2 {
  text-wrap: balance;
  font-size: 2rem;
  max-width: 400px;
}

3. Pretty Wrapping for Articles

Pretty Paragraph

article p {
  text-wrap: pretty;
  line-height: 1.6;
}

πŸ“Š Comparison Table

ValueEffect
wrapNormal wrapping at word boundaries
nowrapNo wrapping; content stays in a single line
balanceEvenly balances text across lines
prettyMakes wrapping aesthetically pleasing

πŸ–ΌοΈ Visual Example

>>β€œBeautiful text isn’t just about fonts β€” it’s about how words flow on the page.” ✨

πŸŽ‰ Conclusion

The text-wrap property is a modern and flexible way to control text wrapping in CSS. Whether you’re designing responsive headings, clean paragraphs, or UI labels, this property ensures your text flows naturally across different devices. Start using text-wrap to make your text more readable, elegant, and visually balanced! πŸš€