如何进行文本溢出:两行省略?

时间:2015-07-29 10:53:14

标签: css

我有一个容器,文本可以扩展为两行,高度为40px,字体大小为18px。当我这样做时:

text-overflow: ellipsis;
white-space: nowrap;

然后虚线显示正确,但它在一行上被切断。当我这样做时:

text-overflow: ellipsis;

然后它正确地显示了2行,但没有" ..."在末尾。我如何实现这一目标,以便正确使用" ..."在第二行?

4 个答案:

答案 0 :(得分:4)

span添加到容器中,该容器将保存文本:

<div class="container">
  <span>text...</span>
</span>

添加此CSS:

.container {
   overflow: hidden;
   position: relative;
   background: white;   //or other color
}

.container:after {
  content: '...';       //the ellipsis
  position: absolute;   //positioned in bottom-right
  bottom: 0;            //...
  right: 0;             //...
  padding: 0 0.3em;     //give some separation from text
  background: inherit;  //same color as container
}

.container span:after {
  content: '\0000a0';   //a space
  position: absolute;   //to cover up ellipsis if needed
  width: 1000px;        //...
  z-index: 1;           //...
  background: white;    //must match container's color.  can't use inherit here.
}

Fiddle

调整容器大小,您将看到省略号仅在必要时显示。

应该适用于所有现代浏览器。

答案 1 :(得分:2)

您可以使用-webkit-line-clamp执行此操作。但是需要一个黑客攻击。您需要设置div的高度,使其只能容纳两行。

请参阅此codepen https://codepen.io/VamsiKaja/pen/xYZVzY

HTML文件:

<p class="">Pellentesque habitant morbi tristique senectus et netus et </p>

CSS文件:

p {
    width:250px;  
    font-size:20px;
    margin:1em;
    height:50px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;

}

答案 2 :(得分:0)

只想加我的两分钱。最接近的纯CSS解决方案是-webkit-line-clamp方法,但仅限于webkit浏览器,并且是旧的flexbox实现的残余(将来可能会被删除)。

如果您真的需要在垂直溢出的文本上使用省略号,也许您应该重新考虑。

我建议改为使用可滚动的文本框。我认为这是最好的解决方案,因为有以下三个原因:

  1. 适用于所有主流浏览器
  2. 没有css变通办法,黑客攻击或javascript参与
  3. 向用户传达文本已被截止但如果他们愿意,可以选择滚动它 - 此外,用户已经习惯了垂直滚动条
  4. 以下是文本框的示例代码:

    .element {
      display: inline-block;
      height: 200px;
      overflow-y: auto;
    
      /* this is to preserve text containing new lines */
      white-space: pre-line;
    }
    

    如果我必须选择,我会使用Succint行的javascript解决方案,删除一定数量后的单词并在最后添加...。如果您使用React,这很好,因为您可以将组件内部所需的所有代码实现为函数,如果您需要原始文本,您仍然可以将其作为道具值。

答案 3 :(得分:-1)

基于-webkit-line-clamp的纯css方法:

@-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>