12 个答案:

答案 0 :(得分:199)

你总是可以通过设置max-width和溢出ellipsis来使用截断方法

p {

white-space: nowrap;

overflow: hidden;

text-overflow: ellipsis;

max-width: 200px;

}

对于多行截断,请查看flex解决方案。

截断3行的示例。

p {

overflow: hidden;

display: -webkit-box;

-webkit-line-clamp: 3;

-webkit-box-orient: vertical;

}

答案 1 :(得分:84)

有一个CSS'长度值' ch。

来自MDN

此单位表示宽度,或更准确地说是提前量度,

字形' 0'元素中的(零,Unicode字符U + 0030)

字体。

这可能与你所追求的相近。





p {

overflow: hidden;

max-width: 75ch;

}

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Deserunt rem odit quis quaerat. In dolorem praesentium velit ea esse consequuntur cum fugit sequi voluptas ut possimus voluptatibus deserunt nisi eveniet!Lorem ipsum dolor sit amet, consectetur

adipisicing elit. Dolorem voluptates vel dolorum autem ex repudiandae iste quasi. Minima explicabo qui necessitatibus porro nihil aliquid deleniti ullam repudiandae dolores corrupti eaque.





答案 2 :(得分:31)

尝试将其设置为max-width后截断字符。我在这种情况下使用了75ch

p {

white-space: nowrap;

overflow: hidden;

text-overflow: ellipsis;

max-width: 75ch;

}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin nisi ligula, dapibus a volutpat sit amet, mattis etc. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin nisi ligula, dapibus a volutpat sit amet, mattis etc.

对于多行截断,请点击链接。

我们将使用webkit css。简而言之,WebKit是Safari / Chrome的HTML / CSS Web浏览器渲染引擎。这可能是特定的,因为每个浏览器都由渲染引擎支持以绘制HTML / CSS网页。

答案 3 :(得分:11)

示例代码:

.limited-text{

white-space: nowrap;

width: 400px;

overflow: hidden;

text-overflow: ellipsis;

}

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut odio temporibus voluptas error distinctio hic quae corrupti vero doloribus optio! Inventore ex quaerat modi blanditiis soluta maiores illum, ab velit.

答案 4 :(得分:4)

在Chrome中,您可以设置显示为“ -webkit-line-clamp”的行数:

display: -webkit-box;

-webkit-box-orient: vertical;

-webkit-line-clamp: 3; /* Number of lines displayed before it truncate */

overflow: hidden;

答案 5 :(得分:2)

此文章是针对CSS解决方案的,但是该文章已经很老了,以防万一其他人偶然发现了这个问题并使用了Angular 4+等现代JS框架,有一种简单的方法可以通过Angular Pipes不必弄乱CSS。

也可能有“反应”或“ Vue”方法。这只是为了展示如何在框架内完成。

truncate-text.pipe.ts

/**

* Helper to truncate text using JS in view only.

*

* This is pretty difficult to do reliably with CSS, especially when there are

* multiple lines.

*

* Example: {{ value | truncateText:maxLength }} or {{ value | truncateText:45 }}

*

* If maxLength is not provided, the value will be returned without any truncating. If the

* text is shorter than the maxLength, the text will be returned untouched. If the text is greater

* than the maxLength, the text will be returned with 3 characters less than the max length plus

* some ellipsis at the end to indicate truncation.

*

* For example: some really long text I won't bother writing it all ha...

*/

@Pipe({ name: 'truncateText' })

export class TruncateTextPipe implements PipeTransform {

transform(value: string, ...args: any[]): any {

const maxLength = args[0]

const maxLengthNotProvided = !maxLength

const isShorterThanMaximumLength = value.length < maxLength

if (maxLengthNotProvided || isShorterThanMaximumLength) {

return value

}

const shortenedString = value.substr(0, maxLength - 3)

return `${shortenedString}...`

}

}

app.component.html

{{ application.name | truncateText:45 }}

答案 6 :(得分:1)

HTML 强>

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin nisi ligula, dapibus a volutpat sit amet, mattis et dui. Nunc porttitor accumsan orci id luctus. Phasellus ipsum metus, tincidunt non rhoncus id, dictum a lectus. Nam sed ipsum a urna ac

