CSS line-clamp Property
The CSS line-clamp property limits the visible content of a block to a specified number of lines, typically displaying an ellipsis when additional text is hidden.
It is useful for article previews, product descriptions, cards, and other layouts where text should occupy a consistent amount of space. The standardized property is still gaining browser support, while the prefixed -webkit-line-clamp technique is widely used.
line-clamp Syntax
The standardized line-clamp property accepts none or a positive integer, optionally combined with a block ellipsis value.
Basic Syntax
selector {
line-clamp: 3;
}
No Line Limit
.description {
line-clamp: none;
}
Two Lines
.description {
line-clamp: 2;
}
Five Lines
.description {
line-clamp: 5;
}
Block Ellipsis
.description {
line-clamp: 3 auto;
}
Suppress the Ellipsis
.description {
line-clamp: 3 no-ellipsis;
}
Custom Ellipsis
.description {
line-clamp: 3 "… Read more";
}
Custom block ellipsis syntax is part of the evolving CSS Overflow specification and should not be assumed to work in all browsers.
Widely Used Prefixed Syntax
.description {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
This prefixed technique is the practical choice for broad browser compatibility.
CSS-Wide Keywords
.description {
line-clamp: initial;
}
.description {
line-clamp: inherit;
}
.description {
line-clamp: unset;
}
.description {
line-clamp: revert;
}
.description {
line-clamp: revert-layer;
}
The initial value is none. The standardized property is not inherited.
Property Values
The line-clamp shorthand controls the maximum number of visible lines and, where supported, the block ellipsis.
| Value | Description |
|---|---|
none |
Does not impose a line limit through this property. |
<integer> |
Specifies the maximum number of visible lines using a positive integer. |
auto |
Uses the automatic block ellipsis behavior when combined with a line count. |
no-ellipsis |
Requests truncation without a block ellipsis, where supported. |
<string> |
Specifies a custom block ellipsis string, where supported. |
initial |
Resets the property to none. |
inherit |
Uses the computed value from the parent element. |
unset |
Resets the property to none because it is not inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | none |
| Inherited | No |
| Applies to | Block containers, subject to the clamping model and applicable layout restrictions |
| Computed value | Specified line count and block ellipsis settings |
| Animation type | Discrete |
| Percentages | Not applicable |
| Negative values | Not allowed for the line count |
| Primary purpose | Limit the number of visible text lines |
Support for the standardized shorthand and its optional ellipsis values is less established than support for the prefixed technique.
How It Works
Text normally wraps into as many lines as needed to fit its container. A line-clamping rule limits how many of those lines remain visible.
Without Clamping
.description {
line-clamp: none;
}
The paragraph can occupy as many lines as its content requires.
With Clamping
.description {
line-clamp: 3;
}
In a browser supporting the standardized property, content beyond the third line is clamped.
Compatible Implementation
.description {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
The legacy technique uses a special WebKit box layout to limit the visible content to three lines.
Important Consideration
The number of lines depends on the element's available width, font size, font family, and text content. A three-line clamp does not guarantee the same number of visible words on every screen.
none Value
The none value removes the line limit imposed by the standardized property.
Basic Example
.description {
line-clamp: none;
}
Show Complete Text
.article-content {
line-clamp: none;
overflow: visible;
}
Remove a Legacy Clamp
.description {
display: block;
-webkit-line-clamp: unset;
overflow: visible;
}
Removing a prefixed clamp may also require restoring the element's normal display and overflow values.
Important Consideration
line-clamp: none does not automatically undo unrelated height, overflow, or prefixed layout restrictions.
Integer Values
A positive integer specifies the maximum number of lines to display.
One Line
.description {
line-clamp: 1;
}
Two Lines
.description {
line-clamp: 2;
}
Three Lines
.description {
line-clamp: 3;
}
Four Lines
.description {
line-clamp: 4;
}
Invalid Zero Value
.description {
line-clamp: 0;
}
Zero is not a valid line count.
Invalid Negative Value
.description {
line-clamp: -2;
}
Negative integers are also invalid.
Important Consideration
A line count limits visible lines rather than setting an exact element height. Content shorter than the limit remains fully visible.
Block Ellipsis
The standardized line-clamp shorthand includes a block ellipsis component that controls the marker displayed when content is truncated.
Automatic Ellipsis
.description {
line-clamp: 3 auto;
}
The browser selects an appropriate truncation marker.
No Ellipsis
.description {
line-clamp: 3 no-ellipsis;
}
This requests clamping without an ellipsis marker.
Custom Text Marker
.description {
line-clamp: 3 "…";
}
Custom Continuation Text
.description {
line-clamp: 3 "… more";
}
Important Consideration
Custom block ellipsis values have limited implementation support. For a reliable, interactive "Read more" feature, provide a separate control that reveals the full text instead of relying on a custom ellipsis string.
The -webkit-line-clamp Technique
The prefixed -webkit-line-clamp property is the established approach for multi-line text truncation across modern browsers.
Three-Line Clamp
.description {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
Two-Line Clamp
.card-title {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
Four-Line Clamp
.article-preview {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 4;
overflow: hidden;
}
Adding the Standard Property
.description {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
line-clamp: 3;
overflow: hidden;
}
The standardized declaration provides a forward-compatible option where supported, while the prefixed declarations provide the established fallback.
Important Consideration
Despite its WebKit prefix, this technique is supported by several non-WebKit browsers for compatibility with existing websites.
display: -webkit-box
The legacy clamping technique depends on a special layout mode created with display: -webkit-box.
Required Layout
.description {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
}
Vertical Orientation
.description {
-webkit-box-orient: vertical;
}
The vertical orientation is required for the established prefixed multi-line clamping behavior.
Incorrect Layout
.description {
display: block;
-webkit-line-clamp: 3;
}
The prefixed line clamp does not function as intended with an ordinary block layout alone.
Important Consideration
The legacy WebKit box model is not the same as modern Flexbox. Do not replace display: -webkit-box with display: flex and expect the same clamping behavior.
overflow: hidden
The overflow: hidden declaration is commonly used with the prefixed clamping technique to prevent remaining content from being displayed outside the intended area.
Complete Example
.description {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
Why It Is Included
The prefixed clamping behavior and overflow clipping are related but distinct. Using overflow: hidden keeps the visual presentation contained.
Restoring Overflow
.description.expanded {
display: block;
-webkit-line-clamp: unset;
overflow: visible;
}
Important Consideration
Clipping content can hide information and interactive controls. Avoid placing essential buttons or links inside text that may be truncated.
line-clamp vs. text-overflow
Both properties are associated with truncated text, but they serve different purposes.
| Property | Purpose |
|---|---|
line-clamp |
Limits visible content to a specified number of lines. |
text-overflow |
Controls how clipped inline content is indicated at the end of a line. |
Single-Line Ellipsis
.title {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
Multi-Line Ellipsis
.description {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
Important Consideration
Use text-overflow for ordinary single-line truncation and the prefixed line-clamping technique for multi-line previews.
Line Height and Text Clamping
The line-height property affects the height of each text line, while line-clamp controls the number of visible lines.
Three Lines with Comfortable Spacing
.description {
line-height: 1.6;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
Compact Line Spacing
.compact-description {
line-height: 1.3;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
Alternative Height-Based Clipping
.description {
line-height: 1.5rem;
max-height: 4.5rem;
overflow: hidden;
}
This limits the visible height to approximately three lines, but it does not automatically provide a proper multi-line ellipsis.
Important Consideration
Height-based clipping is not equivalent to line clamping. Font changes, inline elements, and line box differences can affect where clipping occurs.
Responsive Layouts
Different screen sizes may benefit from different line limits.
Desktop Layout
.article-preview {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 4;
overflow: hidden;
}
Mobile Layout
@media (max-width: 600px) {
.article-preview {
-webkit-line-clamp: 2;
}
}
Responsive Standard Property
.article-preview {
line-clamp: 4;
}
@media (max-width: 600px) {
.article-preview {
line-clamp: 2;
}
}
The standardized example requires a browser that implements the unprefixed clamping model.
Important Consideration
Responsive clamping should account for the available text width. A smaller line limit on mobile devices can make previews too short if the container is also narrow.
Flexbox and Grid Layouts
Line clamping is frequently used inside Flexbox and Grid cards to keep text previews visually consistent.
Grid Cards
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
gap: 20px;
}
.card-description {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
Flexbox Cards
.card {
display: flex;
flex-direction: column;
}
.card-description {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
.card-footer {
margin-top: auto;
}
Preventing Minimum-Width Problems
.card-content {
min-width: 0;
}
In some Flexbox and Grid layouts, setting min-width: 0 allows a child to shrink and wrap as intended.
Important Consideration
Apply the legacy display: -webkit-box declaration to the text element itself. The surrounding card can still use Flexbox or Grid.
Accessibility
Clamping text changes what is visually displayed, but the complete content may remain in the document and accessibility tree.
Provide Access to Full Content
<article class="card">
<h3>Understanding CSS Layout</h3>
<p class="card-description">This tutorial explains how CSS layout techniques work and how to apply them to responsive web pages.</p>
<a href="/tutorials/css-layout.html">Read the full article</a>
</article>
Avoid Hidden Interactive Controls
Do not place important links, buttons, or form controls inside clamped content. They may become visually hidden while remaining reachable by keyboard navigation.
Avoid Using Truncation for Essential Instructions
Important instructions, warnings, and error messages should remain fully visible.
Expandable Text
If users need to read the full content without navigating away, provide an accessible control that expands and collapses the text.
Important Consideration
An ellipsis indicates that content has been shortened, but it does not provide a way to retrieve the missing information. Include a clear path to the complete text when the content matters.
line-clamp Example
This example compares text limited to two, three, and four lines using the widely supported prefixed technique.
HTML Example
<div class="clamp-demo">
<div class="clamp-card">
<h3>2 Lines</h3>
<p class="clamp-text clamp-two">CSS provides many tools for controlling how text appears on a web page. The line-clamping technique allows designers to limit long descriptions while keeping card layouts compact and consistent across different screen sizes.</p>
</div>
<div class="clamp-card">
<h3>3 Lines</h3>
<p class="clamp-text clamp-three">CSS provides many tools for controlling how text appears on a web page. The line-clamping technique allows designers to limit long descriptions while keeping card layouts compact and consistent across different screen sizes.</p>
</div>
<div class="clamp-card">
<h3>4 Lines</h3>
<p class="clamp-text clamp-four">CSS provides many tools for controlling how text appears on a web page. The line-clamping technique allows designers to limit long descriptions while keeping card layouts compact and consistent across different screen sizes.</p>
</div>
</div>
CSS Example
.clamp-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
gap: 15px;
}
.clamp-card {
padding: 15px;
border: 1px solid #cbd5e1;
background-color: #f8fafc;
}
.clamp-card h3 {
margin: 0 0 12px;
font-size: 1rem;
}
.clamp-text {
margin: 0;
line-height: 1.6;
display: -webkit-box;
-webkit-box-orient: vertical;
overflow: hidden;
}
.clamp-two {
-webkit-line-clamp: 2;
}
.clamp-three {
-webkit-line-clamp: 3;
}
.clamp-four {
-webkit-line-clamp: 4;
}
2 Lines
CSS provides many tools for controlling how text appears on a web page. The line-clamping technique allows designers to limit long descriptions while keeping card layouts compact and consistent across different screen sizes.
3 Lines
CSS provides many tools for controlling how text appears on a web page. The line-clamping technique allows designers to limit long descriptions while keeping card layouts compact and consistent across different screen sizes.
4 Lines
CSS provides many tools for controlling how text appears on a web page. The line-clamping technique allows designers to limit long descriptions while keeping card layouts compact and consistent across different screen sizes.
Each card uses the same paragraph but displays a different number of lines. The visible amount of text changes with the line limit and available card width.
Change to Five Lines
.clamp-four {
-webkit-line-clamp: 5;
}
Remove the Clamp
.clamp-four {
display: block;
-webkit-line-clamp: unset;
overflow: visible;
}
Removing the prefixed clamp allows the full paragraph to be displayed.
Common Mistakes
Using Only the Prefixed Property
.description {
-webkit-line-clamp: 3;
}
This does not provide the required legacy box layout. Include display: -webkit-box and -webkit-box-orient: vertical.
Using display: flex Instead
.description {
display: flex;
-webkit-line-clamp: 3;
}
Modern Flexbox does not replace the legacy box model required by the prefixed technique.
Forgetting overflow: hidden
Without overflow clipping, the resulting appearance may not match the intended truncated preview.
Using Zero or Negative Line Counts
.description {
line-clamp: 0;
}
A line count must be a positive integer.
Assuming the Unprefixed Property Works Everywhere
The standardized line-clamp property is not as broadly implemented as the prefixed technique. Include a compatible fallback.
Confusing line-clamp with text-overflow
text-overflow: ellipsis alone does not provide multi-line clamping.
Assuming a Fixed Number of Words
A line clamp limits lines, not characters or words. Different widths and fonts change the amount of visible text.
Clamping Important Content
Do not hide essential instructions, warnings, or required information without providing a way to access the complete content.
Placing Links Inside Clamped Text
Interactive elements may become visually hidden but remain accessible through keyboard navigation. Keep interactive controls outside the clamped region.
Assuming line-clamp Sets a Fixed Height
Short content can occupy fewer lines than the specified limit. Use layout rules separately if equal-height cards are required.
Ignoring Browser Differences
Ellipsis placement and the treatment of nested elements may vary. Test the final result in the browsers you support.
Important Considerations
Keep the following points in mind when using line-clamp:
- The property limits the visible content to a specified number of lines.
- The initial value is
none. - The standardized property is not inherited.
- Line counts must be positive integers.
- The property is useful for article previews, cards, and descriptions.
- The standardized property is still gaining browser support.
- The prefixed
-webkit-line-clamptechnique is widely supported. - The legacy technique requires
display: -webkit-box. - The legacy technique also requires
-webkit-box-orient: vertical. overflow: hiddenis commonly included to clip excess content.- Modern
display: flexis not a replacement for the legacy box layout. - The standardized shorthand includes a block ellipsis component.
- Custom ellipsis strings have limited browser support.
line-clampandtext-overfloware different properties.- Use
text-overflowfor ordinary single-line truncation. - Use line clamping for multi-line text previews.
- The number of visible words depends on container width and font metrics.
line-heightaffects the height occupied by each visible line.- A line clamp does not guarantee a fixed element height.
- Responsive layouts may use different line counts.
- Clamping can be used inside Flexbox and Grid cards.
- Apply the legacy box layout to the text element rather than the surrounding card.
- Essential content should remain accessible.
- Keep interactive elements outside clamped text.
- Provide a link or expansion control when users need the full content.
- The property uses discrete animation behavior.
- Test the final appearance across browsers and screen sizes.
Browser Support
The standardized CSS line-clamp property has limited availability across browsers. Support for the unprefixed syntax and its optional block ellipsis values is still developing.
The prefixed -webkit-line-clamp technique, used with display: -webkit-box and -webkit-box-orient: vertical, is widely supported in modern Chrome, Edge, Firefox, Safari, and Opera.
For production websites, use the established prefixed technique and verify support before relying on the unprefixed property.
For current compatibility information, visit Can I Use? and search for line-clamp.
Specifications
The line-clamp property is defined in the CSS Overflow Module Level 4 specification.
CSS Overflow Module Level 4: line-clamp
The specification describes line clamping, the number of visible lines, and the block ellipsis mechanism.
For additional documentation and examples, consult MDN Web Docs.
