QA
Intermediate LevelCSS & Modern Layouts

How do you truncate text to a specific number of lines with CSS?

❓ Interview Prompt: Explain single-line ellipsis, multi-line truncation with -webkit-line-clamp, and the standard line-clamp.

Single-line Truncation:

Combine three properties: white-space: nowrap, overflow: hidden, text-overflow: ellipsis.

Multi-line Truncation:

Use -webkit-line-clamp (widely supported despite the prefix) with the old flex box model:

1
display: -webkit-box
2
-webkit-box-orient: vertical
3
-webkit-line-clamp: N (number of visible lines)
4
overflow: hidden

Standard line-clamp (upcoming):

The unprefixed line-clamp property is being standardized to replace the webkit version.

Code Implementation Example

css
/* Single-line truncation */
.title-truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Multi-line truncation (3 lines) */
.excerpt {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.6;
}
💡Single-line and multi-line text truncation with ellipsis.
🎯 Interview Key Takeaway

Use -webkit-line-clamp for multi-line text truncation. It is the de facto standard supported in all browsers.

#Truncation#Ellipsis#Text#Intermediate
Back to all CSS & Modern Layouts Questions→

More CSS & Modern Layouts Interview Questions

Beginner LevelCSS & Modern Layouts

What is the CSS Box Model and how do content, padding, border, and margin interact?

Beginner LevelCSS & Modern Layouts

How does CSS specificity and the cascade determine which styles are applied?

Beginner LevelCSS & Modern Layouts

What are the differences between display: block, inline, inline-block, flex, and grid?