@chitovas/ngx-clamp

January 21, 2026 ยท View on GitHub

Build NPM version bundle size npm

A lightweight Angular directive for clamping text to a specified number of lines or height. A fast, cross-browser alternative to CSS line-clamp that works in all browsers, including legacy browsers where native support is unavailable.

  • Fast - Uses binary search algorithm for O(log n) truncation performance
  • Universal - Works across all browsers including IE11 and older
  • Lightweight - Zero dependencies, tree-shakeable

Installation

npm install @chitovas/ngx-clamp

Usage

import { Component } from '@angular/core';
import { NgxClamp } from '@chitovas/ngx-clamp';

@Component({
    selector: 'app-example',
    standalone: true,
    imports: [NgxClamp],
    template: ` <div ngxClamp [lines]="3">Long text content that will be clamped...</div> `,
})
export class ExampleComponent {}

Clamp by Height

<div ngxClamp [maxHeight]="100">Content clamped at 100px height...</div>

Custom Truncation Text

<div ngxClamp [lines]="3" truncationText=" Read more...">Content with custom truncation indicator...</div>

API

InputTypeDefaultDescription
linesnumber-Number of lines before clamping
maxHeightnumber-Maximum height (px) before clamping
truncationTextstring'...'Text appended to clamped content

Use either lines or maxHeight. If both are provided, lines takes precedence.

License

MIT