省略号在ie中可以使用text-overflow:ellipsis了,但有很多的浏览器都需要固定宽度了,同时ff这些浏览器并不支持text-overflow:ellipsis设置了,下文来给各位整理一下兼容各浏览器显示省略号教程。

大家应该都知道用text-overflow:ellipsis属性来实现单行文本的溢出显示省略号(…)。当然部分浏览器还需要加宽度width属性。

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

  

但是这个属性并不支持多行文本溢出显示省略号,这里根据应用场景介绍几个方法来实现这样的效果。

WebKit浏览器或移动端的页面
在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;,可以用来多行文本的情况下,用省略号“…”隐藏超出范围的文本 。
overflow : hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;

  

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

垮浏览器兼容的方案
比较靠谱简单的做法就是设置相对定位的容器高度,用包含省略号(…)的元素模拟实现;

例如:

p {
position:relative;
line-height:1.4em;
/* 3 times the line-height to show 3 lines */
height:4.2em;
overflow:hidden;
}
p::after {
content:"...";
font-weight:bold;
position:absolute;
bottom:0;
right:0;
padding:0 20px 1px 45px;
background:url(http://css88.b0.upaiyun.com/css88/2014/09/ellipsis_bg.png) repeat-y;
}

  看demo:

这里注意几点:

height高度真好是line-height的3倍;
结束的省略好用了半透明的png做了减淡的效果,或者设置背景颜色;
IE6-7不显示content内容,所以要兼容IE6-7可以是在内容中加入一个标签,比如用<span class="line-clamp">...</span>去模拟;
要支持IE8,需要将::after替换成:after;
JavaScript 方案
用js也可以根据上面的思路去模拟,实现也很简单,推荐几个做类似工作的成熟小工具:

1.Clamp.js
下载及文档地址:https://github.com/josephschmitt/Clamp.js
使用也非常简单:

var module = document.getElementById("clamp-this-module");
$clamp(module, {clamp: 3});

2.jQuery插件-jQuery.dotdotdot
这个使用起来也很方便:

$(document).ready(function() {
$("#wrapper").dotdotdot({
// configuration goes here
});
});

-----------------------

jq控制文字数量---多行文本字符截取-仿溢出为省略号

  1.单行 (知识点:substr(0,12),截取从a到b的字符串长度的内容)

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv = "X-UA-Compatible" content ="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="renderer" content="webkit"/>
<title>无标题文档</title>
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<style>
*{ margin:0; padding:0;}
ul,li{ list-style:none;}
.txtList .icon{ display:inline-block;height:20px; cursor:pointer;}
</style>
</head> <body> <div class="txtList">
<ul>
<li><span class="text" title='文本文本本1'>文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本1</span></li>
<li><span class="text" title='文本文本文文本2'>文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本2</span></li>
<li><span class="text" title='文本文本文本3'>文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本3</span></li>
</ul>
</div> <script type="text/javascript">
$( function(){ var $name_text=$(".txtList li .text");
var substr_text=$name_text.text();
if(substr_text.length>12){
$name_text.text(substr_text.substr(0,12)+"...");
} }) </script>
</body>
</html>

  2.多行 (主要是宽度和行高。)

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv = "X-UA-Compatible" content ="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="renderer" content="webkit"/>
<title>无标题文档</title>
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<style>
*{ margin:0; padding:0;}
ul,li{ list-style:none;}
.txtList{ width:100px;}
.txtList .icon{ display:inline-block; width:20px; height:20px; background:url(images/icon1.png) no-repeat; cursor:pointer;}
.txtList .icon.curr{background:url(images/icon2.png) no-repeat;}
</style>
</head> <body> <div class="txtList">
<ul>
<li><i class="icon"></i><span txt='文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本1'>文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本1</span></li>
<li><i class="icon"></i><span txt='文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本2'>文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本2</span></li>
<li><i class="icon"></i><span txt='文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本3'>文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本3</span></li>
</ul>
</div> <script type="text/javascript">
$( function(){ $(".txtList .icon").click( function(){
var _text=$(this).next("span").text();
if(_text.length>12){
$(this).next("span").text(_text.substr(0,12)+"...");
}
}); }) </script>
</body>
</html>

  

布局常见问题之css实现多行文本溢出显示省略号(…)全攻略的更多相关文章

  1. css实现多行文本溢出显示省略号(…)全攻略

    省略号在ie中可以使用text-overflow:ellipsis了,但有很多的浏览器都需要固定宽度了,同时ff这些浏览器并不支持text-overflow:ellipsis设置了,下文来给各位整理一 ...

  2. CSS 单行 多行文本溢出显示省略号

    单行文本 overflow: hidden; text-overflow:ellipsis; white-space: nowrap; 多行文本溢出显示省略号: <style type=&quo ...

  3. css实现多行文本溢出显示省略号(…)

    WebKit浏览器或移动端的页面在WebKit浏览器或移动端(绝大部分是WebKit内核的浏览器)的页面实现比较简单,可以直接使用WebKit的CSS扩展属性(WebKit是私有属性)-webkit- ...

  4. CSS实现单行、多行文本溢出显示省略号(…)

    如果实现单行文本的溢出显示省略号同学们应该都知道用text-overflow:ellipsis属性来,当然还需要加宽度width属来兼容部分浏览. 实现方法: overflow: hidden; te ...

  5. CSS和JS实现单行、多行文本溢出显示省略号(该js方法有问题不对)

    如果实现单行文本的溢出显示省略号同学们应该都知道用text-overflow:ellipsis属性来,当然还需要加宽度width属来兼容部分浏览. 实现方法: overflow: hidden; te ...

  6. CSS单行、多行文本溢出显示省略号(……)

    这个问题经常遇到 1.单行文本溢出显示省略号(…) text-overflow:ellipsis-----部分浏览器还需要加宽度width属性 overflow:hidden;text-overflo ...

  7. CSS单行、多行文本溢出显示省略号

    如果实现单行文本的溢出显示省略号小伙伴们应该都知道用text-overflow:ellipsis属性来,当然还需要加宽度width属来兼容部分浏览. 实现方法: overflow: hidden; t ...

  8. CSS单行、多行文本溢出显示省略号(……)解决方案

    单行文本溢出显示省略号(-) text-overflow:ellipsis-----部分浏览器还需要加宽度width属性 .ellipsis{ overflow: hidden; text-overf ...

  9. css多行文本溢出显示省略号(…)

    text-overflow:ellipsis属性可以实现单行文本的溢出显示省略号(…).但部分浏览器还需要加宽度width属性. css代码: overflow: hidden; text-overf ...

随机推荐

  1. 四、spark常用函数说明学习

    1.parallelize       并行集合,切片数.默认为这个程序所分配到的资源的cpu核的个数.       查看大小:rdd.partitions.size      sc.paraliel ...

  2. SQL性能优化注意事项

    1.选用适合的Oracle优化器 Oracle的优化器共有3种: a.RULE(基于规则) b.COST(基于成本) c.CHOOSE(选择性) 设置缺省的优化器,可以通过对init.ora文件中OP ...

  3. 仿qq的侧拉菜单效果

    自定义控件 import android.animation.ArgbEvaluator; import android.animation.FloatEvaluator; import androi ...

  4. C#23种开发模式,陆续完善中

    #region 单例模式 #region 线程非安全单例模式 public class Singleton1 { private Singleton1() { } private static Sin ...

  5. 抓包分析SIP消息

    I. 实验环境 SIP服务器: Yate服务器, IP:port=10.15.3.34:5060 SIP客户端: 移植Sipdroid的Android网络电话软件, IP: 10.15.3.73, A ...

  6. linux看代码方法和建议

    http://blog.csdn.net/lxl584685501/article/details/46803077

  7. nginx在window上无法启动的问题

    内容列表: 简要介绍 下载安装 配置测试 一.简要介绍 Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP ...

  8. 在MacOS下Python安装lxml报错xmlversion.h not found 报错的解决方案

    最近在看一个自动化测试框架的问题,需要用到Lxml库,下载lxml总是报错. 1,使用pip安装lxml pip install lxml 2,然后报错了,报错内容是: In file include ...

  9. Python学习笔记——基础篇【第七周】———FTP作业(面向对象编程进阶 & Socket编程基础)

    FTP作业 本节内容: 面向对象高级语法部分 Socket开发基础 作业:开发一个支持多用户在线的FTP程序 面向对象高级语法部分 参考:http://www.cnblogs.com/wupeiqi/ ...

  10. [SOJ] 无路可逃?

    Description 唐僧被妖怪关在迷宫中.孙悟空好不容易找到一张迷宫地图,并通过一个魔法门来到来到迷宫某个位置.假设迷宫是一个n*m的矩阵,它有两种地形,1表示平地,0表示沼泽,孙悟空只能停留在平 ...