quam.

的jQuery 强>

var p = $('#dash p');

var ks = $('#dash').height();

while ($(p).outerHeight() > ks) {

$(p).text(function(index, text) {

return text.replace(/\W*\s(\S)*$/, '...');

});

}

CSS 强>

#dash {

width: 400px;

height: 60px;

overflow: hidden;

}

#dash p {

padding: 10px;

margin: 0;

}

RESULT 强>

Lorem ipsum dolor坐下来,精神上的精神。 Proin nisi

ligula,dapibus a volutpat sit amet,mattis et ...

答案 7 :(得分:1)

CSS无法实现,您必须使用Javascript。虽然你可以将p的宽度设置为多达30个字符,但是下一个字母会自动降低,但这又不会准确,如果字符是大写字母则会有所不同。

答案 8 :(得分:0)

您始终可以查看字体的宽度并获取平均字符像素大小。然后,将其乘以所需的字符数即可。有点俗气,但可以快速解决。

答案 9 :(得分:0)

以两种不同的方式尝试我的解决方案。

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut odio temporibus voluptas error distinctio hic quae corrupti vero doloribus optio! Inventore ex quaerat modi blanditiis soluta maiores illum, ab velit.

Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ut odio temporibus voluptas error distinctio hic quae corrupti vero doloribus optio! Inventore ex quaerat modi blanditiis soluta maiores illum, ab velit.

.wrapper {

padding: 20px;

background: #eaeaea;

max-width: 400px;

margin: 50px auto;

}

.demo-1 {

overflow: hidden;

display: -webkit-box;

-webkit-line-clamp: 3;

-webkit-box-orient: vertical;

}

.demo-2 {

overflow: hidden;

white-space: nowrap;

text-overflow: ellipsis;

max-width: 150px;

}

答案 10 :(得分:0)

用于截断多行字符的纯CSS解决方案

我遇到了类似的问题,并从Hackingui.com找到了这个优秀的css解决方案。您可以阅读该文章以获取信息,但下面是主要代码。

我对它进行了测试,效果很好。希望有人在选择JS或服务器端选项之前发现它很有用

/* styles for '...' */

.block-with-text {

/* 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: 3.6em;

/* fix problem when last visible word doesn't adjoin right side */

text-align: justify;

/* place for '...' */

margin-right: -1em;

padding-right: 1em;

}

/* create the ... */

.block-with-text: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 */

.block-with-text: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;

}

答案 11 :(得分:0)

现代CSS网格答案

在CodePen上查看完整的工作代码。鉴于以下HTML:

Several paragraphs of text...

您可以使用CSS Grid创建三列,并告诉容器在包含我们段落的中间列中采用70个字符的最大宽度。

.container

{

display: grid;

grid-template-columns: 1fr, 70ch 1fr;

}

p {

grid-column: 2 / 3;

}

这是它的样子(检出CodePen为完整示例):

这是另一个示例,您可以在其中使用minmax来设置值的范围。在小屏幕上,宽度设置为50个字符,在大屏幕上,宽度设置为70个字符。

.container

{

display: grid;

grid-template-columns: 1fr minmax(50ch, 70ch) 1fr;

}

p {

grid-column: 2 / 3;

}

