Skip to main content

CSS Text Effects

Overflowing text, word wrapping, line breaking rules and writing modes all have dedicated CSS properties.

This chapter covers four of them:

  • text-overflow — what happens to content that overflows
  • word-wrap — lets a long word split and continue on the next line
  • word-break — the rules for breaking lines
  • writing-mode — horizontal or vertical layout for lines of text

CSS text-overflow Property

When content overflows and stays hidden, the CSS text-overflow property decides how the user is told about it: the text is either clipped or ends in an ellipsis (...).

It has no effect unless two other declarations are present:

  • white-space: nowrap;
  • overflow: hidden;

Example

Two treatments of overflowed text: clipped in the first paragraph, ended with an ellipsis (...) in the second.

Prompt
Show two paragraphs with CSS text-overflow. The first paragraph uses text-overflow: clip and the second uses text-overflow: ellipsis. Both have a 200px width, 1px solid black border, white-space: nowrap, and overflow: hidden. Use a long text that overflows the container.
Output: complete HTML page

Hover to show overflow

Here the overflowed content comes back into view the moment you hover over the element.

Prompt
Show a paragraph with text-overflow: ellipsis, white-space: nowrap, overflow: hidden, and a 200px width with a black border. On hover, set overflow to visible so the full text is shown.
Output: complete HTML page

CSS word-wrap Property

A word too long for its area simply sticks out of it. The CSS word-wrap property stops that by letting long words break and continue on the next line.

In other words, it forces the text to wrap, even when the split falls in the middle of a word.

Example

A long word breaks here and continues on the next line.

Prompt
Show a paragraph containing a very long unbroken word (like "thisisaveryveryveryveryveryverylongword") with word-wrap: break-word applied so the long word breaks and wraps onto the next line. Use an 11em wide container.
Output: complete HTML page

CSS word-break Property

What happens to words that reach the end of a line is up to the CSS word-break property.

It accepts one of three values:

  • normal — the language's default line breaking rules apply
  • break-all — to prevent overflow, a word may break at any character
  • keep-all — words are kept from breaking

Example

Compare word-break: normal in the first paragraph with word-break: break-all in the second.

Prompt
Show two paragraphs demonstrating word-break values. The first uses word-break: normal with hyphenated text, the second uses word-break: break-all. Both should be in a narrow container with a border to show how the text breaks differently.
Output: complete HTML page

CSS writing-mode Property

Lines of text can run horizontally or vertically, and the CSS writing-mode property chooses between the two.

It accepts one of three values:

  • horizontal-tb — the default: text runs horizontally from left to right, and lines stack vertically from top to bottom
  • vertical-rl — text runs vertically from top to bottom, and lines stack horizontally from right to left
  • vertical-lr — text runs vertically from top to bottom, and lines stack horizontally from left to right

Example

Several writing modes at work, covering both horizontal and vertical text layout.

Prompt
Show examples of CSS writing-mode. Include a paragraph with horizontal-tb (default), a paragraph containing a span with writing-mode: vertical-rl, and a third paragraph entirely set to writing-mode: vertical-rl. Add borders to visualize each.
Output: complete HTML page

CSS Text Effect Properties

The CSS text effect properties at a glance:

  • text-justify — the alignment and spacing of justified text
  • text-overflow — how the user is told about overflowed content that stays hidden
  • word-break — the line breaking rules applied to words
  • word-wrap — permits long words to break and continue on the next line
  • writing-mode — whether lines of text run horizontally or vertically