【问题标题】:Add three dots in a multiline span在多行跨度中添加三个点
【发布时间】:2012-08-03 19:49:50
【问题描述】:

我找到了this question here on SO,解决方法很简单:

jsfiddle:http://jsfiddle.net/Y5vpb/

html:

<span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen look</span>​

css:

span{
    display:inline-block;
    width:180px;
    white-space: nowrap;
    overflow:hidden !important;
    text-overflow: ellipsis;
}

它按预期工作,打印:Lorem Ipsum is simply du...

现在,我想做同样的事情,但一直失败,在以下示例中创建:

jsfiddle:http://jsfiddle.net/9HRQq/

html:

<div class="textContainer">                
    <img src="#" class="image" alt="the_image">
    <span class="text">"The quick brown fox jumps over the lazy dog" is an english-language pangram, that is, a phrase that contains all of the letters of the English alphabet. It has been used to test typewriters and computer keyboards, and in other applications involving all of the letters in the English alphabet. Owing to its brevity and coherence, it has become widely known.</span>
</div>

css:

.textContainer{
    width: 430px;
    float: left;
    margin-top: 10px;
    border: 1px solid black;                
}

.text {
    font-size: 24px;
    line-height: 24px;
    font-weight: bold;
    color: #047F04;
    display: block;
    white-space: normal;
    overflow: hidden !important;
    text-overflow: ellipsis;
    height: 99px;
}

.image {
    float: right;
    position: absolute;
    top: 85px;
    right: 10px;
    width: 108px;
    height: 42px;
}

您能告诉我如何在我的示例中将... 放在字符串的末尾吗?

【问题讨论】:

  • 你不应该需要!important。 (如果您确实需要它,那么您的样式表中的其他地方可能有问题)

标签: html css


【解决方案1】:

这在没有 js 或 jquery 的情况下是可能的。只是纯 CSS。

类似于 vijay 的回答,但要添加,您需要为 -webkit-box-orient: vertical; 设置一个额外的字段

span {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;  /* limit to 2 lines */
}

【讨论】:

    【解决方案2】:

    text-overflow 属性的规范表明这对于多行文本是不可能的:

    此属性指定当内联内容在其具有“溢出”而不是“可见”的内联进展方向上溢出其块容器元素(“块”)时呈现。例如,当无法换行时(例如,由于“white-space:nowrap”或单个单词太长而无法容纳),文本可能会溢出。

    换句话说,仅适用于单行文本块。

    来源:http://dev.w3.org/csswg/css3-ui/#text-overflow

    编辑 这个小提琴有一个使用 jquery 的解决方法:http://jsfiddle.net/k5VET/(来源:Cross browsers mult-lines text overflow with ellipsis appended within a width&height fixed div?

    【讨论】:

    • 我再次搜索后也遇到了这个问题。其他人也尝试过,但最终还是使用了 jQuery。
    • @devundef:是的,实际上我可以,我不知道为什么我以前没有想到这一点。谢谢!现在,当我用谷歌搜索时,我看到还有(OFC)插件可以用 jQuery 做这种事情,例如:dotdotdot.frebsite.nl
    【解决方案3】:
    span{
        display: block; /* Fallback for non-webkit */
        display: -webkit-box;
        -webkit-line-clamp: 3; /* no of lines */
        text-overflow: ellipsis;
        overflow:hidden !important;
        width:180px;
    }
    

    CSS 属性上方会放置三个点...
    例如:

    Lorem Ipsum 只是假的
    印刷文字和
    排版行业。洛雷姆...

    【讨论】:

      【解决方案4】:

      width: 200px;white-space: nowrap; 添加到您的 .text css 块中。没有设置宽度,文本只是扩展和填充。

      【讨论】:

      • white-space: nowrap; 将导致内容不跨越多行。我认为 OP 的问题是关于保持多行,同时仍然在末尾自动生成省略号,如果内容已被剪辑。
      • 啊,我想我没听懂那部分,谢谢。
      猜你喜欢
      • 2018-08-03
      • 2023-03-25
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-24
      相关资源
      最近更新 更多