25

I use this webkit line clamp, it works in Chrome, but not in Firefox. The following is the code:

{
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 4; /* number of lines to show */
  line-height: X; /* fallback */
  max-height: X * 4; /* fallback */
}

How should I make it work on Firefox as well?

Penny Liu
  • 15,447
  • 5
  • 79
  • 98
uklp
  • 551
  • 1
  • 6
  • 14

5 Answers5

40

Important Update:

As of Firefox version 68 Firefox supports -webkit-line-clamp!!

Demo snippet (caniuse)

p {
  width: 300px;
  border: 2px solid green;
  padding: 0.5em 0.5em 0 0.5em;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3; /* number of lines to show */
}
<p>When the CSS -webkit-line-clamp property is applied to a block of text (e.g., a paragraph), the text is limited to the specified number of lines (1 or more), and an ellipsis character (…) is added to the end of the last visible line. - see <a href="https://webplatform.news/issues/2019-05-17">webplatform.news</a>

Although Firefox uses the Gecko rendering Engine which uses the -moz- vendor prefix, since version 49, Firefox decided to add support for several -webkit- prefixes and WebKit-specific interfaces

Note: CSS Overflow Module Level 3 Editor's draft includes an official property line-clamp - which will likely replace the proprietary-webkit-line-clamp property.

Original Answer

You can't. -webkit-line-clamp is for browsers that use webkit. Firefox runs on gecko and uses the vendor prefix: -moz-

If you're interested - you could take a look at my answer here: a line-clamp with fade-out fallback fiddle which adds a fade-out effect workaround (instead of ellipsis) for non-webkit browsers.

Danield
  • 121,619
  • 37
  • 226
  • 255
1

/----line clamp---/

.line-clamp {
    position: relative;
    height: 2.7em; 
    overflow: hidden;
}
.line-clamp:after {
    background: $white;
    bottom: 0;
    position: absolute;
    right: 0;
    float: right;
    content: '\2026';
    margin-left: -3rem;
    width: 1rem;
}

@supports (-webkit-line-clamp: 2) {
    .line-clamp {
        display: -webkit-box;
        -webkit-line-clamp: 2;
        / autoprefixer: off /
        -webkit-box-orient: vertical; 
        / autoprefixer: on /
        max-height:3.6em; 
        height: auto;
    }
    .line-clamp:after {
        display: none;
    }
}

/----line clamp end---/

0

In firefox, -webkit-line-clamp don't work

Here a javascript code that works fine
http://codepen.io/nilsynils/pen/zKNpkm?editors=1111

const containers = document.querySelectorAll('.container');
Array.prototype.forEach.call(containers, (container) => {  // Loop through each container
    var p = container.querySelector('p');
    var divh = container.clientHeight;
    while (p.offsetHeight > divh) { // Check if the paragraph's height is taller than the container's height. If it is:
        p.textContent = p.textContent.replace(/\W*\s(\S)*$/, '...'); // add an ellipsis at the last shown space
    }
})
Lsb
  • 9
  • 3
0

I just face thisi issue on latest Firefox with TailwindCSS and I fixed it by following code:

[class*="line-clamp-"]:not(.line-clamp-none) * {
    display: inline !important;
}
hayatbiralem
  • 649
  • 8
  • 15
-2
{
    overflow:hidden;
    text-overflow: ellipsis;    
    display: -webkit-box;    
    -webkit-line-clamp: 3;
    /* number-of lines */    
    -webkit-box-orient: vertical; 
    word-wrap:break-word;    
    line-height:1.2; 
    /* line-height for 1line*/    
    max-height:3.6rem; 
    /* line-height * 3*/
}

it works fine with chrome, ff, ie, edge

Runtime Terror
  • 6,242
  • 11
  • 50
  • 90
  • 2
    [`-webkit-line-clamp` is not supported by Firefox 58-61, IE 11 or Edge 16-17.](https://caniuse.com/#feat=css-line-clamp) – Gaʀʀʏ Apr 02 '18 at 15:35
  • @Gaʀʀʏ Edge supports it with -webkit prefix in Edge 17-18, according to the link – Alexander Nov 14 '18 at 15:39
  • @Gaʀʀʏ update 2021: `-webkit-line-clamp` is now supported by all major browsers with the `-webkit` prefix. – Joel May 10 '21 at 12:43