Emmanuel Akinfulubi

HTML and CSSPractical II

Multi-line text clamp with line-clamp in CSS

28 Truncate one line, and clamp several

fair

Cut a single line with an ellipsis, then clamp a paragraph to three lines. Count the declarations each one genuinely needs.

Stuck? Three hints, smallest first
  1. 1. One line needs three together: white-space: nowrap, overflow: hidden, text-overflow: ellipsis.
  2. 2. Multi-line needs four: display: -webkit-box, -webkit-box-orient: vertical, -webkit-line-clamp, overflow: hidden.
  3. 3. The prefixed properties are not legacy. They are the only interoperable way to clamp lines today.

index.html

<p class="one-line">One line only: this sentence is far too long for the box it has been put in.</p>

<p class="clamped">Three lines, then an ellipsis. The coach leaves Jibowu at 06:30, stops once at
  Lokoja for twenty minutes, and reaches the central terminal in Abuja by early
  evening barring roadworks on the Abaji stretch, which are frequent enough to be
  worth allowing for.</p>

style.css

.one-line {
  max-width: 260px;
  white-space: nowrap;       /* without this it wraps and never truncates */
  overflow: hidden;          /* without this it spills past the border */
  text-overflow: ellipsis;   /* without this it is cut mid-letter */
  border: 1px solid #c9cad1;
  border-radius: 6px;
  padding: 8px;
  font-size: 0.85rem;
}

.clamped {
  max-width: 260px;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;     /* the line count */
  overflow: hidden;          /* still required, even here */
  border: 1px solid #c9cad1;
  border-radius: 6px;
  padding: 8px;
  font-size: 0.85rem;
  line-height: 1.5;
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
8 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 28

const single = ['white-space:\\s*nowrap', 'overflow:\\s*hidden', 'text-overflow:\\s*ellipsis'];
const multi = ['display:\\s*-webkit-box', '-webkit-box-orient:\\s*vertical', '-webkit-line-clamp', 'overflow:\\s*hidden'];
const present = (list) => list.filter((p) => new RegExp(p).test(css)).length;

test('the single-line recipe needs three declarations', single.length, 3);
test('and all three are here', present(single), 3);
test('the clamp needs four', multi.length, 4);
test('and all four are here', present(multi), 4);
test('overflow: hidden appears in both, because both depend on it', (css.match(/overflow:\s*hidden/g) || []).length, 2);
test('both have a width to overflow', (css.match(/max-width:\s*260px/g) || []).length, 2);
test('the clamp is set to three lines', /-webkit-line-clamp:\s*3/.test(css), true);
test('and line-height is set, so three lines is a predictable height', /line-height:\s*1\.5/.test(css), true);

Output


      

Every declaration in both recipes is load-bearing, and each omission fails differently. Drop nowrap and the text wraps, so no ellipsis ever appears; drop overflow: hidden and it spills past the border instead of being cut; drop text-overflow and it is chopped mid-letter. The clamp is the same story with four parts, and the one people forget is overflow: hidden — without it the clamp does nothing at all. The -webkit- prefixes look like a smell and are not: no unprefixed equivalent has shipped, and every browser supports these.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.