A lot of times, while working with text in our designs, we need to limit copy to a certain number of characters or lines—and cut off anything after that gracefully. That technique is known as line clamping. There are a few ways to do it; in this article we’ll look at one approach with pure CSS and one with pure JavaScript.
Originally published on Medium in FAUN.dev().
If line clamping still feels fuzzy, the examples below should make it clear.


Using CSS
We can get the result with CSS alone. The biggest benefit: when the container width changes, the browser figures out how much text fits and adds the … ellipsis at the end of the last visible line.
<span class="trendingListItemTextHeadLine">
Twelve Start-ups from PIER71’s Smart Port Challenge Awarded S$600,000 to Bring Innovative Solutions to Maritime Market
</span>
.trendingListItemTextHeadLine {
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
overflow: hidden;
width: 100%;
}
-webkit-line-clampsets how many lines the element may show; anything beyond that is clamped.- For
-webkit-line-clampto work,displaymust be-webkit-boxand-webkit-box-orientmust bevertical. - That combination adds the ellipsis after two lines and constrains the box height, but overflowing text can still paint unless you also set
overflow: hidden.
And that’s it—that’s all you need.
To read more about -webkit-line-clamp, see the MDN documentation.
You can also check browser support below (as captured when the article was written):

Using JavaScript
You can also shorten a sentence with JavaScript. It’s not as elegant as CSS line-clamp, but it’s handy often enough—especially when you care about character count rather than line count. Here we use Array.prototype.reduce.
function reduceText(itemText) {
const maxLengthOfLink = 120 // maximum number of characters you want
const itemTextArray = itemText.split(' ')
let reducedFlag = -1
return itemTextArray.reduce((accumulator, currentVal) => {
if (accumulator.length + currentVal.length > maxLengthOfLink) {
reducedFlag++
return reducedFlag ? accumulator : `${accumulator}...`
}
else {
return `${accumulator} ${currentVal}`
}
})
}
- First, split the string into an array of words on spaces.
- The reducer receives four arguments:
- Accumulator
- Current value
- Current index
- Source array
- Whatever the reducer returns becomes the next accumulator, and after the last iteration that value is the final result.
If reduce still isn’t clear, read the docs on MDN: Array.prototype.reduce().
On each iteration we return a string. If adding the next word would exceed the max character count, we flip a flag and append ...; otherwise we keep concatenating words.
This is useful when layout space is tight and the server returns long strings—you can run them through reduceText before rendering.
That’s it for this article. These aren’t the only ways to line-clamp; CSS-Tricks has a roundup of more approaches: Line Clampin’ (Truncating Multiple Line Text).
Thanks for reading—see you next time.



