首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用webkit-line-clamp?

如何使用webkit-line-clamp?
EN

Stack Overflow用户
提问于 2020-08-12 20:05:21
回答 2查看 3.5K关注 0票数 2

我有这个代码来让我的段落变短。

代码语言:javascript
复制
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;

但问题是,这段代码将所有内容都写在一行中。我想用3行文字,然后...我在谷歌上搜索了一下,发现我们有它的WebKit

代码语言:javascript
复制
webkit-line-clamp

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

EN

回答 2

Stack Overflow用户

发布于 2020-08-12 20:29:34

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

代码语言:javascript
复制
span {
    white-space: nowrap;
    text-overflow: ellipsis;
    width: 250px;
    display: block;
    overflow: hidden
}
代码语言:javascript
复制
<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

代码语言:javascript
复制
p {
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical; 
}
代码语言:javascript
复制
<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>

票数 2
EN

Stack Overflow用户

发布于 2020-08-12 20:22:07

不带flexbox的解决方案

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

代码语言:javascript
复制
.line-clamp-3 {
  display: block;
  display: -webkit-box;
  height: 50px;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

文本将按如下方式显示。

使用flexbox解决方案

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

代码语言:javascript
复制
.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;
}
代码语言:javascript
复制
<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>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63376172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档