使用文本溢出截断多行文本:省略号

时间:2016-04-04 22:44:09

标签: css css3 overflow

如何让我的文字填满<p>标签的空格,然后用省略号将其剪掉?

你可以看到一张&#34;卡&#34;的例子。这意味着在这里填写文字。该卡是固定高度,150px,20px填充。段落元素在卡片中只有固定数量的空间,不应该展开。使用空格时应删除文本:https://jsfiddle.net/os986qsg/1/

关于SO的其他问题,text-overflow: ellipsis的元素也需要text-wrap: nowrap。只有在需要1行文本时才能使用此解决方案。在这种情况下,我想要多行文本,然后在文本到达其垂直空间的末尾时截止。

2 个答案:

答案 0 :(得分:2)

您可以使用webkit-line-clamp属性 - 此属性允许您只显示所需的行,这样您就可以放置62等等。以下示例:

.card {
  width: 400px;
  height: 150px;
  background: white;
  border: 1px solid #EAEAEA;
  box-shadow: 0px 1px 1px 0px rgba(0,0,0,0.18);
  padding: 20px;
}

h4 {
  margin: 0;
}

p {
  text-overflow: ellipsis;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 6;
  -webkit-box-orient: vertical;
}
<div class="card">
  <h4>Test</h4>
  <p>
    A test or examination (informally, exam) is an assessment intended to measure a test-taker's knowledge, skill, aptitude, physical fitness, or classification in many other topics (e.g., beliefs).[1] A test may be administered verbally, on paper, on a computer, or in a confined area that requires a test taker to physically perform a set of skills. Tests vary in style, rigor and requirements. For example, in a closed book test, a test taker is often required to rely upon memory to respond to specific items whereas in an open book test, a test taker may use one or more supplementary tools such as a reference book or calculator when responding to an item.
  </p>
</div>

修改

  

仅Chrome和Safria支持此功能

您可以尝试全球支持的这一项,我们使用:before:after元素来操纵p代码

.card {
  width: 400px;
  height: 150px;
  background: white;
  border: 1px solid #EAEAEA;
  box-shadow: 0px 1px 1px 0px rgba(0,0,0,0.18);
  padding: 20px;
}

h4 {
  margin: 0;
}

p {
  /* hide text if it more than N lines  */
  overflow: hidden;
  /* for set '...' in absolute position */
  position: relative; 
  /* use this value to count block height */
  line-height: 1.2em;
  /* max-height = line-height (1.2) * lines max number (3) */
  max-height: 112px; 
  /* fix problem when last visible word doesn't adjoin right side  */
  text-align: justify;  
  /* place for '...' */
  margin-right: -1em;
  padding-right: 1em;
}
/* create the ... */
p:before {
  /* points in the end */
  content: '';
  /* absolute position */
  position: absolute;
  /* set position to right bottom corner of block */
  right: 0;
  bottom: 0;
}
/* hide ... if we have text, which is less than or equal to max lines */
p:after {
  /* points in the end */
  content: '';
  /* absolute position */
  position: absolute;
  /* set position to right bottom corner of text */
  right: 0;
  /* set width and height */
  width: 1em;
  height: 1em;
  margin-top: 0.2em;
  /* bg color = bg color under block */
  background: white;
}
<div class="card">
  <h4>Test</h4>
  <p>
    A test or examination (informally, exam) is an assessment intended to measure a test-taker's knowledge, skill, aptitude, physical fitness, or classification in many other topics (e.g., beliefs).[1] A test may be administered verbally, on paper, on a computer, or in a confined area that requires a test taker to physically perform a set of skills. Tests vary in style, rigor and requirements. For example, in a closed book test, a test taker is often required to rely upon memory to respond to specific items whereas in an open book test, a test taker may use one or more supplementary tools such as a reference book or calculator when responding to an item.
  </p>
</div>

答案 1 :(得分:1)

Clamp.js是一个有效的JavaScript解决方案。 https://github.com/josephschmitt/Clamp.js/

以下是如何使用它的示例:

// Get the DOM node.
var myParagraph = $('.myParagraph')[0];

// Clamp it.
$clamp(myParagraph, { clamp: 3 });

编辑:不能在Firefox中使用