Truncate
Cuts off text that does not fit with an ellipsis: on one line, or after a set number of lines.
Example
View Code
<div class="center" data-max="xs">
<h3 class="truncate"><a href="#">Quarterly report: revenue, churn and the hiring plan for next year</a></h3>
<p class="truncate" data-lines="2">A bordered surface for one thing: a figure that bleeds to the edges, a body, and a footer that sits at the bottom, however long the body runs.</p>
</div>
When to use it
Text that must fit a fixed space whatever its length: a file name in a table cell, a title in a narrow sidebar, an excerpt on a card in a grid where every card should be the same height.
How it works
truncate keeps the text on one line and cuts what does not fit with an ellipsis. It needs a block to cut against: put it on a heading, a paragraph or a cell, not on a link or a span inside one.
<h3 class="truncate"><a href="#">Quarterly report: revenue, churn and the hiring plan for next year</a></h3>
data-lines lets the text wrap and cuts the last of that many lines instead. It takes 2 to 5.
<p class="truncate" data-lines="3">A bordered surface for one thingβ¦</p>
The element must hold the text directly. A truncate around other blocks cuts nothing.
Accessibility
The cut is only visual: a screen reader reads the whole text. A sighted reader cannot see the rest, so truncate only text whose full version is one step away, such as a title that links to its page or a row that opens its record. Do not rely on a title attribute to show it: touch and keyboard users never see one.
Attributes
| Attribute | Type | Values | Default | Description |
|---|---|---|---|---|
data-lines |
enum | 2, 3, 4, 5 |
Keeps this many lines and cuts the last one with an ellipsis. Absent, the text stays on one line. |
Children
No structural requirements.
Tokens
No public tokens.
Accessibility
- The cut is visual only: a screen reader reads the whole text. A sighted reader cannot see the rest, so truncate only text whose full version is one step away, such as a title that links to its page. A title attribute is not a way to show it, since touch and keyboard users never see one.
Browser support
- Used without guards: -webkit-line-clamp
- Behind
@supports: nothing
JavaScript
None. This component is CSS only.
Available since 7.0.0.