如何使用 webkit-line-clamp?

naf*_*.ir 5 html css wordpress web

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

white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
Run Code Online (Sandbox Code Playgroud)

但问题是这段代码将所有内容都放在一行中。我想用 3 行文本...我在 Google 中搜索了它,我发现我们有一个 WebKit。

webkit-line-clamp
Run Code Online (Sandbox Code Playgroud)

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

Ray*_* AC 6

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

span {
    white-space: nowrap;
    text-overflow: ellipsis;
    width: 250px;
    display: block;
    overflow: hidden
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

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

p {
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical; 
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)


Ahm*_*mad 1

没有 Flexbox 的解决方案

设置文本容器的高度并相应地使用 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;
}
Run Code Online (Sandbox Code Playgroud)

文本将像这样显示。 在此输入图像描述

使用 Flexbox 的解决方案

您还可以从此链接获取帮助:https://css-tricks.com/flexbox-truncated-text/

.line-clamp-3 {
  display: block;
  display: -webkit-box;
  height: 50px;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}
Run Code Online (Sandbox Code Playgroud)
.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;
}
Run Code Online (Sandbox Code Playgroud)