
Fluid Typography with CSS clamp()
04/05/2024
CSS Scroll Snap: Align Scrolling to Sections and Cards
05/05/2024
CSS line clamping limits how many lines of a block of text are visible. It is often used for previews where space is limited. The prefixed implementation combines several CSS properties and generally places an ellipsis where text is clipped.
1. How to limit text to a number of lines
Use a line limit together with the required display and overflow settings. The examples below use the prefixed combination.
p {
display: -webkit-box; /* 1: select the legacy box layout */
-webkit-box-orient: vertical; /* 2: set the box orientation to vertical */
-webkit-line-clamp: 3; /* 3: limit the visible line count */
overflow: hidden; /* 4: clip the remaining text */
}
2. How to implement line clamping
Apply these declarations to the text block:
display: -webkit-box: Selects the legacy box layout required by the prefixed clamping pattern.-webkit-line-clamp: 3: Limits the visible content to three lines in this example. Replace 3 with the number of lines you need.-webkit-box-orient: Sets the box orientation. The legacy layout supports both orientations:horizontalarranges boxes horizontally.verticalarranges boxes vertically and is required for this clamping pattern.
overflow: hidden: Clips the text that exceeds the line limit.
Syntax:
.clamped-element {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
Worked example:
To show up to three lines of a paragraph, apply the following declarations to its class:
.clamped-paragraph {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
The text remains in the document. The class clips the visual preview and usually adds an ellipsis if there is more text. Keep a way to reach the complete version when it matters.
3. Practical line-clamp examples
Clamping can make repeated previews easier to scan. Check the result at different widths and zoom levels because the words that fit will change.
Example 1: Text in content cards
Use a line limit on excerpts in a card grid to keep the cards visually consistent. Link to the complete article for the rest of the text.
.card-text {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
Example 2: Long headings or subtitles
Use line limits when titles need to fit a compact list. Make sure essential distinctions and instructions stay visible, and provide access to the full title where needed.
.clamped-title{
display: -webkit-box;
-webkit-line-clamp: 2; /* Clamp after 2 lines */
-webkit-box-orient: vertical;
overflow: hidden;
}
.clamped-subtitle {
display: -webkit-box;
-webkit-line-clamp: 4; /* Clamp after 4 lines */
-webkit-box-orient: vertical;
overflow: hidden;
}
Example 3: Product descriptions
Clamp a longer product description to show a short preview in a product list. Make the full description available on the product page.
.product-description {
display: -webkit-box;
-webkit-line-clamp: 3; /* Clamp after 3 lines */
-webkit-box-orient: vertical;
overflow: hidden;
}
Other uses for line clamping
News and article summaries:
On a homepage or news page, short summaries help readers scan items and decide which articles to open.
Comments and reviews:
Use a short excerpt for a long review and let readers open or expand the full review.
Try line clamping on a preview in your own project. Compare it at different widths with the full text so you can choose a useful line limit.