【问题标题】:Why webkit line clamping does not work in firefox?为什么 webkit 线夹在 Firefox 中不起作用?
【发布时间】:2014-01-02 21:41:22
【问题描述】:

我使用这个 webkit 线夹,它适用于 Chrome,但不适用于 Firefox。以下是代码:

{
  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 */
}

我应该如何让它也能在 Firefox 上运行?

【问题讨论】:

  • 因为 Firefox 不使用 WebKit...?
  • 是的,我注意到了。我只是想要另一种方式来实现相同的功能。现在明白了。谢谢。

标签: html css firefox


【解决方案1】:

重要更新:

截至 Firefox 版本 68 Firefox supports -webkit-line-clamp!!

演示 sn-p (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>

尽管 Firefox 使用 Gecko rendering Engine,它使用了 -moz- 供应商前缀,但从版本 49 开始,Firefox decided to add support for several -webkit- prefixes 和 WebKit 特定接口

注意: CSS Overflow Module Level 3 Editor's draft 包含一个官方属性 line-clamp - 它可能会取代专有的-webkit-line-clamp 属性。

原答案

你不能。 -webkit-line-clamp 适用于使用 webkit 的浏览器。 Firefox 在gecko 上运行并使用供应商前缀:-moz-

如果您有兴趣 - 您可以查看my answer hereline-clamp with fade-out fallback fiddle,它为非 webkit 浏览器添加了淡出效果解决方法(而不是省略号)。

【讨论】:

  • 它仍然无法在 Firefox 上运行,因为它不支持 -webkit-line-clamp
  • 无法在 Firefox 上工作...@MarcosPérezGude
  • 仍然无法在 Firefox 中工作,尽管他们声称现在可以使用
  • @Pixelomo - 在 68 版之前它无法在 firefox 上运行。Firefox 有一个名为 firefox nightly 的预发布浏览器...我在那里进行了测试,目前正在运行
  • 啊,好吧,因为大多数用户不使用它们,所以最前沿的构建并不算数,知道什么时候将 68 发布到主浏览器吗?
【解决方案2】:

/----线夹---/

.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;
    }
}

/----线夹结束---/

【讨论】:

    【解决方案3】:

    在 Firefox 中,-webkit-line-clamp 不起作用

    这是一个运行良好的 javascript 代码
    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
        }
    })
    

    【讨论】:

      【解决方案4】:
      {
          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*/
      }
      

      它适用于 chrome、ff 即边缘

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-04
      • 2013-03-18
      • 1970-01-01
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多