日本語

CSS -webkit-line-clamp Not Cutting to 3 Lines: Causes and Fixes

​​CSS​Flexbox,Grid,HTML,Playwright

If -webkit-line-clamp: 3 doesn't cut your text at three lines, first check the computed display and -webkit-box-orient values of the element. If another rule overrides display to block or flex, or if -webkit-box-orient: vertical is missing, the line count is ignored. I tested each cause that stops truncation for truncating the body text or headings to three lines with "…" in article list cards and news lists.

Note

The test environment is Ubuntu 24.04, Node.js 22.23.5, Playwright 1.63.0, and Chromium 158. I opened a page with only HTML and CSS in Playwright at an 800px-wide viewport and measured the element heights (clientHeight and scrollHeight) and computed values. The text is Japanese text that would take six lines without clamping. The width is 320px, the font size is 16px, the line height is 1.6 (25.6px), and the font is Noto Sans CJK JP. If the text is cut at three lines, clientHeight is 77px. If it isn't cut, it is 154px. I did not test Firefox, Safari, mobile browsers, or the developer tools display.

The basic setup uses the four properties below together. In my earlier article on this blog, "Truncating long text to three lines with "…" in CSS," I confirmed that max-height does not add "…", and that adding padding lets the fourth line show.

css
.clamp-3 {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
}

List of causes

The causes I tested and the heights I measured are below.

Cause Height (clientHeight) Fix
Overridden to display: block / flex / grid by a later rule 154px (6 lines) Remove the rule that overrides it
-webkit-box-orient: vertical is missing 154px (6 lines) Add it
white-space: nowrap inherited from the parent 26px (1 line) white-space: normal on the element
Long URL with no break points 26px (1 line) overflow-wrap: anywhere
Inner element is display: flex / inline-block 154px (6 lines) Make the inner element a normal block
height set larger than three lines 200px Remove height
Height stretched in flex or grid 100px (4th line visible) In flex, align-items: flex-start on the parent; in grid, align-self: start on the element

I'll go through each one below.

display is overridden by another rule

When a rule placed after .clamp-3 sets display: block on the same element, all six lines were visible without cutting. The same happened with display: flex and display: grid, and the height stayed at 154px. I also put a media query that sets display: block for widths of 600px and up after the rule, and at an 800px viewport it did not cut either.

As MDN's description of line-clamp says, -webkit-line-clamp only works when display is -webkit-box or -webkit-inline-box. If you read getComputedStyle(el).display and get block or flex, some rule is overriding it.

What makes this confusing is the computed value when clamping works. In Chromium 158, the test environment, the computed display was flow-root, not -webkit-box, when the text was cut. When I used -webkit-inline-box, it was inline-block. The draft of CSS Overflow Module Level 4, in the section on the continue property, says that when the line count applies, the computed value becomes flow-root if the specified display is -webkit-box, and inline-block if it is -webkit-inline-box. If the computed value is flow-root, it has not been overridden.

-webkit-box-orient: vertical is missing

When I set display: -webkit-box, -webkit-line-clamp: 3, and overflow: hidden but left out -webkit-box-orient, the text was not cut. The computed -webkit-box-orient was horizontal. Setting -webkit-box-orient: horizontal explicitly gave the same result.

The draft of CSS Overflow Module Level 4, in the section on compatibility with the legacy syntax, says -webkit-line-clamp only works when the specified display is -webkit-box or -webkit-inline-box and -webkit-box-orient is vertical. The same draft's section on the continue property also notes that without -webkit-box-orient: vertical, the element becomes a flex container rather than a block container.

The parent's white-space: nowrap is inherited

When the parent element has white-space: nowrap, -webkit-line-clamp: 3 on a child element did not wrap the text, and it stayed on one line. The height was 26px and scrollWidth was 1720px. The text overflowed to the right, and overflow: hidden hid the overflow.

As the official definition in MDN's white-space description states, white-space is inherited from the parent.

css
.clamp-3 {
  /* in addition to the four above */
  white-space: normal; /* cancels the parent's nowrap */
}

With white-space: normal on the element being cut, the text was cut at three lines, and the height was 77px.

Long URLs and alphanumeric strings with no break points

I put in only a URL, https://example.com/ followed by 120 letters with no break points. The height was 26px on one line, and scrollWidth was 1097px. The URL did not wrap, so it stayed on one line and overflowed to the right.

As MDN's overflow-wrap description says, overflow-wrap: anywhere wraps long words and strings like URLs, even where there are no normal break points. With it, the URL wrapped, and the text was cut at three lines with a height of 77px.

Inner element is flex or inline-block

When I wrapped the content of the element being cut in <div style="display: flex">, and also when I wrapped it in <span style="display: inline-block">, the text was not cut and the height was 154px in both cases. When I wrapped it in a <p> (a normal block), it was cut at 77px.

If you want to use flex, reverse the nesting. Put the flex box on the outside, and make the element being cut a child inside it. When I set the parent to display: flex and placed only the child with .clamp-3 inside, it was cut at 77px. However, if I placed an element taller than three lines (77px) next to it in the same parent, the height of the text element stretched. The fix for that is covered in the section below, "Cards in flex or grid stretch in height and show a fourth line."

height is set

When I added height: 200px to the element being cut, the element was 200px tall, and lines 4 through 6 were visible. There were 3,455 text pixels below the 77px that three lines take. min-height: 120px also showed the fourth and later lines down to 120px.

Conversely, when the height was smaller than three lines, such as height: 40px, the element was cut at 40px and did not show three lines. With max-height: 200px, it stopped at 77px, the height of three lines. Let -webkit-line-clamp decide the height, and do not set height.

Cards in flex or grid stretch in height and show a fourth line

In a list card, I placed a 100px square thumbnail on the left and the body text on the right, and set -webkit-line-clamp: 3 on the body text. The card was display: flex. A "…" appeared at the end of the third line, but the height of the body element stretched to the same 100px as the thumbnail, and most of the fourth line was visible.

Comparison of two cards, one above the other. In the top card, with no setting (the default align-items value, normal), a "…" appears after the third line, and the fourth line is visible below it, reading roughly "...tell at a glance what to do" (the Japanese text in the image). In the bottom card, with align-items: flex-start, the text ends at the third line with "What to do next…" (the Japanese text in the image).
A flex card with a 100px thumbnail on the left. With no setting, the body text stretches to the thumbnail’s height and the fourth line is visible (top). With align-items: flex-start, the text stops at three lines (bottom)

According to MDN's align-items description, the initial value normal acts as stretch for flex items. The body element is stretched to the full height of the parent, so the part below three lines is also inside the overflow: hidden area and becomes visible.

css
.card {
  display: flex;
  gap: 8px;
  align-items: flex-start; /* keep the text from stretching */
}

With align-items: flex-start on the card, the body height returned to 77px, and no text pixels remained below three lines. When I built the same layout with grid, the body text stretched to 100px and the fourth line was visible. In grid, adding align-self: start to the body element returned it to 77px.

Wrong value format and property name

When I wrote the value with a unit, such as -webkit-line-clamp: 3px, the setting was ignored and the computed value stayed none. The draft of CSS Overflow Module Level 4 allows only none or an integer of 1 or more for -webkit-line-clamp. Write the line count as an integer without a unit.

I also kept display: -webkit-box, -webkit-box-orient: vertical, and overflow: hidden, and replaced only -webkit-line-clamp: 3 with the unprefixed line-clamp: 3. The text was still not cut. In Chromium 158, the test environment, you must write the prefixed -webkit-line-clamp.

​Posted by 86