Lesson 5 of 8

Truncating long text

Goal

One or two lines, then an ellipsis

01

Explanation

A long title in a narrow card wraps onto two or three lines, so the cards end up at different heights and the row looks uneven. The usual fix is to cut the title short and put an ellipsis … where the cut-off part was.

Truncating a single line takes three properties together: white-space: nowrap keeps the text from wrapping onto a new line, overflow: hidden hides whatever doesn't fit, and text-overflow: ellipsis adds the ellipsis. The trap is writing only the third one and seeing nothing happen, because the ellipsis only appears on text that really is longer than its box and has its overflow hidden.

To cut text off after several lines, use -webkit-line-clamp with display: -webkit-box and -webkit-box-orient: vertical. The -webkit- prefix is old, but every browser supports it. Truncation only changes what the eye sees: the full text stays in the page for screen readers and search engines.

02

Your task

Truncate .title to a single line that ends in an ellipsis, and limit .excerpt to two lines with -webkit-line-clamp, hiding everything after them.

Checks

6 checks
  1. The title stays on one line instead of wrapping
  2. The rest of the title is hidden
  3. An ellipsis replaces the cut-off part
  4. The excerpt is no taller than two lines
  5. The excerpt is limited with -webkit-line-clamp: 2
  6. Everything after the two lines is hidden
03

Hints

0/3
Track lessons0 of 8 done

Learn to scale headings, choose the right units, and set line length and text direction, because text is what people read most in any interface.

Beginner level, 8 lessons, 1.5 hours in total.

index.html
Preview
index.html100%
–/6

Press “Run and check” to grade your code.

6 checks listed under “Your task”.

Track lessons0 of 8 done

Learn to scale headings, choose the right units, and set line length and text direction, because text is what people read most in any interface.

Beginner level, 8 lessons, 1.5 hours in total.