css - 截断多行文本并出现文本溢出 : ellipsis

标签 css overflow

如何让我的文本填充 <p> 的空间标签,然后用省略号将其截断?

您可以在此处查看用于填充文本的“卡片”示例。该卡片的高度固定,为 150 像素,内边距为 20 像素。段落元素在卡片内仅具有固定的空间量,并且不应扩展。使用空格时文本应被截断:https://jsfiddle.net/os986qsg/1/

从关于 SO 的其他问题中,一个带有 text-overflow: ellipsis 的元素还需要text-wrap: nowrap 。仅当您需要 1 行文本时,此解决方案才可接受。在本例中,我想要多行文本,然后在文本到达其垂直空间的末尾时进行截断。

最佳答案

您可以使用 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>

编辑

This is only supported on Chrome and 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>

关于css - 截断多行文本并出现文本溢出 : ellipsis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36414200/

相关文章:

jQuery - 多个音频元素和音量控制

jquery - 我的 jQuery "showcase"- 不透明度问题 :)

html - 停止 div 延伸到父级下方

ios - 使用溢出 :Scroll 时移动/ios 滚动缓慢

javascript - 如何根据里面的文本在不同的元素上应用不同的类?

javascript - 如何停止显示 window.status?

html - 使用 Overflow-y :scroll 将 child 拖出容器

html - Chrome 自动隐藏垂直滚动条

css - 类等于另一个类

jquery - 如何在这个 div 上添加滚动条?