text-overflow: ellipsis第二行的CSS ,这可能吗?我在网上找不到。
例:
我想要的是这样的:
I hope someone could help me. I need 
an ellipsis on the second line of...但这是怎么回事:
I hope someone could help me. I ... text-overflow: ellipsis第二行的CSS ,这可能吗?我在网上找不到。
例:
我想要的是这样的:
I hope someone could help me. I need 
an ellipsis on the second line of...但这是怎么回事:
I hope someone could help me. I ... Answers:
text-overflow: ellipsis;工作要求是white-space(pre,nowrap等)的单行版本。这意味着文本永远不会到达第二行。
嗯 在纯CSS中是不可能的。
我刚在寻找完全相同的东西时的来源:http ://www.quirksmode.org/css/textoverflow.html(Quirksmode ftw!)
编辑如果优秀的CSS众神将实现http://www.w3.org/TR/css-overflow-3/#max-lines,我们可以使用fragments(new)和max-lines(new)在纯CSS中进行模糊处理。在http://css-tricks.com/line-clampin/上还有更多信息
编辑2 WebKit / Blink具有line-clamp:-webkit-line-clamp: 2将省略号放在第二行。
这应该在webkit浏览器中起作用:
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;overflow: hidden才能使其正常工作。
                    正如其他人已经回答的那样,不存在纯CSS解决方案。有一个非常易于使用的jQuery插件,称为dotdotdot。它使用容器的宽度和高度来计算是否需要截断并添加省略号。
$("#multilinedElement").dotdotdot();这是一个jsFiddle。
如果有人正在使用SASS / SCSS并偶然发现了这个问题-也许这个mixin可能会有所帮助:
@mixin line-clamp($numLines : 1, $lineHeight: 1.412) {
  overflow: hidden;
  text-overflow: -o-ellipsis-lastline;
  text-overflow: ellipsis;
  display: block;
  /* autoprefixer: off */
  display: -webkit-box;
  -webkit-line-clamp: $numLines;
  -webkit-box-orient: vertical;
  max-height: $numLines * $lineHeight + unquote('em');
}这只会在webkit浏览器中添加省略号。休息只是切断它。
/* autoprefixer: off */为/*! autoprefixer: off */其他选项,否则-webkit-box-orient: vertical;在编译后会被删除,这意味着省略号从未显示过
                    只需对支持它的浏览器(大多数现代浏览器)使用line-clamp,而对于较旧的浏览器则使用line-clamp。
  .text {
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    @supports (-webkit-line-clamp: 2) {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: initial;
      display: -webkit-box;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }
  }您无法让它在两行上工作真是太可惜了!如果该元素是显示块,并且高度设置为2em或类似的东西,那就太棒了,当文本溢出时,它会显示省略号!
对于单个衬板,您可以使用:
.show-ellipsis {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}对于多行,您可以使用github http://pvdspek.github.com/jquery.autoellipsis/上的jQuery autoellipsis之类的Polyfill
dotdotdot在另一篇文章中提到使用。
                    我不是JS专业人士,但是我想出了几种方法可以做到这一点。
HTML:
<p id="truncate">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi elementum consequat tortor et euismod. Nam commodo consequat libero vel lobortis. Morbi ac nisi at leo vehicula consectetur.</p>然后使用jQuery将其截断为特定字符数,但保留最后一个字,如下所示:
// Truncate but leave last word
var myTag = $('#truncate').text();
if (myTag.length > 100) {
  var truncated = myTag.trim().substring(0, 100).split(" ").slice(0, -1).join(" ") + "…";
  $('#truncate').text(truncated);
}结果看起来像这样:
Lorem ipsum dolor坐下,一直保持着安静的状态。Morbi
elementum consequat tortor等...
或者,您可以简单地将其截断为特定的字符数,如下所示:
// Truncate to specific character
var myTag = $('#truncate').text();
if (myTag.length > 15) {
  var truncated = myTag.trim().substring(0, 100) + "…";
  $('#truncate').text(truncated);
}结果看起来像这样:
Lorem ipsum dolor坐下,一直保持着安静的状态。Morbi
elementum consequat tortor等euismod ...
希望那些对你有帮助。
这是jsFiddle。
这是纯CSS的好例子。
.container{
  width: 200px;
}
.block-with-text {
  overflow: hidden;
  position: relative; 
  line-height: 1.2em;
  max-height: 3.6em;
  text-align: justify;  
  margin-right: -1em;
  padding-right: 1em;
}
.block-with-text+.block-with-text{
  margin-top:10px;
}
.block-with-text:before {
  content: '...';
  position: absolute;
  right: 0;
  bottom: 0;
}
.block-with-text:after {
  content: '';
  position: absolute;
  right: 0;
  width: 1em;
  height: 1em;
  margin-top: 0.2em;
  background: white;
}<div class="container">
<div class="block-with-text">
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
</div>
<div class="block-with-text">
Lorem Ipsum is simply dummy text of the printing and typesetting industry
</div>
<div class="block-with-text">
Lorem Ipsum is simply
</div>
</div>我以前使用过jQuery-condense-plugin,看起来它可以完成您想要的工作。如果没有,那么可能有满足您需求的其他插件。
编辑:为您制作了一个演示 -请注意,我在演示上链接了jquery.condense.js,该演示具有神奇的效果,因此,该插件的作者非常感谢:)
用省略号修饰多行文本的纯CSS方式
调整文本容器的高度,控制线由-webkit-line-clamp断开:2;
.block-ellipsis {
  display: block;
  display: -webkit-box;
  max-width: 100%;
  height: 30px;
  margin: 0 auto;
  font-size: 14px;
  line-height: 1;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}我之前遇到过此问题,并且没有纯CSS解决方案
