# Truncation

Clip text to one line or a number of lines and end it with an ellipsis.

- ID: truncation
- Type: utility
- Canonical: /reference/truncation
- Version: 2.1.8
- Source revision: 6ddc2ad780f7639f26ffc9ad66eed90484ca12eb (revision)
- Source: packages/preset/src/utilities.css
- Content digest: 8cdc80b0f1e57663f3a97b8835b2024a4d4970fd64c66bd93ac06c910a2ab497
- Semantic digest: 83962260d6d56817dd052d2beebf7999a75804215dac050c47b26353145c099a
- Language: en

> Syntax placeholders illustrate declaration shapes; they are not an exhaustive grammar for valid values. Examples use the current preset.

<a id="overview"></a>

## Classes

| Class | Declarations |
| --- | --- |
| `truncate` | `overflow: hidden;` `text-overflow: ellipsis;` `white-space: nowrap;` |
| `clamp-lines(<integer>)` | `display: -webkit-box;` `-webkit-box-orient: vertical;` `-webkit-line-clamp: <integer>;` `overflow: hidden;` `overflow-wrap: break-word;` `text-overflow: ellipsis;` |

Both need a constrained width. Inside a flex or grid item, give that item `min-w(0)` so it can shrink.

<a id="visible-lines"></a>

## Limit the visible text, keep access to the full text

`truncate` combines hidden overflow, an ellipsis and no wrapping. It needs a block container with a finite inline width to overflow; applying it to an unconstrained inline span does not make a one-line preview.

**The same text can paint all lines, one line or two lines**

The font, width and text are identical. Only the truncation class changes. The native browser paints the ellipsis; the full string remains available in each element.

- Each copy has a 192px width, a 14px font and a 24px line height.
- The first copy wraps freely and grows to contain every line. truncate keeps the second on one line and hides the overflow with an ellipsis.
- clamp-lines(2) lets the third copy wrap, then limits its visible content to two lines using the prefixed line-clamp combination.
- All three elements contain the same full string. A separate disclosure or link is needed when readers must access the complete visible text.

`clamp-lines(2)` uses the prefixed line-clamp combination: `display: -webkit-box`, vertical box orientation, a two-line clamp and hidden overflow. Use a positive integer. It allows wrapping, so do not combine it with `truncate` and its `white-space: nowrap`.

The text stays in the DOM. Clipping is a visual constraint, not a shorter string or a disclosure mechanism. Keep essential information available through a real link or an expandable full-text view; do not rely on a hover-only title.

A flex or grid item’s automatic minimum can keep it wider than the space available. Set `min-w(0)` on the item that must shrink, then constrain the text within it. With line clamping, keep padding on a wrapper so padding does not expose clipped lines.

The recipe owns its display and overflow declarations. Overriding those declarations with `flex`, `grid` or visible overflow can break the clamp. For custom truncation rules, use the native properties explicitly.

See [native text overflow](https://www.w3.org/TR/css-overflow-3/#text-overflow) and [legacy line clamping](https://www.w3.org/TR/css-overflow-4/#webkit-line-clamp).

<a id="examples"></a>

## Usage

Keep a file name on one line and its summary to two. Expand the details to read both in full.

```html
<article class="… max-w-3xs">
  <p class="… truncate">field-notes-autumn-collection-final-v03.fig</p>
  <p class="… clamp-lines(2)">Good interfaces make space for…</p>
  <details><summary>Read full details</summary><p>field-notes-autumn-collection-final-v03.fig</p><p>Good interfaces make space for…</p></details>
</article>
```

```css disclosure=generated-css
@layer theme{:root,:host{--container-3xs:16rem}}@layer utilities{.clamp-lines\(2\){display:-webkit-box;-webkit-box-orient:vertical;-webkit-line-clamp:2;overflow:hidden;overflow-wrap:break-word;text-overflow:ellipsis}.truncate{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}.max-w-3xs{max-width:var(--container-3xs)}}
```

<a id="customize"></a>

## Customize

The preset defines these classes with `@utility`. Define the same name in your stylesheet to replace it.

```css
@utility truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

@utility clamp-lines(--n <integer>) {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: var(--n);
  overflow: hidden;
  overflow-wrap: break-word;
  text-overflow: ellipsis;
}
```

See [utility definitions](/reference/directives/definitions) and [Typography](/guide/typography).

## Related reference

- [directives/definitions](/reference/directives/definitions.md)
