# Text Styling & Wrapping

> Shape how text looks and flows, from underlines to perfectly balanced headlines.

Subject: CSS | Lesson 12 of 44 | 4 min read
Interactive lesson: https://codewithanimation.in/css/text-styling-and-wrapping
Previous: [Font Sizing & Spacing](https://codewithanimation.in/css/font-sizing-and-spacing.md) | Next: [Color Formats & OKLCH](https://codewithanimation.in/css/color-formats-and-oklch.md)

## What it is

Text styling in CSS goes beyond picking a font and a size. It covers how text is decorated (`text-decoration`), capitalized (`text-transform`), aligned (`text-align`), and how it wraps at the end of a line (`text-wrap`).

While older properties like `text-align` have been around forever, modern CSS has introduced powerful new tools like `text-wrap: balance` to give developers layout-engine-level control over typography.

## Why it matters

Good typography shapes the reading experience. A headline that wraps awkwardly with one word on the second line looks unprofessional.

In the past, developers tried to fix wrapping issues by hardcoding `<br>` tags into HTML. But when the screen size changed on a mobile device, those hard breaks caused even worse layout issues. CSS wrapping properties solve this responsively.

## How it works

You apply these properties directly to the text container.

`text-transform: uppercase` changes the casing visually, leaving the original HTML text intact (which is better for screen readers than typing in ALL CAPS).

`text-decoration` adds lines (underline, line-through) and can be styled with color and thickness.

The modern `text-wrap` property gives the browser hints on how to break lines: `balance` makes lines roughly equal in width (ideal for headlines), while `pretty` prevents a single word from sitting alone on the last line (ideal for paragraphs).

## Common mistake: The `<br>` trap

The most common mistake when trying to fix an ugly headline wrap is inserting a `<br>` tag in the HTML. On a desktop monitor, it looks perfect. But on a mobile phone, the text naturally wraps earlier, and then your `<br>` forces a *second* break, leaving an awkward jagged edge.

Instead, use `text-wrap: balance` on headings, and let the browser calculate the best break points for every screen size.

## Tip: Balance vs Pretty

Because `text-wrap: balance` is computationally expensive (the browser has to calculate multiple layouts to find the most even one), it is limited to a maximum number of lines (usually 6).

Use `text-wrap: balance` for short headlines, and use `text-wrap: pretty` for long body paragraphs to prevent widows (lonely words at the end).

## Try it: Try it

See how modern text wrapping and styling shape the reading experience.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/text-styling-and-wrapping

## Check yourself

### Question 1

How do you prevent a headline from having one very long line and one very short line?

- A. text-wrap: balance
- B. text-align: center
- C. Insert a <br> tag

Correct answer: A

`text-wrap: balance` tells the browser to calculate the width of the text and wrap it so that all lines are roughly the same width. It is the modern, responsive solution for headline typography.

- Why not B: Center alignment centers the text, but the first line might still be much longer than the second.
- Why not C: A hard break might look good on your screen, but it will cause awkward wrapping on smaller mobile devices.

### Question 2

Which property forces text to appear in ALL CAPS regardless of how it was typed in the HTML?

- A. text-transform: uppercase
- B. font-variant: caps
- C. text-decoration: uppercase

Correct answer: A

`text-transform: uppercase` changes the visual casing of the text. This is preferred over typing ALL CAPS in HTML because screen readers often read capitalized words letter-by-letter as acronyms.

- Why not B: font-variant is used for typographic features like small-caps, not for forcing standard uppercase.
- Why not C: text-decoration is for underlines, overlines, and line-throughs, not casing.

### Question 3

What is the modern CSS way to prevent a single word from sitting alone on the last line of a paragraph (a "widow")?

- A. text-wrap: pretty
- B. white-space: nowrap
- C. text-align: justify

Correct answer: A

`text-wrap: pretty` instructs the browser to evaluate the end of a text block and adjust the wrapping to avoid leaving a single word on the final line, improving the block's visual rhythm.

- Why not B: nowrap prevents the text from wrapping at all, which will cause it to overflow its container.
- Why not C: justify stretches the spacing between words so lines touch both edges, but it does not prevent widows on the last line.

## Remember this

- Use `text-transform` for casing instead of typing in all caps.
- `text-wrap: balance` makes headline lines equal in width.
- `text-wrap: pretty` prevents single-word orphans in paragraphs.
- Never use `<br>` tags just to make a headline look better on your specific screen size.