这就是为什么我开发了一个小型图书馆来处理此问题(以及其他问题)的原因。该库提供用于建模和执行字母级文本呈现的对象。例如,您可以模拟文本溢出:带有任意限制的省略号(不需要一行)
有关更多信息,请访问http://www.samuelrossille.com/home/jstext.html,以获取屏幕截图,教程和下载链接。
如果有人想让线夹在flexbox中工作,那会有些棘手-尤其是当您用很长的单词对酷刑进行测试时。此代码段中唯一的实际区别是min-width: 0;在包含截断文本的flex项目中,和word-wrap: break-word;。有关见解的https://css-tricks.com/flexbox-truncated-text/的提示。免责声明:据我所知,这仍然是webkit。
.flex-parent {
  display: flex;
}
.short-and-fixed {
  width: 30px;
  height: 30px;
  background: lightgreen;
}
.long-and-truncated {
  margin: 0 20px;
  flex: 1;
  min-width: 0;/* Important for long words! */
}
.long-and-truncated span {
  display: inline;
  -webkit-line-clamp: 3;
  text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  word-wrap: break-word;/* Important for long words! */
}<div class="flex-parent">
  <div class="flex-child short-and-fixed">
  </div>
  <div class="flex-child long-and-truncated">
    <span>asdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdasd</span>
  </div>
  <div class="flex-child short-and-fixed">
  </div>
</div>这里的所有答案都是错误的,他们缺少重要的部分,高度
.container{
    width:200px;
    height:600px;
    background:red
}
.title {
        overflow: hidden;
        line-height: 20px;
        height: 40px;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }
<div class="container">
    <div class="title">this is a long text you cant cut it in half</div>
</div>基于-webkit-line-clamp的纯CSS方法,可在webkit上使用:
@-webkit-keyframes ellipsis {/*for test*/
    0% { width: 622px }
    50% { width: 311px }
    100% { width: 622px }
}
.ellipsis {
    max-height: 40px;/* h*n */
    overflow: hidden;
    background: #eee;
    -webkit-animation: ellipsis ease 5s infinite;/*for test*/
    /**
    overflow: visible;
    /**/
}
.ellipsis .content {
    position: relative;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-box-pack: center;
    font-size: 50px;/* w */
    line-height: 20px;/* line-height h */
    color: transparent;
    -webkit-line-clamp: 2;/* max row number n */
    vertical-align: top;
}
.ellipsis .text {
    display: inline;
    vertical-align: top;
    font-size: 14px;
    color: #000;
}
.ellipsis .overlay {
    position: absolute;
    top: 0;
    left: 50%;
    width: 100%;
    height: 100%;
    overflow: hidden;
    /**
    overflow: visible;
    left: 0;
    background: rgba(0,0,0,.5);
    /**/
}
.ellipsis .overlay:before {
    content: "";
    display: block;
    float: left;
    width: 50%;
    height: 100%;
    /**
    background: lightgreen;
    /**/
}
.ellipsis .placeholder {
    float: left;
    width: 50%;
    height: 40px;/* h*n */
    /**
    background: lightblue;
    /**/
}
.ellipsis .more {
    position: relative;
    top: -20px;/* -h */
    left: -50px;/* -w */
    float: left;
    color: #000;
    width: 50px;/* width of the .more w */
    height: 20px;/* h */
    font-size: 14px;
    /**
    top: 0;
    left: 0;
    background: orange;
    /**/
}<div class='ellipsis'>
    <div class='content'>
        <div class='text'>text text text text text text text text text text text text text text text text text text text text text </div>
        <div class='overlay'>
            <div class='placeholder'></div>
            <div class='more'>...more</div>
        </div>
    </div>
</div>我找到了一个解决方案,但是您需要知道适合于您的文本区域的粗略字符长度,然后将...连接到前面的空格。
我这样做的方法是:
代码:
truncate = function(description){
        return description.split('').slice(0, description.lastIndexOf(" ",200)).join('') + "...";           
}这是一个小提琴-http: //jsfiddle.net/GYsW6/1/