css显示省略号

html-css05

css显示省略号,第1张

.goods {

/* 一行显示*/

-webkit-line-clamp: 1

/* 将对象作为弹性伸缩盒子模型显示 */

display: -webkit-box

/*子元素的排列方式 */

-webkit-box-orient: vertical

text-overflow:ellipsis

overflow: hidden

word-break:break-all

}

语法:

text-overflow:clip/ellipsis

默认值:clip

适用于:所有元素

clip: 当对象内文本溢出时不显示省略标记(...),而是将溢出的部分裁切掉。

ellipsis: 当对象内文本溢出时显示省略标记(...)。

在使用的时候,有时候发现不会出现省略标记效果,经过测试发现,使用ellipsis的时候,必须配合overflow:hiddenwhite-space:nowrapwidth:具体值这三个样式共同使用才会有效果。

在WebKit浏览器或移动端(绝大部分是WebKit内核的浏览器)的页面实现比较简单,可以直接使用WebKit的CSS扩展属性(WebKit是私有属性)-webkit-line-clamp ;注意:这是一个 不规范的属性(unsupported WebKit property),它没有出现在 CSS 规范草案中。

-webkit-line-clamp用来限制在一个块元素显示的文本的行数。 为了实现该效果,它需要组合其他的WebKit属性。常见结合属性:

display: -webkit-box必须结合的属性 ,将对象作为弹性伸缩盒子模型显示 。

-webkit-box-orient 必须结合的属性 ,设置或检索伸缩盒对象的子元素的排列方式 。

text-overflow: ellipsis,可以用来多行文本的情况下,用省略号“…”隐藏超出范围的文本。

这个属性只合适WebKit浏览器或移动端(绝大部分是WebKit内核的)浏览器

效果如图所示:

从效果上来看,它的优点有:

1.响应式截断,根据不同宽度做出调整

2.文本超出范围才显示省略号,否则不显示省略号

3.浏览器原生实现,所以省略号位置显示刚好

但是缺点也是很直接,因为 -webkit-line-clamp 是一个不规范的属性,它没有出现在 CSS 规范草案中。也就是说只有 webkit 内核的浏览器才支持这个属性,像 Firefox, IE 浏览器统统都不支持这个属性,浏览器兼容性不好。

使用场景:多用于移动端页面,因为移动设备浏览器更多是基于 webkit 内核,除了兼容性不好,实现截断的效果不错。

效果如图:

适合场景:文字内容较多,确定文字内容一定会超过容器的,那么选择这种方式不错。但文字未超出行的情况下也会出现省略号,可结合js优化该方法。

注:

css:

js:

网上有很多介绍关于使用JavaScript实现多行文本溢出省略的办法,有的使用插件,有的使用自己封装好的JavaScript文件,但是,我认为,还是自己写的js比较好用。

插件:

另外,word-break: normal可以防止文字被部分截断,这个在内容为英文的情况下显得尤其重要。 要实现多行文本自动截断以省略号结尾的效果,通常的做法是使用JavaScript脚本。下面这些文章给出了如何通过脚本进行字符串截断,不过仅限于英文环境。这里是一个可以在多个通用浏览器中实现该效果的例子: html, body, p { margin: 0padding: 0font-family: sans-serif} .ellipsis { overflow: hiddenheight: 200pxline-height: 25pxmargin: 20pxborder: 5px solid #AAA} .ellipsis:before { content:""float: leftwidth: 5pxheight: 200px} .ellipsis >*:first-child { float: rightwidth: 100%margin-left: -5px} .ellipsis:after { content: "\02026"box-sizing: content-box-webkit-box-sizing: content-box-moz-box-sizing: content-boxfloat: rightposition: relativetop: -25pxleft: 100%width: 3emmargin-left: -3empadding-right: 5pxtext-align: rightbackground: -webkit-gradient(linear, left top, right top, from(rgba(255, 255, 255, 0)), to(white), color-stop(50%, white))background: -moz-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white)background: -o-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white)background: -ms-linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white)background: linear-gradient(to right, rgba(255, 255, 255, 0), white 50%, white)}Call me Ishmael. Some years ago – never mind how long precisely – having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world. It is a way I have of driving off the spleen, and regulating the circulation. Whenever I find myself growing grim about the mouthwhenever it is a damp, drizzly November in my soulwhenever I find myself involuntarily pausing before coffin warehouses, and bringing up the rear of every funeral I meetand especially whenever my hypos get such an upper hand of me, that it requires a strong moral principle to prevent me from deliberately stepping into the street, and methodically knocking people's hats off – then, I account it high time to get to sea as soon as I can. 通过修改.ellipsis和.ellipsis:before样式中height属性的值来指定容器的高度。