【问题标题】:how can I use webkit-line-clamp?如何使用 webkit-line-clamp?
【发布时间】:2020-12-02 04:21:25
【问题描述】:

我有这段代码可以让我的段落变短

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

但问题是这段代码使所有内容都在一行中。我想用 3 行文字然后... 我在 Google 上搜索了它,发现我们有一个 WebKit 供它使用

webkit-line-clamp

但我不知道如何使用它并且它不起作用......

【问题讨论】:

  • 嗨!请添加一些代码 sn-p 显示您的确切问题。
  • 并非所有浏览器都支持线夹。
  • @DebsmitaPaul 嗨,伙计.. 请再读一遍我的问题 :)
  • 您能否详细说明“不工作”? webkit-line-clamp 是怎么失效的?

标签: html css wordpress web


【解决方案1】:

方法 - 1:使用text-overflow: ellipsis;

span {
    white-space: nowrap;
    text-overflow: ellipsis;
    width: 250px;
    display: block;
    overflow: hidden
}
<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 book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span>

方法 - 2:使用-webkit-line-clamp

p {
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical; 
}
<p>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 book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

【讨论】:

  • 朋友您好,感谢您的帮助。我不想在三行之后显示(...)
  • 现在检查@nafashmn.ir 我更新了答案。有两种方法。
【解决方案2】:

没有弹性盒的解决方案

设置文本容器的高度并相应地使用 webkit-line-clamp。

.line-clamp-3 {
  display: block;
  display: -webkit-box;
  height: 50px;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

文本将像这样显示。

弹性盒解决方案

您也可以通过此链接获得帮助:https://css-tricks.com/flexbox-truncated-text/

.flex-parent {
  display: flex;
}

.short-and-fixed {
  width: 30px;
  height: 30px;
}

.long-and-truncated {
  margin: 0 20px;
  flex: 1;
  min-width: 0;
}

.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;
}
<div class="flex-parent">
  <div class="flex-child short-and-fixed">
  </div>
  <div class="flex-child long-and-truncated">
    <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 book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span>
  </div>
  <div class="flex-child short-and-fixed">
  </div>
</div>

【讨论】:

  • 朋友您好,感谢您的帮助。使用此代码可以解决我的问题,但不会在行尾留下三个点(...)
  • 请分别使用 -webkit-line-clamp 和 height 显示三个点(...)。你能分享一下文本 div 容器吗?
猜你喜欢
  • 2013-09-16
  • 2021-01-24
  • 2017-03-29
  • 1970-01-01
  • 2021-09-07
  • 2013-12-01
  • 2019-02-09
  • 1970-01-01
  • 2016-08-18
相关资源
最近更新 更多