-
用CSS实现文字超出部分显示省略号
网络 2014/5/8 9:41:05123456789101112131415161718192021222324252627282930313233343536373839<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
>
<
meta
http-equiv
=
"Content-Type"
content
=
"text/html; charset=utf-8"
/>
<
title
>省略号 test</
title
>
<
style
type
=
"text/css"
>
*{
margin:0;
padding:0;
}
body{
padding:10px;
font-family:Arial;
}
#test {
position:relative;
width:150px;
height:20px;
line-height:20px;
text-overflow:ellipsis;
white-space:nowrap;
*white-space:nowrap;
overflow:hidden;
border:1px solid #999;
}
#test span{
position:absolute;
top:0;
right:0;
display:block;
float:left;
}
</
style
>
</
head
>
<
body
>
<
div
id
=
"test"
>小虾奋斗起来吧,加油加油加油加油加油加油</
div
>
</
body
>
</
html
>
使用text-overflow:ellipsis对溢出文本显示省略号有两个好处,一是不用通过程序限定字数;二是有利于SEO。需要使用对对溢出文本显示省略号的通常是文章标题列表,这样处理对搜索引擎更友好,因为标题实际上并未被截字,而是局限于宽度而未被显示而已。
通常的做法是这样的:
1.overflow:hidden;
2.text-overflow:ellipsis;
3.-o-text-overflow:ellipsis;
4.white-space:nowrap;
5.width:100%;
其中,overflow: hidden和white-space: nowrap都是必须的否则不会显示省略号;-o-text-overflow: ellipsis针对Opera;而宽度的设定主要是针对IE6;
该方法支持Internet Explorer, Safari, Chrome 和 Opera,但FF并不支持,不过可以通过Jquery来实现类似的效果。
下载这个Jquery插件:jQuery ellipsis plugin
调用方法:
1.$(document).ready(function() {
2. $('.ellipsis').ellipsis();
3.}阅读(756) 分享(0)
上一篇: 马云内部邮件:为什么这15年我们能幸运的生存下来
下一篇: Web app设计浅谈