text-overflow
时间:2010-12-07 来源:seili
程序截取一定长度的字符后输出到前台显示或者用js截取一定长度的字符,但是这样做存在一个中文和英文字符宽度
不一致的问题,通用性很差。
所以最好的办法是用css直接在前台控制显示,text-overflow:clip(不显示省略号,只是根据宽度直接剪裁)/ellipsis(当字符长度溢出的时候溢出部分会显示省略号)/ellipsis-word(显示省略号,插入的位置是最后一个词),注意输出的标题是完整的,只是受容器大小的局限显
示不全而已,同时还要配合强制文本在一行内显示(white-space:nowrap)以及溢出的内容设置为隐藏
(overflow:hidden)这两个属性一起使用,才能达到当文本在一定宽度内溢出时显示省略号。
例如,为p定义text-overflow:ellipsis
p{
white-space:nowrap;
width:200px;
overflow:hidden;
-o-text-overflow:ellipsis;
text-overflow:ellipsis;
}
这种写法在ie、360、tt、max、世界之窗(ie5中是按照容器宽度直接截取)中都是兼容的,但不兼容ff
兼容ff可以定义一个xml,为此xml保存名为a.xml
<?xml version="1.0"?>
<bindings
xmlns="http://www.mozilla.org/xbl"
xmlns:xbl="http://www.mozilla.org/xbl"
xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
>
<binding id="ellipsis">
<content>
<xul:window>
<xul:description crop="end"
xbl:inherits="value=xbl:text"><children/></xul:description>
</xul:window>
</content>
</binding>
</bindings>
然后在p中加一句
p{
white-space:nowrap;
width:200px;
overflow:hidden;
-o-text-overflow:ellipsis; /*opera*/
text-overflow:ellipsis;
-moz-binding: url('a.xml#ellipsis'); /* ff */
}