css设置字符长度,在css中设置最大字符长度相关推荐

  1. android 设置全屏方法,Android中设置全屏的方法

    在开发中,我们经常需要把我们的应用设置为全屏,这里有两种方式: 一是在代码中设置; 二是在配置文件中设置 一. 在代码中设置 public class BaseActivity extends Act ...

  2. 如何在不使用任何图像或跨度标签的情况下通过CSS在UL / LI html列表中设置子弹颜色[复制]

    本文翻译自:How to set Bullet colors in UL/LI html lists via CSS without using any images or span tags [du ...

  3. 计算机考试怎么设置下标,怎么在Excel中设置字符上标和下标

    小编这里要跟大家分享的是关于怎么在Excel中设置字符上标和下标的方法,很多人可能都知道在Word"格式"菜单中可以直接设置字体格式,但在Excel的"格式"菜 ...

  4. html字体怎么设置大写,如何在html中设置字体的属性

    如何在html中设置字体的属性 发布时间:2021-06-08 17:45:33 来源:亿速云 阅读:72 作者:Leah 这篇文章给大家介绍如何在html中设置字体的属性,内容非常详细,感兴趣的小伙 ...

  5. html给标题设置背景,怎么在html中设置一级标题背景

    怎么在html中设置一级标题背景 发布时间:2021-06-02 17:54:26 来源:亿速云 阅读:85 作者:Leah 这篇文章给大家介绍怎么在html中设置一级标题背景,内容非常详细,感兴趣的 ...

  6. html img设置最底层,在HTML片段中设置img标签的baseUrl

    我有一个页面托管在domain1上,我通过ajax调用来检索包含一些标签的HTML片段.这些标签有一个相对的src网址,我只想为这些标签设置指向另一个域的基本网址,如domain2.在HTML片段中设 ...

  7. pythonjs设置_在节点js中设置env变量并在python脚本中使用

    我正在node js app中设置一个env变量: process.env.data = "data-env"; 使用:print(os.environ["data&qu ...

  8. iosxib 设置图片_iOS从Xib中设置样式

    简介 iOS在写视图的有的人喜欢纯代码去写,从之前的绝对定位方式(Frame),到现在的自动布局(Autolayout),但这种方式的好处是便于复制修改和装X,但是缺点是代码不容易看,不便于修改 也有 ...

  9. 计算机word设置渐变填充,在word2013中设置渐变填充效果的详细设置步骤

    软件安装:装机软件必备包             装机软件必备包官方下载 关于电脑装机必须的软件,比如windows office系列办公软件.网页浏览器.杀毒软件.安全防护软件.刻录软件.压缩软件. ...

  10. win7计算机字体大小怎么设置,新手使用win7系统中设置桌面的字体大小的方法

    win7的系统是现在很多的小伙伴安装系统的时候最好的最好的选择,那在win7电脑中对于新手来说一些操作还是不知道的,有疑问对于字体大小的是怎么自己修改字体还有文字的大小是怎么实现的呢,对于这个问题今天 ...

最新文章

  1. 用 Java 爬小姐姐图片,这个厉害了。。。
  2. Xamarin简介与Xamarin支持MVC设计模式
  3. ubuntu 下mysql的常用命令
  4. 计算机的定点运算器原理,计算机组成原理第二章第10讲定点运算器的组成.ppt
  5. 【Pytorch】model.train()和model.eval()用法和区别,以及model.eval()和torch.no_grad()的区别
  6. PROTEUS元件库元件称呼 .
  7. 人脸识别系统Python源代码的实现
  8. jsp mysql超市管理_基于WEB的小型超市管理系统的设计与实现(JSP,MySQL)
  9. 安卓夜神模拟器设置代理
  10. 局域网即时通讯Active Messenger 完美破解版本 最新版本破解
  11. 深度爬取网易Lofter的爬虫
  12. 【泡咖啡1】linux下caffe编译以及python环境配置手记
  13. Python金融分析
  14. 亚马逊数据技能,选择新品的8大核心原则
  15. 差商近似1阶导数matlab,常微分方程的解法 (一): 常微分方程的离散化 :差商近似导数、数值积分方法、Taylor 多项式近似...
  16. 编写Firefox扩展(翻译)
  17. 〖Python网络爬虫实战⑫〗- XPATH语法介绍
  18. Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器
  19. 【阿里云镜像】使用阿里云oVirt镜像部署oVirt平台
  20. 新媒体人必备的4个热点数据分析工具,超实用

热门文章

  1. matlab区分卷积和相关
  2. centos8部署Django项目---后台运行
  3. html往下滑变成水平,HTML - 水平滑块CSS最佳方法_html_开发99编程知识库
  4. 圣诞节,程序员应该给女朋友送一个线上圣诞树
  5. 无法获取签名信息,请上传有效包(110506)
  6. java获取系统当前时间格式化_java 获取系统当前时间并格式化
  7. android中按一个按钮弹出字,允许用户在Android中长按一次即可编辑按钮文字
  8. React中ref的使用方法
  9. Three.js制作360度全景图
  10. js实现类名的添加与移除